0

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

蟒蛇代码:

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)
4

1 回答 1

2

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

      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-08-23T16:02:08.530 回答