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提交