【问题标题】:MySQL INSERT fails in Python but works fine in MySQL WorkbenchMySQL INSERT 在 Python 中失败,但在 MySQL Workbench 中工作正常
【发布时间】:2013-08-26 18:01:17
【问题描述】:

这是我的一个查询,它在 MySQL 工作台中使用包含的示例值运行良好,如果我在代码中手动插入值,它工作正常,但当我使用这些值作为参数时会失败。有什么想法吗?

Python 代码:

print player
                cur.execute("""
                            INSERT INTO scoredata
                            (gameid, playerid, starter, pos, min, fgm, fga, tpm, tpa, ftm, fta, oreb, reb, ast, stl, blk, tos, pf, pts)
                            VALUES
                            (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
                            """), (player[0], 
                                   int(player[20]), 
                                   int(player[19]), 
                                   player[3], 
                                   int(player[4]), 
                                   int(player[5]), 
                                   int(player[6]), 
                                   int(player[7]), 
                                   int(player[8]), 
                                   int(player[9]), 
                                   int(player[10]), 
                                   int(player[11]), 
                                   int(player[12]), 
                                   int(player[13]), 
                                   int(player[14]), 
                                   int(player[15]), 
                                   int(player[16]), 
                                   int(player[17]), 
                                   int(player[18]) )
                db.commit()

错误信息:

['330060130', 103, 'Roy Devyn Marble', 'G-F', '28', '4', '9', '3', '6', '3', '3', '0', '2', '1', '0', '0', '0', '1', '14', 1, 1391]
Traceback (most recent call last):
  File "C:\Users\jcaine\workspace\BasketballStats\src\BasketballStats\basketballstats.py", line 350, in <module>
    insert_game_data('20130106', '20130106')
  File "C:\Users\jcaine\workspace\BasketballStats\src\BasketballStats\basketballstats.py", line 284, in insert_game_data
    """), (player[0], int(player[20]), int(player[19]), player[3], int(player[4]), int(player[5]), int(player[6]), int(player[7]), int(player[8]), int(player[9]), int(player[10]), int(player[11]), int(player[12]), int(player[13]), int(player[14]), int(player[15]), int(player[16]), int(player[17]), int(player[18]) )
  File "c:\users\jcaine\appdata\local\temp\easy_install-7_fysp\MySQL_python-1.2.3-py2.7-win32.egg.tmp\MySQLdb\cursors.py", line 174, in execute
  File "c:\users\jcaine\appdata\local\temp\easy_install-7_fysp\MySQL_python-1.2.3-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' at line 4")

MySQL scoredata 表列:

gameid  varchar
playerid    int
starter int
pos varchar
min int
fgm int
fga int
tpm int
tpa int
ftm int
fta int
oreb    int
reb int
ast int
stl int
blk int
tos int
pf  int
pts int

在 Workbench 中运行良好的 MySQL 代码:

INSERT INTO scoredata (gameid, playerid, starter, pos, min, fgm, fga, tpm, 
    tpa, ftm, fta, oreb, reb, ast, stl, blk, tos, pf, pts) 
VALUES ('3300601300', 1391, 1, 'G-F', 28, 4, 9, 3, 6, 3, 3, 0, 2, 1, 0, 0, 0, 1, 14)

【问题讨论】:

    标签: python mysql sql syntax insert


    【解决方案1】:

    您没有将数据传递给执行调用。请注意示例中的右大括号。

          cur.execute("""
                        INSERT INTO scoredata
                        (gameid, playerid, starter, pos, min, fgm, fga, tpm, tpa, ftm, fta, oreb, reb, ast, stl, blk, tos, pf, pts)
                        VALUES
                        (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
                        """)//*Remove me*
                            , (player[0], 
                               int(player[20]), 
                               int(player[19]), 
                               player[3], 
                               int(player[4]), 
                               int(player[5]), 
                               int(player[6]), 
                               int(player[7]), 
                               int(player[8]), 
                               int(player[9]), 
                               int(player[10]), 
                               int(player[11]), 
                               int(player[12]), 
                               int(player[13]), 
                               int(player[14]), 
                               int(player[15]), 
                               int(player[16]), 
                               int(player[17]), 
                               int(player[18]) )
            db.commit()
    

    【讨论】:

    • 谢谢!就是这样……真是个愚蠢的错误。
    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2012-04-07
    • 2015-02-17
    • 2013-10-29
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多