Python知識分享網 - 專業(yè)的Python學習網站 學Python,上Python222
pymysql執(zhí)行insert插入操作
匿名網友發(fā)布于:2023-09-19 10:44:46
(侵權舉報)

1小時學會 Python操作Mysql數據庫之pymysql模塊技術https://www.bilibili.com/video/BV1Dz4y1j7Jr

 

 

執(zhí)行修改操作,需要通過Connection對象調用commit()方法確認提交,或者 構造方法里面,autocommit設置True,自動提交

 

from pymysql import Connection

con = None

try:
    # 創(chuàng)建數據庫連接
    con = Connection(
        host="localhost",  # 主機名
        port=3306,  # 端口
        user="root",  # 賬戶
        password="123456",  # 密碼
        database="db_python",  # 指定操作的數據庫
        autocommit=True  # 設置自動提交
    )
    # 獲取游標對象
    cursor = con.cursor()
    # 使用游標對象,執(zhí)行sql語句
    cursor.execute("insert into t_student values(null,'趙六2',25)")
    # 獲取主鍵
    print("主鍵id=", con.insert_id())
    # 確認提交
    # con.commit()
except Exception as e:
    print("異常:", e)
finally:
    if con:
        # 關閉連接
        con.close()

 

 

 

轉載自: