当前位置 : 主页 > 编程语言 > python >

pymysql模块,python与MySQL之间的交互

来源:互联网 收集:自由互联 发布时间:2022-06-15
import pymysql db = pymysql.connect(host="192.168.137.1", port=3306, user="wu", password="123456", db="爸爸测试", charset="utf8") #这是个坑啊utf-8没有- # cursor = db.cursor() # sql = "select * from boy" # 这是一个坑,拼接字
import pymysql

db = pymysql.connect(host="192.168.137.1",
port=3306,
user="wu",
password="123456",
db="爸爸测试",
charset="utf8") #这是个坑啊utf-8没有-
# cursor = db.cursor()
# sql = "select * from boy"

# 这是一个坑,拼接字符串的坑
# sql = "select * from t1 where user = %s and pwd = %s"
# return_value = cursor.execute(sql,("wu","sda"))
# sql = "insert into boy(name,age) values(%s,%s)"
# return_value = cursor.execute(sql,("老九",1))
# return_value = cursor.executemany(sql,[(),(),()])

cursor = db.cursor(pymysql.cursors.DictCursor) #传参,输出结果为字典
# print(cursor.lastrowid) 插入的时候看光标

sql = "select * from boy"
return_value = cursor.execute(sql)

print(cursor.fetchone())

# cursor.scroll(3,mode="relative")
cursor.scroll(0,mode="absolute")

print(cursor.fetchall())
# result = cursor.fetchall()
# # print(db)
# print(return_value)
# print(result)
db.commit()
cursor.close()
db.close()

 

-----------------------------------------------------------------------------------------------------------------------------------------

上一篇:python朗读播放中文和英文
下一篇:没有了
网友评论