Python tkinter--ProgrammingError: 1064 (42000) - python

Following code is a function design for update data in mysql database.I get issue with following error message:
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 '#gmail.com,gender=Female,
def update_data(self):
con = mysql.connector.connect(
host="localhost",
user="root",
password="123456",
database="stm",
)
cur = con.cursor()
cur.execute(
"""
update
students
set
name=%s,
email=%s,
gender=%s,
contact=%s,
dob=%s,
address=%s,
where
roll_no=%s;
""" % (
self.name_var.get(),
self.email_var.get(),
self.gender_var.get(),
self.contact_var.get(),
self.dob_var.get(),
self.txt_Address.get('1.0', END),
self.Roll_No_var.get(),
), )
con.commit()
con.close()
self.fetch_data()
self.clear()

Related

i am getting mulltilin statement error in python can anyone suggest me

import mysql.connector
cnx=mysql.connector.connect(user='root',password='Noot#123',
host='127.0.0.1',
database='grocery_store')
cursor = cnx.cursor()
query=("SELECT products.product_id, products.name, products.uom_id, products.price_per_unit, uom.uom_name"
'FROM products inner join uom on products.uom_id=uom.uom_id')
cursor.execute(query)
for(product_id,name,uom_id,price_per_unit,uom_name) in cursor:
print(product_id,name,uom_id,price_per_unit,uom_name)
cnx.close()
ERROR i am getting: 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 'inner join uom on products.uom_id=uom.uom_id' at line 1
you need to add a space before FROM
import mysql.connector
cnx=mysql.connector.connect(user='root',password='dc2019DB',
host='127.0.0.1',
database='testdb')
cursor = cnx.cursor()
query=("SELECT products.product_id, products.name, products.uom_id, products.price_per_unit, uom.uom_name"
' FROM products inner join uom on products.uom_id=uom.uom_id')
cursor.execute(query)
for(product_id,name,uom_id,price_per_unit,uom_name) in cursor:
print(product_id,name,uom_id,price_per_unit,uom_name)
cnx.close()

Correct syntax to use near '%s' error while selecting data using Python from MySQL database

Running this code
res = cursor.execute("SELECT `password` FROM `players` WHERE `username` = %s", usern)
I get this error:
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 '%s' at line 1
The parametrized queries expect a tuple as an argument:
query = """SELECT `password` FROM `players` WHERE `username` = %s"""
res = cursor.execute(query, (usern, ))

SQL truncate table where table name is defined in function argument or variable

I'm trying to pass variable or function argument to the execute method
import pymysql
tablename = 'test_table'
con = pymysql.connect(**...)
with con.cursor() as cur:
cur.execute("TRUNCATE TABLE %s", tablename)
con.commit()
con.close()
Following error:
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 ''test_table'' at line 1")
Expecting to see no errors and the test_table empty of rows.
SO is slacking lately...
I did this instead and it worked:
import pymysql
tablename = 'test_table'
con = pymysql.connect(**...)
with con.cursor() as cur:
cur.execute("TRUNCATE TABLE %s" % tablename)
con.commit()
con.close()
print('Done')

How to insert variable to mysql database with python? "You have an error in your SQL syntax"

I am trying to insert one variable to mysql database with python. But I have syntax error.. Can You help me please? :)
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
try:
connection = mysql.connector.connect(host='localhost',
database='Xiaomi_Temp',
user='user',
password='pass')
cursor = connection.cursor()
mySql_insert_query = """ INSERT INTO Temp_test (batt) VALUES (%d) """
recordTuple = (batt)
print(batt)
cursor.execute(mySql_insert_query, recordTuple)
connection.commit()
print("Record inserted successfully into table")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into table {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")
I have variable batt and it has integer value.
My output is:
Failed to insert record into table 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 '%d)' at line 1
MySQL connection is closed
As the whole query needs to be in a string format while execution of query so %s should be used instead of %d which can be done as follows,
mySql_insert_query = """ INSERT INTO Temp_test (batt) VALUES (%s) """
recordTuple = (batt)
print(batt)
cursor.execute(mySql_insert_query, recordTuple)

Insert into MySQL table with Python

I am trying to insert basic data into my MySQL table without any success yet I'm using basic tutorials as demos.
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 '%s)' at line 1
My MySQL
CREATE TABLE awesome (
id int NOT NULL AUTO_INCREMENT,
test varchar(50),
PRIMARY KEY (id)
);
My Python code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="pass",
database="db"
)
mycursor = mydb.cursor()
sql = "INSERT INTO awesome (test) VALUES (%s)"
val = ("Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
Yes the problem arises when there is only 1 argument in val tuple. Use
val=("Highway 21",)
Note the comma at the end.

Categories

Resources