1 Star 0 Fork 19

小春/Python_Learning

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
insert.py 798 Bytes
Copy Edit Raw Blame History
阿基米东 authored 2020-09-10 20:44 +08:00 . pymysql
import pymysql
import datetime
# 打开数据库连接
db = pymysql.connect(host="localhost",user="root",password="",db="test",charset="utf8")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 定义添加 sql 语句
data = (20190001, "Allen", 'allen@gmail.com', \
'{0}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
sql = "INSERT INTO USER(id, name, email, cdate) value('%d','%s','%s','%s')"%(data)
try:
# 使用 execute() 方法执行 SQL
m = cursor.execute(sql)
# 事务提交
db.commit()
print("成功操作条数:",m)
#print("成功操作条数:",cursor.rowcount)
except Exception as err:
# 事务回滚
db.rollback()
print("SQL执行错误,原因:",err)
# 关闭数据库连接
db.close()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wython/Python_Learning.git
git@gitee.com:wython/Python_Learning.git
wython
Python_Learning
Python_Learning
master

Search