I am new to python mysql connector and I am trying to get results for a query that will find the user_ids with p_id = 0 will go to product table find how many products are available in that city
import mysql.connector
con = mysql.connector.connect(user='user', password = 'pass', host = 'blah.com')
cursor1 = con.cursor(buffered = True)
query = ("SELECT l.user_id, count(u.prod_id)"
"FROM db1.users as l INNER JOIN db2.product as u "
"ON l.u_city = u.p_city"
"WHERE l.p_id =0 GROUP BY l.user_id limit 10;" )
cursor1.execute(query)
the query is getting executed mysql but from python mysql connector i am getting the following error
C:\Python27\python.exe C:/Python27/Lib/site-packages/mysql/connector/update_campus_user_profile_suset.py
Traceback (most recent call last):
File "C:/Python27/Lib/site-packages/mysql/connector/update_campus_user_profile_suset.py", line 12, in <module>
cursor1.execute(camp_zip)
File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 491, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 683, in cmd_query
statement))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 601, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'l.campus_id <2 GROUP BY l.user_id' at line 1
Process finished with exit code 1
query ='''SELECT l.user_id, count(u.prod_id) FROM db1.users as l INNER JOIN db2.product as u ON l.u_city = u.p_city WHERE l.p_id =0 GROUP BY l.user_id limit 10'''
always it is better to intialize ur query to a variable with ('''stmt''') and execute
there is no need of using ;
It looks like you're just missing some spaces at the end of each line. Try:
query = ("SELECT l.user_id, count(u.prod_id) "
"FROM db1.users as l INNER JOIN db2.product as u "
"ON l.u_city = u.p_city "
"WHERE l.p_id = 0 GROUP BY l.user_id limit 10;" )
Sometimes it's more natural to use a multi-line string:
query = ("""
SELECT l.user_id, count(u.prod_id)
FROM db1.users as l INNER JOIN db2.product as u
ON l.u_city = u.p_city
WHERE l.p_id = 0 GROUP BY l.user_id limit 10
""")
Related
I want to change the bot database from SQLite to MySQL (pymysql) and ran into a problem.
I have a function for generating sql queries, but for some reason it stopped working:
def update_format_with_args(sql, parameters: dict):
values = ", ".join([
f"{item} = ?" for item in parameters
])
sql = sql.replace("XXX", values)
return sql, tuple(parameters.values())
def update_settingsx(**kwargs):
with connection.cursor() as db:
sql = f"UPDATE main_settings SET XXX "
sql, parameters = update_format_with_args(sql, kwargs)
db.execute(sql, parameters)
db.commit()
Error:
update_settingsx(profit_buy=now_unix)
File "/root/rent/bot_id/utils/db_api/sqlite.py", line 154, in update_settingsx
db.execute(sql, parameters)
File "/usr/local/lib/python3.7/dist-packages/pymysql/cursors.py", line 168, in execute
query = self.mogrify(query, args)
File "/usr/local/lib/python3.7/dist-packages/pymysql/cursors.py", line 147, in mogrify
query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting
Not every database connector supports the ? substitution scheme. Pymysql uses %s instead.
Another alternative would be to use named substition:
def update_format_with_args(sql, parameters: dict):
values = ", ".join([
f"{item} = %({item})s" for item in parameters
])
sql = sql.replace("XXX", values)
return sql, parameters
I am new to coding and databases, I can not get the query to work if I write it long hand but I have a lot to carry out and want it in a function but cannot get it to work, it returns a parameters error
import mysql.connector
def connection_check_1(query, value):
mydb = mysql.connector.connect(
host="******",
user="*****",
passwd="*****",
database="****"
)
mycursor = mydb.cursor()
mycursor.execute(query, (value))
myresult = mycursor.fetchall()
mydb.close()
return myresult
value = "sheep"
query = 'select inlicence from licence where animal = %s'
myresult = connection_check_1(query, value)
print(myresult)
Here is the SQL table I have
create table licence
(
animal varchar (20) primary key,
inlicence int (1)
);
This is the error I get
Traceback (most recent call last):
File "*******************", line 20, in
myresult = connection_check_1(query, value)
File "********************", line 13, in connection_check_1
mycursor.execute(query, (value))
File "********************************************88", line 246, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/kev/PycharmProjects/test bed/venv/lib/python3.5/site-packages/mysql/connector/connection_cext.py", line 535, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
I have tried changing the way the query is written, changing it to fetchall().
Wrapping a value with () doesn't turn it in to a tuple. You probably meant to add a comma there:
mycursor.execute(query, (value,))
# Creates a one-element tuple-^
UPDATE
conn = pymysql.connect(host='localhost',user='user',password='password',db='mydb',charset='utf8')
cur = conn.cursor(pymysql.cursors.DictCursor)
try:
query = 'select * from test where id = 1;abcd'
cur.execute(query)
res = cur.fetchone()
print(res)
except Exception as e:
print(e)
try:
query = 'select * from test where id = 2;'
cur.execute(query)
res = cur.fetchone()
print(res)
except Exception as e:
print(e)
The code makes the result as below:
{'name': '', 'score': 1.1, 'id': 1}
(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 'abcd' at line 1")
The first line {'name': '', 'score': 1.1, 'id': 1} comes from the first query before ; in the first try...except.... And then the error message comes from the second try...except... but it comes from the sql query abcd, which means that the cur.execute(query) in the second try...except... produces the exception because of abcd and select * from test where id = 2; isn't executed.
So how can I ignore the abcd and make the second query execute as expected?
ORIGINAL QUESTION
I'm building a web server with python. I use MySQL as the database and the pymysql as the interface of the database.
Now I get an issue:
When some sql query makes an error because of a ;, the program will be blocked even if I've used try...except.... Here is an example:
import pymysql
import pymysql.cursors
conn = pymysql.connect(host='localhost',user='user',password='password',db='mydb',charset='utf8')
cur = conn.cursor(pymysql.cursors.DictCursor)
try:
query = 'select * from test where id = 1;abcd' <--- exception!
cur.execute(query)
res = cur.fetchone()
print(res)
except Exception as e:
print(e)
query = 'select * from test where id = 2;'
cur.execute(query)
res = cur.fetchone()
print(res)
As you see, the first query is illegal because of the part ;abcd. So it will produce an error. However, the try...except... can not catch this exception because I find that the print(e) isn't executed. Here is the messages that I get:
{'score': 1.1, 'name': '', 'id': 1}
Traceback (most recent call last):
File "db_mysql.py", line 23, in <module>
cur.execute(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 161, in execute
while self.nextset():
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 103, in nextset
return self._nextset(False)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 98, in _nextset
conn.next_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 860, in next_result
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.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 'abcd' at line 1")
I can't understand why the error can not be caught by try...except.... Moreover, it seems that the query which produces the error is abcd, instead of select * from test where id = 1;abcd. If I'm right, I think the ; make this query into two queries.
Also, if I remove the ;, which means that the first query becomes query = 'select * from test where id = 1abcd', the try...except... could catch the error so the second query can be executed as expected.
So my question is: why the try...except... can't catch the error? What could I do to handle all of sql errors so that the program won't be blocked?
So my question is: why the try...except... can't catch the error? What
could I do to handle all of sql errors so that the program won't be
blocked?
Maybe with the good exception name?
try:
…
except pymysql.err.ProgrammingError as except_detail:
print("pymysql.err.ProgrammingError: «{}»".format(except_detail))
See all exceptions type in help(pymysql.err) or on PyMySQL's Github
I am using mysql-connector/python for the first time and I am facing an error.
I want to fetch the email addresses with net from a mysql table called blah.
cursor = cnx.cursor()
p = "net"
query = ("SELECT * FROM blah WHERE email LIKE %s limit 10", ("%" + p + "%",))
cursor.execute(query,(p,))
for row in cursor:
print row
cursor.close()
cnx.close()
and I am getting this error
Traceback (most recent call last):
File "<input>", line 9, in <module>
File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 491, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 683, in cmd_query
statement))
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 601, in _handle_result
raise errors.get_exception(packet)
ProgrammingError: 1064 (42000): 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 '"SELECT * FROM blah WHERE email LIKE %s limit 10", ("%" + p +"%")' at line 1
Can anyone suggest why is the code not working? It is working on sql -
select * from blah where email LIKE "%net" limit 10
Try this:
query = "SELECT * FROM blah WHERE email LIKE CONCAT('%', %s, '%') limit 10"
cursor.execute(query,(p,))
You need to leave %s in the string so that MySQL Connector can substitute the execute parameter for it. You were setting query to a list, not a string.
the first argument to cursor.execute should be a string it looks like you have munged assigning the string and calling the function. Probably should look something like:
query = "SELECT * FROM blah WHERE email LIKE %s limit 10"
cursor.execute(query,("%" + p + "%",))
New to SO and fairly new to coding, so doing my best to follow the appropriate protocols.
In my python script, I'm creating a new table and populating column names from a list, named 'dups'.
dups = ['Id', 'Name', 'Price', 'Rating']
I'm inputting this list as columns for the new table, called "SuperTable", via a for loop. See code below:
with new_db:
cur = new_db.cursor()
cur.execute("DROP TABLE IF EXISTS SuperTable")
for i in dups:
if i == dups[0]:
new_col = i.replace("'","")
cur.execute("CREATE TABLE SuperTable(%s)" % (new_col))
else:
cur.execute("ALTER TABLE SuperTable ADD COLUMN %s" % i)
I've looked around a lot and can't seem to identify what I'm doing wrong. This approach worked with Sqlite but I keep getting this same error for MySQLdb:
Traceback (most recent call last):
File "MySQL_SuperTable.py", line 125, in <module>
cur.execute("CREATE TABLE Super(%s)" % (new_col))
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_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 ')' at line 1")
Thanks to eggyal! He pointed out that MySQL columns require a datatype. This is what the code looks like now (I created a list of tuples to input the datatypes + column names via a for loop):
with new_db:
cur = new_db.cursor()
cur.execute("DROP TABLE IF EXISTS SuperTable")
for i in col_namestypes:
if i == col_namestypes[0]:
cur.execute("CREATE TABLE SuperTable(%s %s)" % (i))
else:
cur.execute("ALTER TABLE SuperTable ADD COLUMN %s %s" % i)
for i in new_table:
count = len(i)
question_marks = []
while a < count:
question_marks.append('%s')
a += 1
quests = ','.join(question_marks)
cur.executemany("INSERT INTO SuperTable VALUES(%s)" % quests, new_table)