MySQL中的空表,即使Python可以将数据插入表中

时间:2015-07-02 06:25:06

标签: python mysql insert database-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"

似乎工作正常,因为“错误”永远不会出现。相反,我的Python shell中出现了许多“1L”行。但是,当我转到MySQL时,“mydb”中的“恢复”表仍然是完全空的。

我不知道可能出现什么问题,当我在MySQL中查看表格时,我是否未正确连接到MySQL的服务器?请帮助。

(我只使用导入MySQLdb,就够了吗?)

1 个答案:

答案 0 :(得分:3)

使用commit提交您已完成的更改

MySQLdb默认情况下自动关闭,这可能会让人感到困惑

你可以像这样提交

conn.commit()

conn.autocommit(True) Right after the connection is created with the DB