我希望有人能幫助我嗎?我正在嘗試使用Python 2.7和MySQLdb庫來填充MySQL數據庫。我已經寫了下面的示例腳本,但無論我嘗試什麼,我都無法使其工作。MySQLdb/Python/LocalHost - 查詢運行時沒有錯誤,但沒有結果
代碼運行時沒有錯誤(當我運行代碼時它會打印'Done',但就是這樣),當我直接在phpMyAdmin中嘗試查詢時,它工作正常,出於某種原因,我無法使其工作從Python內部。
我錯過了什麼嗎?有什麼我需要在phpMyAdmin中激活才能使其工作?我在下午的大部分時間裏一直在尋找答案,當其他人遇到類似問題時,雖然他們傾向於獲取錯誤消息(我不知道)。
import MySQLdb
# Define the database access details
DB_HOST = "localhost"
DB_USER = "username"
DB_PASSWORD = "secretpassword"
DB_NAME = "TestDatabase"
# Establish the connection to the database
db = MySQLdb.connect(
host=DB_HOST,
user=DB_USER,
passwd=DB_PASSWORD,
db=DB_NAME
)
cur = db.cursor()
Query = "INSERT INTO SearchResults (`TimeStamp`, `SearchTerm`, `SearchResult`) VALUES ('2013-06-22 17:28:09', 'TestSearchTerm', 'Test Search Result');"
cur.execute(Query)
print"Done"
這是我的第一次經歷,因此如果我忽略了一些明顯的東西,請原諒我!任何幫助,將不勝感激!
嘗試在'cur.execute(Query)'後面調用'db.commit()'。 – unutbu
複製http://stackoverflow.com/questions/384228/database-does-not-update-automatically-with-mysql-and-python –
'db.commit()'工作,感謝您的幫助! – AdamDynamic