import pymysql
# 打开数据库连接
def db_insert(sql_insert):
db = pymysql.connect(host='47.104.*.*',
port=3308,
user='root',
passwd='123456',
db='ernest')
# 使用cursor()方法创建一个游标对象cur (可以理解为激活数据库)
cur = db.cursor()
try:
cur.execute(sql_insert) # 使用execute()执行SQL语句
db.commit()
except Exception as e:
print("发生的错误:%s" % e)
db.rollback()
finally:
db.close()
if __name__ == "__main__":
sql_add ="insert student (name,age) VALUES ('李四',22)"
db_insert(sql_add)