2017-10-10 58 views
0

我是MYSQL的新手,我正面臨MYSQL的一個非常簡單的問題。我創建了一個包含學校表的數據庫,該表包含sschool的名稱,聯繫人,地址,ID(主鍵)。我需要根據用戶選擇的id(調用這個變量school_id)刪除一條記錄,那麼如何使用python在mysql語句中編寫這個記錄呢?我已經試過,但我知道這是錯的 - >python Mysql刪除

print "Delete school" 
school_id = int(input('Please, enter an ID: ')) 
cursor.execute("DELETE FROM `schools` WHERE id = %s", (school_id,)) 

其無deleting.whats

+1

你犯? – Mureinik

回答

1

你犯了刪除錯誤?

cursor.commit()

0

正如已經joof說,大部分可能是你沒有COMMITED的變化。 如果它沒有幫助,我會先嚐試在mysql中執行這個語句,看看它是否有效,然後檢查你是否有正確的連接到你的分貝。

鏈接到在Python中使用sql。 https://docs.python.org/3.6/library/sqlite3.html

+0

我已連接數據庫 – mutegeki

0

除非你承諾,它不會被執行:

cursor.commit()

有樂趣。

+0

我已經使用db.commit() – mutegeki

0
print "Delete school" 
school_id = int(input('Please, enter an ID: ')) 
cursor.execute("DELETE FROM `schools` WHERE id = %s", (school_id,)) 
+0

如果我做db.commit() – mutegeki

0

MySQL documentationcommit

每一個爲使用事務性存儲引擎的表修改數據的事務後調用這個方法是非常重要的。

你不必從字面上每一筆交易後,使用它 - 這是不夠的,如果你一旦把它所有execute被稱爲後:

def a(): 
    cursor.execute('insert ...') # cursor object 
    cursor.execute('update ...') 
    connection.commit() # connection object 

def b(): 
    cursor.execute('delete ...') 
    connection.commit()