gpt4 book ai didi

python - 通过python更新Mysql数据库出错

转载 作者:行者123 更新时间:2023-11-29 00:07:03 24 4
gpt4 key购买 nike

I am trying to add all words of a text file into a column such that one row has one word. my code is as :

 import MySQLdb
conn = MySQLdb.connect (host = "localhost",user = "root", db = "pcorpora")
c = conn.cursor()
file = open('C:\Users\Admin\Desktop\english.txt', 'r')
words = list(file.read())
i=0
for value in words:
c.execute("""INSERT INTO tenglish (`english words`) VALUES (%s)""" % (words[i]) i=i+1)`

代码运行没有错误,但表仍然是空的。

最佳答案

你应该使用commit

c.execute("""INSERT INTO tenglish (`english words`) VALUES (%s)""" % (value))
con.commit()

This method sends a COMMIT statement to the MySQL server, committing the current transaction. Since by default Connector/Python does not autocommit, it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.

关于python - 通过python更新Mysql数据库出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com