jcaine04
jcaine04

Reputation: 427

MySQL INSERT fails in Python but works fine in MySQL Workbench

Here is a query I have that runs fine in MySQL workbench with the included sample values and works fine if I manually plug in the values in the code, but fails when I use the values as parameters. Any ideas?

Python Code:

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

Error message:

['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 Table Columns:

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

MySQL Code that runs fine in Workbench:

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)

Upvotes: 0

Views: 821

Answers (1)

Jim
Jim

Reputation: 22656

You're not passing data to the execute call. Note the closing brace in your example.

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

Upvotes: 2

Related Questions