py和mysql

##Python安装MySQL步骤

2018年11月30日
8:48

一、下载和安装pymysql

	1. 使用ubuntu 安装
		命令为 pip3 install pymysql       --->需要联网

二、python操作mysql

	1. 引入模块
		from pymysql import *
	2. Connection 对象  
	3. cursor 对象
	4. 基本流程
				  1 from pymysql import *
				  2 
				  3 
				  4 def main():
				  5     # 创建connection对象
				  6     conn = connect(host="localhost",port=3306,user="root",password="root",database="jing_dong",charset="utf8")
				  7     # 获取cursor 对象
				  8     cursor = conn.cursor()
				  9     # 执行SQL语句
				 10     count = cursor.execute("select id,name from goods where id>=4")
				 11     # 打印
				 12     print("查询到%d数据" %count)
				 13 
				 14     for i in range(count):
				 15         # 获取查询结果
				 16         result = cursor.fetchone()
				 17         # 打印查询结果
				 18         print(result)
				 19 
				 20 
				 21     # 关闭cursor对象
				 22     cursor.close()
				 23     # 关闭 connection 对象
				 24     conn.close()
				 25 
				 26 
				 27 
				 28 
				 29 if __name__ == "__main__":
				 30     main()

三、增删改

	1. 需要提交commit
		conn.commit()
	2. 错误需要改
		conn.rollback()   --->类似撤销

你可能感兴趣的:(MySQL数据库)