开发者

python mysql problem using string as query

开发者 https://www.devze.com 2023-03-28 07:36 出处:网络
So I\'m at the point in my program where I have a string containing the query I want to use to insert a row into a database:

So I'm at the point in my program where I have a string containing the query I want to use to insert a row into a database:

query = '"INSERT INTO new_test (test_name, IP, test_run_date, results_query_time, run_time) VALUES (%s, %s, %s, %s, %s)", ("new_test", "192.168.17.194", "143917160811", "1开发者_StackOverflow2.4847829342", "46.1268320084")'

However, when I execute the command:

cursor.execute(query)

I get this error

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 \'"INSERT INTO new_test (test_name, IP, test_run_date, results_query_time, run_tim\' at line 1')

I tried a few other combinations of quotes but I can't for the life of me figure out what I am doing wrong. Any Ideas? Thanks!


You have an extra " at the beginning of the query. That will definitely break it. It looks like you wanted:

# notice the extra ' around the %s
query = """INSERT INTO new_test 
              (test_name, IP, test_run_date, results_query_time, run_time) 
           VALUES ('%s', '%s', '%s', '%s', '%s')""" % \
           ("new_test", "192.168.17.194", "143917160811", 
           "12.4847829342", "46.1268320084")
0

精彩评论

暂无评论...
验证码 换一张
取 消