import mysql.connector
from mysql.connector import Error
def query_with_fetchone(tit):
try:
conn = mysql.connector.connect(host='localhost',
database='python_mysql',
user='root',
password='')
cursor = conn.cursor()
query = "INSERT INTO sample(title) VALUES(%s)"
args = (tit)
cursor.execute(query,args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
query_with_fetchone('Vrajesh')
I am getting following error:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s)' at line 1
Please Guide I am new Python
asked Dec 2, 2017 at 10:40
Vrajesh Doshi
7642 gold badges9 silver badges31 bronze badges
-
2I have two guesses that may be helpful: (1) you should put an ending semicolon at the end of the query, and (2) try to change args into a list, not a tuple.Alessandro– Alessandro2017年12月02日 10:44:46 +00:00Commented Dec 2, 2017 at 10:44
-
Your Second Guess WorkedVrajesh Doshi– Vrajesh Doshi2017年12月02日 10:52:48 +00:00Commented Dec 2, 2017 at 10:52
1 Answer 1
Your args is not a tuple, tuple with one element requires comma.
args = (tit,)
It may be the solution
answered Dec 2, 2017 at 10:47
Nestor Yanchuk
1,2869 silver badges10 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Vrajesh Doshi
Thank You. It was the expected solution
default