我在从python向MySQL数据库插入记录时遇到了困难。这就是我要做的。在
def testMain2():
conn = MySQLdb.connect(charset='utf8', host="localhost", user="root", passwd="root", db="epf")
cursor = conn.cursor()
tableName = "test_table"
columnsDef = "(export_date BIGINT, storefront_id INT, genre_id INT, album_id INT, album_rank INT)"
exStr = """CREATE TABLE %s %s""" % (tableName, columnsDef)
cursor.execute(exStr)
#Escape the record
values = ["1305104402172", "12", "34", "56", "78"]
values = [conn.literal(aField) for aField in values]
stringList = "(%s)" % (", ".join(values))
columns = "(export_date, storefront_id, genre_id, album_id, album_rank)"
insertStmt = """INSERT INTO %s %s VALUES %s""" % (tableName, columns, stringList)
cursor.execute(insertStmt)
cursor.close()
conn.close()
创建了表,但表中没有任何内容。我可以使用相同的凭据在终端中成功运行INSERT
语句。在
对我可能做错了什么有什么建议吗?在
您尚未提交交易。在
(MySQLdb库在连接到MySQL时将
autocommit
设置为False。这意味着您需要手动调用commit,否则您的更改将永远不会进入数据库。)相关问题 更多 >
编程相关推荐