Values in mysql with python - python

I'm trying to do a little program for print an user that it was selected in a list. But I don't know how to mix a variable with a query. I tried this but I get an error:
def datos(name):
conn = MySQLConnection(host=DB_HOST,user=DB_USER,password=DB_PASS,database=DB_NAME)
cursor = conn.cursor()
cursor.execute("SELECT nombre FROM clientes WHERE nombre = %s", name)
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

Related

How can you insert data to sql realtion using python dictionary(data-type),

lis = {'mno': 1, 200000:2}
query = """
insert into one values(%s, %s);
"""
cus.execute(query, lis)
a = cus.fetchall()
Where is error in this code?
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, %s)' at line 1

A problem with a Python function to modify the information in the database

I wrote a Python function to modify the data and information entered in the database
def update(self):
con = pymysql.connect(
host = 'localhost',
user = 'root',
password = '',
database = 'employ')
cur = con.cursor()
cur.execute("update employees set family_members=%s, social_status=%s, gender=%s, date_birth=%s, id_number=%s, mail=%s, name=%s where id=%s",(
self.family_members_var.get(),
self.social_status_var.get(),
self.gender_var.get(),
self.date_birth_var.get(),
self.id_number_var.get(),
self.mail_var.get(),
self.name_var.get(),
self.id_var.get()
))
con.commit()
self.fetch_all()
self.clear()
con.close()
I got the following error:
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")

Python MySQL, get special column names

I am using Python to fetch MySQL process.
The codes are shown below:
import mysql.connector
mydb = mysql.connector.connect(
host='192.168.95.9', user='root',
passwd='qwe123', database='information_schema')
cursor = mydb.cursor()
sql = "SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist
WHERE `TIME`>100 AND `COMMAND` = 'Sleep' AND `DB` IN ('api','monitor', 'workflow');"
cursor.execute(sql)
cursor.fetchall()
The sql shown above can get results but in python, it returns nothing. When I get rid of the where condition, it returns values as expected. I also tried
sql = "SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE \`TIME\`>100 AND \`COMMAND\` = 'Sleep' AND \`DB\` IN ('api','monitor', 'workflow');"
But it returns 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 '\`TIME\`>100 lim
How to address this issue?

Python-MySQL prepared statement error while selecting where a value IS NULL

I want to select rows where a value is null but I get this error on executing using Python-MySQL prepared statement.
mysql.connector.errors.InterfaceError: 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 '?' at line 1
import mysql.connector
connection = mysql.connector.connect(database="XXXXX", user="XXXXX", password="XXXXX", host="XXXXX")
cursor = connection.cursor(prepared=True)
parameters = ["Ahmed", None]
statement = "SELECT * FROM Persons WHERE name_first = ? AND start_date IS ?"
cursor.execute(statement, tuple(parameters))

1064 error while operating MySQL database with Python

I want to write a program in Python to control MySQL database, but the following error occurs
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; chec
k the manual that corresponds to your MySQL server version for the right syntax
to use near 'Code,type, number,Whether )VALUES ('cl8k-k520-fg45-d4sa-sd9k','n' at line 1")
Codeļ¼š
import pymysql
db = pymysql.connect("localhost", "root", "root", "test")
cursor = db.cursor()
sql = "INSERT INTO BOOKS(Activation Code, \
type, number,Whether ) \
VALUES (%s,%s,%s,%s)"
data = [("cl8k-k520-fg45-d4sa-sd9k",'n','10','n'),
("jhg6-58jh-gh8o-8uik-7jk8",'c','20','n'),
("x5hg-gf5h-4hj5-g4h5-fh5t",'y','999','n'),
("fg8j-h584-fd4g-fg4d-fgg4",'n','1','y'),
("4jk4-5kl4-4klk-4lji-4ghj",'e','6','n'),
]
cursor.executemany(sql,data)
db.close()
This is my first time to ask questions on this platform. I hope to get answers
enclose Activation Code as 'Activation Code'

Categories