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 + "%",))
Related
So I'm trying to build a student-management system and I've built a table in mysql and connected the database to python using pymysql. Now I want to have a function (in python) that accepts data from the user and adds it to the table.
Here's what the table looks like:
Here's the code for the table:
create table student_management(
Admisson_Number int(5) primary key,
Student_Name varchar(100),
Student_Class varchar(7),
Section varchar(2),
Average_Grade varchar(2)
);
select * from student_management;
insert into student_management
values(1,'Harry Potter','5','F','A');
insert into student_management
values(2,'Dani Clayton','11','B','A1');
insert into student_management
values(3,'Fidel Piker','9','L','A');
insert into student_management
values(4,'Aubrey Graham','12',' J','B2');
insert into student_management
values(5,'Abel Makkonen Tesfaye','7','K','B1');
Now the problem arises. Here's the function I wrote in python to accept data and add it to the table.
import pymysql
stdconn = pymysql.connect(host='localhost', user='root', password="password", db='db1')
MyCur = stdconn.cursor()
def addstudent():
admno = input("Enter Student's Admission Number:")
stdname = input("Enter Student's Name :")
stdclass = input("Enter Student's Class:")
section = input("Enter Section:")
avggrade = input ("Enter Student's Avg Grade:")
data = (admno, stdname, stdclass, section, avggrade)
sql = ("insert into student_management values (%s, %s, %s, %s, %s")
MyCur = stdconn.cursor()
MyCur.execute(sql, data)
stdconn.commit()
print("Student Has Been Added")
addstudent()
Running the code results in the following error :
Enter Student's Admission Number:6
Enter Student's Name :abc
Enter Student's Class:9
Enter Section:J
Enter Student's Avg Grade:A
Traceback (most recent call last):
File "D:/practice.py", line 47, in menu
addstudent()
File "D:/practice.py", line 113, in addstudent
MyCur.execute(sql, data)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
packet.raise_for_error()
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\super\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymysql\err.py", line 143, 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 '' at line 1")
Process finished with exit code 1
It says I have an error in my "SQL syntax" but I cant seem to find it. How should I go about fixing this issue? thanks in advance.
you're missing a bracket as mentioned in the other answer
I think your line :
sql = ("insert into student_management values (%s, %s, %s, %s, %s")
should be
sql = ("insert into student_management values (%s, %s, %s, %s, %s)")
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 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
""")