簡體   English   中英

無法使用python Pymysql更新SQL數據庫

[英]Unable to update SQL database using python Pymysql

我正在嘗試創建一個類來自動生成客戶對象,該對象將在Mysql的客戶表中自動得到更新。

雖然我能夠成功產生客戶,但是在localhost / phpmyadmin中看不到更新的結果。

from itertools import count
import pymysql

db = pymysql.connect('localhost','root','',db = 'customers')

cursor = db.cursor()

class customer():
    _ids = count(0)

    def __init__(self,User,Password,Wallet):
        self.ID = next(self._ids)
        self.User = User
        self.Password = Password
        self.wallet = Wallet

        insert = '''INSERT INTO customer(ID,Username,Password,Wallet) values ('%s','%s','%s','%s');'''%(self.ID,self.User,self.Password,self.wallet)

        conn = pymysql.connect('localhost','root','',db = 'customers')
        inscursor = conn.cursor()
        conn.commit()

Python似乎也沒有引發任何錯誤或異常。

J

您必須在提交之前執行。

with connection.cursor() as cursor:
    # Create a new record
    sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
    cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()

https://github.com/PyMySQL/PyMySQL#example

答案很簡單。 您沒有運行正在構建的查詢。

您應該添加該行

inscursor.execute(insert) 

conn.commit()之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM