插入时需要进行commit确认
from pymysql import Connection
conn = Connection(
host="localhost", #主机名(ip)
port=3306, #端口
user="root", #sql账户
password='123321' #密码
)
cursor = conn.cursor() #获取游标对象
conn.select_db("student")
#执行sql插入数据
cursor.execute("insert into student value (1001, '李晨', 20)")
#要进行数据更改要进行确认
conn.commit()
conn.close()
自动提交无需手动commit
from pymysql import Connection
conn = Connection(
host="localhost", #主机名(ip)
port=3306, #端口
user="root", #sql账户
password='123321', #密码
autocommit = True #在构建链接对象的时候可以设置自动的commit的 属性
)
cursor = conn.cursor() #获取游标对象
conn.select_db("student")
#执行sql插入数据
cursor.execute("insert into student value (1001, '李晨', 20)")
cursor.execute("insert into student value (1002, '郑凯', 18)")
#要进行数据更改要进行确认
conn.close()