1.什么是pymysql?pymysql是在python3.x版本中用于连接mysql服务器的一个库,python2中使用mysqldb。pymysql遵循python数据库api v2.0的规范,并包含了pure-python mysql客户端库。
2.安装pymysql $ pip install pymysql
3.mysql数据库安装与配置在用pymysql连接mysql数据库之前,请确保mysql数据库安装配置完成,具体如何安装与配置mysql数据库,请参考 mysql 安装 和 mysql 管理。
4.1.连接数据库操作import pymysql# 数据库服务器名hostname = 'node05'# 数据库用户名user = 'root'# 数据库名database = 'cayman'# 数据库密码password = 'love88me'# 打开数据库连接conn = pymysql.connect(hostname, user, password, database)# 使用cursor()方法创建一个游标对象cursor = conn.cursor()# 使用execute()方法执行sql查询语句cursor.execute(select version())# 使用fetchone()查询单条数据data = cursor.fetchone()print(fdatabase version: {data})# 关闭数据库连接conn.close()
4.2.创建表操作import pymysql# 设置数据库配置项hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor对象创建一个流标对象cursor = db.cursor()# 使用execute()方法执行sql, 如果表存在则删除cursor.execute(drop table if exists employee)# 使用预处理语句创建表sql = create table employee( id bigint primary key auto_increment, user_name varchar(50) not null, age int, sex char(1), income float)# 执行sql语句cursor.execute(sql)# 关闭数据库连接db.close()
4.3.1.数据库插入单条语句import pymysql# 设置数据库配置项hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor对象创建一个流标对象cursor = db.cursor()# sql语句sql = insert into employee(user_name, age, sex, income) values ('风清扬', 64, '男', 22000);try: # 执行sql语句 cursor.execute(sql) # 提交 db.commit()except: # 如果发生错误就回滚 db.rollback()# 关闭数据库连接db.close()
4.3.2.数据库插入多条语句import pymysql# 设置数据库配置项hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor对象创建一个流标对象cursor = db.cursor()# sql语句sql = insert into employee(user_name, age, sex, income) values (%s, %s, %s, %s)data = ( ('风清扬', 64, '男', 22000), ('令狐冲', 22, '男', 14000), ('任盈盈', 20, '男', 10000), ('东方不败', 32, '男', 18000), ('任我行', 56, '男', 17000), ('段誉', 33, '男', 19000), ('王语嫣', 26, '女', 9000), ('木婉清', 23, '女', 6000), ('乔峰', 38, '男', 23000), ('阿朱', 24, '女', 5000), ('阿紫', 22, '女', 5500), ('虚竹', 35, '男', 11000), ('梦姑', 25, '女', 6500), ('梅超风', 41, '女', 15000), ('陈玄风', 44, '男', 12000), ('杨过', 28, '男', 24000), ('小龙女', 38, '女', 15000), ('鸠摩智', 44, '男', 16000))try: # 执行sql语句 cursor.executemany(sql, data) # 提交 db.commit()except: # 如果发生错误就回滚 db.rollback()# 关闭数据库连接db.close()
4.4.数据库查询python查询mysql使用fetchone()获取单条数据,使用fetchall()方法获取多条数据。
fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
fetchall(): 接收全部的返回结果行;
rowcount(): 这是一个只读属性,并返回执行execute()方法后影响的行数。
4.4.1.查询示例查询employee表中income(工资)大于20000的所有数据
# 1.查询employee表中工资大于20000的员工信息import pymysql# 设置数据库配置项hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor对象创建一个流标对象cursor = db.cursor()# 查询语句sql = select * from employee where income >'%d' %(20000)try: # 执行sql语句 cursor.execute(sql) # 获取所有满足条件的列表 ret = cursor.fetchall() # 遍历打印结果 for row in ret: user_name = row[1] age = row[2] sex = row[3] income = row[4] print(f员工: {user_name},年龄: {age}, 性别: {sex}, 工资: {income})except: print(无满足条件的数据或查询出错!!)# 关闭数据库连接db.close()
4.5.数据库更新操作import pymysql# 设置数据库配置项hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor对象创建一个流标对象cursor = db.cursor()# 更新语句sql = update employee set income=income+income*0.1 where sex='%c'%('女')try: # 执行sql语句 cursor.execute(sql) # 提交 db.commit()except: # 发生错误时回滚 db.rollback()# 关闭数据库db.close()
4.6.删除操作import pymysql# 设置数据库连接信息hostname = 'node05'username = 'root'password = 'love88me'database = 'cayman'# 打开数据库连接db = pymysql.connect(hostname, username, password, database)# 使用cursor()方法获取游标cursor = db.cursor()# 构建删除数据sql语句sql = delete from employee where user_name = '%s'%('鸠摩智')try: # 执行sql语句 cursor.execute(sql) # 提交 db.commit()except: # 发生异常时回滚 db.rollback()# 关闭数据库连接db.close()
4.7 执行事务操作在数据库操作中,事务机制可以保证数据的一致性。最基本的事务应当具备4个属性: 原子性、一致性、隔离性、和持久性。这四个属性被称作acid特性。
原子性(atomicity): 一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
在数据库中,事务必须将数据库从一种状态转换为另一种一致性状态,以实现一致性。一致性与原子性是密切相关的。
isolation refers to the ability of a transaction to execute without interference from other transactions.。在一个事务内部,操作和使用的数据对其他并发事务是封闭的,因此并发执行的各个事务不能互相干扰。
持久性(durability): 持续性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
python db api 2.0的事务提供了两个方法commit和rollback。对于支持事务的数据库编程中,当流标建立时,就自动开启了一个隐形的数据库事务。
以上就是mysql数据库如何安装与配置的详细内容。