从Python插入MySQL数据库时出现问题

2024-09-30 01:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从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语句。在

对我可能做错了什么有什么建议吗?在


Tags: idalbumdateexportrootconncursorint

热门问题