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
What Are Some Good Python Orm Solutions
Remove Xticks in a Matplotlib Plot
How to Convert SQL Query Result to Pandas Data Structure
Sqlalchemy: Print the Actual Query
Size Legend for Plotly Bubble Map/Chart
How to Use Rpy2 to Save a Pandas Dataframe to an .Rdata File
Why Is Variable1 += Variable2 Much Faster Than Variable1 = Variable1 + Variable2
Beautifulsoup Not Grabbing Dynamic Content
R, Python: Install Packages on Rpy2
Fama MACbeth Regression in Python (Pandas or Statsmodels)
Plotting of 2D Data:Heatmap with Different Colormaps
How to Use Tailwindcss with Django
Why Is "If Not Someobj:" Better Than "If Someobj == None:" in Python
How to Refer to Relative Paths of Resources When Working with a Code Repository
Pick N Distinct Items at Random from Sequence of Unknown Length, in Only One Iteration
How to Put Parameterized SQL Query into Variable and Then Execute in Python