import pymysql
conn = pymysql.connect(
host = "127.0.0.1",
port = 3306,
user = "test1",
password = “123456”,
db = "myfirst",
charset = "utf8"
)
cur = conn.cursor() #定义游标
table1 = cur.execute("select * from test1") #搜索表
one = cur.fetchone() #搜索第一个数据
all = cur.fetchall() #搜索全部数据
print(all) 打印结果
操作结束后:
conn.commit() 提交事务
cur.close() 关闭游标
conn.close() 关闭连接
插入数据时默认会开启事务,需要加cur.execute("commit") 提交: