尽管 Python 可以将数据插入 table,但 MySQL 中的空 table
Empty table in MySQL even though Python can insert data into table
我是 mySQL 和 Python 的新手。
我有代码可以将 Python 中的数据插入 mySQL、
conn = MySQLdb.connect(host="localhost", user="root", passwd="kokoblack", db="mydb")
for i in range(0,len(allnames)):
try:
query = "INSERT INTO resumes (applicant, jobtitle, lastworkdate, lastupdate, url) values ("
query = query + "'"+allnames[i]+"'," +"'"+alltitles[i]+"',"+ "'"+alldates[i]+"'," + "'"+allupdates[i]+"'," + "'"+alllinks[i]+"')"
x = conn.cursor()
x.execute(query)
row = x.fetchall()
except:
print "error"
它似乎工作正常,因为 "error" 从未出现过。相反,我的 Python shell 中出现了许多行“1L”。但是,当我转到 MySQL 时,"mydb" 中的 "resumes" table 仍然完全是空的。
我不知道哪里出了问题,可能是我在查看 MySQL 中的 table 时未正确连接到 MySQL 的服务器?请帮忙
(我只用import MySQLdb,够了吗?)
使用 commit 提交您所做的更改
MySQLdb 默认关闭自动提交,一开始可能会让人感到困惑
你可以这样提交
conn.commit()
或
conn.autocommit(True) Right after the connection is created with the DB
我是 mySQL 和 Python 的新手。
我有代码可以将 Python 中的数据插入 mySQL、
conn = MySQLdb.connect(host="localhost", user="root", passwd="kokoblack", db="mydb")
for i in range(0,len(allnames)):
try:
query = "INSERT INTO resumes (applicant, jobtitle, lastworkdate, lastupdate, url) values ("
query = query + "'"+allnames[i]+"'," +"'"+alltitles[i]+"',"+ "'"+alldates[i]+"'," + "'"+allupdates[i]+"'," + "'"+alllinks[i]+"')"
x = conn.cursor()
x.execute(query)
row = x.fetchall()
except:
print "error"
它似乎工作正常,因为 "error" 从未出现过。相反,我的 Python shell 中出现了许多行“1L”。但是,当我转到 MySQL 时,"mydb" 中的 "resumes" table 仍然完全是空的。
我不知道哪里出了问题,可能是我在查看 MySQL 中的 table 时未正确连接到 MySQL 的服务器?请帮忙
(我只用import MySQLdb,够了吗?)
使用 commit 提交您所做的更改
MySQLdb 默认关闭自动提交,一开始可能会让人感到困惑
你可以这样提交
conn.commit()
或
conn.autocommit(True) Right after the connection is created with the DB