python(pymysql操作数据库),,第一种方式impor


第一种方式

import pymysql# 打开数据库连接db = pymysql.connect(host="192.168.88.11", user="root",password="123", db="p1807", port=3306)# 使用cursor()方法获取操作游标cur = db.cursor()# 1.查询操作# 编写sql 查询语句user 对应我的表名sql = "select * from students"try:cur.execute(sql)# 执行sql语句results = cur.fetchall()# 获取查询的所有记录for i in results:#遍历结果print(i)except Exception as e:raise efinally:db.close()# 关闭连接

第二种方式

# 打开数据库连接db = pymysql.connect(host="192.168.88.11", user="root",password="123", db="aaa", port=3306)cur = db.cursor()try:for i in range(1000):cur.execute("insert into qqq values (‘ccc-%s‘)‘` %i)results = cur.fetchall() # 获取查询的所有记录db.commit() #提交except Exception as e:raise efinally:db.close()# 关闭连接

python(pymysql操作数据库)

评论关闭