python 操作mysql

    技术2022-07-11  108

    1 安装mysql https://blog.csdn.net/qq_37350706/article/details/81707862 2 安装pymysql

    pip install pymysql

    使用 pymysql 的 connect() 方法连接数据库,connect 参数解释如下:

    host:MySQL服务的地址,若数据库在本地上,使用 localhost 或者127.0.0.1。如果在其它的服务器上,则写对应的 IP地址port:服务的端口号,默认为3306,不写则为默认值。user:登录数据库的用户名passwd:登录 MySQL 的密码db:数据库名charset:设置为 utf8 编码,解决存汉字乱码问题 conn.cursor():获取游标

    3 sql 执行命令:execute()和executemany()

    execute(query,args=None):

    函数作用:执行单条的sql语句,执行成功后返回受影响的行数 参数说明: query:要执行的sql语句,字符串类型 args:可选的序列或映射,用于query的参数值。如果args为序列,query中必须使用%s做占位符;如果args为映射,query中必须使用%(key)s做占位符

    executemany(query,args=None): 函数作用:批量执行sql语句,比如批量插入数据,执行成功后返回受影响的行数 参数说明: query:要执行的sql语句,字符串类型 args:嵌套的序列或映射,用于query的参数值

    其他游标对象如下表:

    名称描述close()关闭游标,之后游标不可用fetchone()返回一条查询结果fetchall()返回所有查询结果fetchmany([size])返回size条查询结果

    4 简单的demo

    # 导入模块 import pymysql # 打开数据库连接 class Db: def __init__(self,host,port,user,password,database,charset): self.host=host self.user=user self.password=password self.database=database self.charset=charset self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database, charset=self.charset) self.cursor = self.conn.cursor() def excutesql(self,sql): try: self.cursor.execute(sql) self.conn.commit() except: self.conn.rollback() def close(self): self.conn.close() def querymany(self,sql): self.cursor.execute(sql) return self.cursor.fetchall() def queryOne(self,sql): self.cursor.execute(sql) return self.cursor.fetchone() db=Db('127.0.0.1','3306','root','rockwell123@RA','db','utf8') print(db.excutesql('replace into db.test(id,name,age) values(1,"eee",100)')) print(db.excutesql('replace into db.test(id,name,age) values(2,"www",100)')) print(db.querymany("select id, name, age from test")) print(db.queryOne("select id, name, age from test where name='eeee'"))

    5、插入数据 插入数据实现代码:

    # 导入模块 import pymysql # 打开数据库连接 class Db: def __init__(self,host,port,user,password,database,charset): self.host=host self.user=user self.password=password self.database=database self.charset=charset self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database, charset=self.charset) self.cursor = self.conn.cursor() def excutesql(self,sql): try: self.cursor.execute(sql) self.conn.commit() except: self.conn.rollback() def close(self): self.conn.close() def querymany(self,sql): self.cursor.execute(sql) return self.cursor.fetchall() def queryOne(self,sql): self.cursor.execute(sql) return self.cursor.fetchone() db=Db('127.0.0.1','3306','root','rockwell123@RA','db','utf8') print(db.excutesql('replace into db.test(id,name,age) values(1,"eee",100)'))

    6、查询数据 Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

    fetchone(): 该方法获取下一个查询结果集。结果集是一个对象 fetchall(): 接收全部的返回结果行. rowcount: 这是一个只读属性,并返回执行 execute()方法后影响的行数。 查询数据代码如下:

    # 导入模块 import pymysql # 打开数据库连接 class Db: def __init__(self,host,port,user,password,database,charset): self.host=host self.user=user self.password=password self.database=database self.charset=charset self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database, charset=self.charset) self.cursor = self.conn.cursor() def excutesql(self,sql): try: self.cursor.execute(sql) self.conn.commit() except: self.conn.rollback() def close(self): self.conn.close() def querymany(self,sql): self.cursor.execute(sql) return self.cursor.fetchall() def queryOne(self,sql): self.cursor.execute(sql) return self.cursor.fetchone() db=Db('127.0.0.1','3306','root','rockwell123@RA','db','utf8') print(db.querymany("select id, name, age from test"))

    7、数据库表更新操作

    # 导入模块 import pymysql # 打开数据库连接 class Db: def __init__(self,host,port,user,password,database,charset): self.host=host self.user=user self.password=password self.database=database self.charset=charset self.conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database, charset=self.charset) self.cursor = self.conn.cursor() def excutesql(self,sql): try: self.cursor.execute(sql) self.conn.commit() except: self.conn.rollback() def close(self): self.conn.close() def querymany(self,sql): self.cursor.execute(sql) return self.cursor.fetchall() def queryOne(self,sql): self.cursor.execute(sql) return self.cursor.fetchone() db=Db('127.0.0.1','3306','root','rockwell123@RA','db','utf8') print(db.excutesql('update test set name="test" where name="eee"'))

    ini file

    [mysqld] # 设置3306端口 port=3306 # 设置mysql的安装目录 basedir=C:\Mysql # 设置mysql数据库的数据的存放目录 datadir=C:\Mysql\Data # 允许最大连接数 max_connections=200 # 允许连接失败的次数。 max_connect_errors=10 # 服务端使用的字符集默认为utf8mb4 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 默认使用“mysql_native_password”插件认证 #mysql_native_password default_authentication_plugin=mysql_native_password [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [client] # 设置mysql客户端连接服务端时默认使用的端口 port=3306 default-character-set=utf8
    Processed: 0.012, SQL: 9