python操作mysql,進行數據插入(插入數據不成功,因爲沒加commit)

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章