时间:2021-07-01 10:21:17 帮助过:27人阅读
pip install PyMySQL
# -*-coding:utf-8-*- import pymysql """ Python 访问 MySQL 数据库 """ """ 创建表的命令 create table t1( id int, name varchar(20), age int ); """ # 连接到数据库 ,注意使用127.0.0.1 conn = pymysql.Connect("127.0.0.1", "root", "123456", "python") # 得到游标 cur = conn.cursor() # 执行语句 # cur.execute("select version()") # 提取第一条记录 # result = cur.fetchone() # print(result) # 插入记录 # cur.execute("insert into t1(id ,name ,age) values(2 ,‘amy‘ , 23)") # 查询 t1表 所有记录 print("=================") cur.execute("select * from t1") results_list = cur.fetchall() for r in results_list: id = r[0] name = r[1] age = r[2] print("%d/%s/%d\r\n" % (id, name, age), ) # 删除记录 # cur.execute("delete from t1 where id >= 1000") conn.commit() cur.close()
[Python_6] Python 配置 MySQL 访问
标签:delete create 数据库 on() pymysql com odi version cut