import pymysql
config = {
'host': '127.0.0.1',
'user': 'root',
'passwd': '123456',
"port": 3306,
'db': 'test_guest',
"charset": "utf8"
}
try:
db = pymysql.Connect(**config)
print('数据库连接成功')
except Exception as e:
print('连接数据库失败!', str(e))
cursor = db.cursor()
sql_insert1 = "INSERT INTO test_guest.auth_group (id, NAME) VALUES (1, '强');"
try:
cursor.execute(sql_insert1)
db.commit() # 提交到数据库执行
except Exception as e:
print('插入失败', str(e))
finally:
cursor.close()
db.close
操作的时候,执行没有成功,是因为没有进行commit提交