Python MySQL Connector Database Query with %S Fails

Python MySQL Connector database query with %s fails

Change your funcursor.execute(query, uName) call to:

funcursor.execute(query, (uName, ))

The second argument in execute takes a list/tuple of strings, not a string. The above call creates the tuple before passing in the string to execute, so no error is thrown.

The reason why execute takes a list/tuple of strings is because it does not know beforehand how many strings it needs in order to satisfy your query.

python-mysql-connector throws error while executing command

Database names should not be quoted. Just do

cursor.execute( "use test;" )

Alternatively, specify the database name in the connect call.

MySQL connector query

Table names and field names cannot use substitution. Assuming you're on Python 3, you can use an 'f' string:

query = f"""INSERT IGNORE INTO {post_table_name} (id, title, body_text, username,time_created,num_of_comments, subreddit, full_link, upvote_ratio) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""

mycursor.execute(query, tuple1)

Python MySQL syntax error while executing creatable ... like

String parameters in a sql query should be in quoted form:

.... '%s' like '%s'.....

UPDATE

In your case, you can try this:

("""create table '%s' like '%s'""", (table_name, table))

or another older solution would be :

("create table " + table_name  + " like " + table + " ")

Python MySQL Connector occasionally fails to insert/update

I found the error:

I was using a global variable (timestamp_packaging_start) in multiple queries as the primary key. As no one can tell when a function in a different thread is executed, sometimes the variable got overwritten by the next entry without executing the query in the first place. That caused my program to update the timestamps for ANOTHER and WRONG primary key. Thanks for your efforts guys.



Related Topics



Leave a reply



Submit