Does anyone know where I am going wrong here? The syntax for the DB update looks correct to me. Also I am wondering if I need to close connection say to open and close a connection within each function. Lets say for example that each function performs a different type of DB command, one for insert, one for update and one for delete, just as a generic example.
Output:
[root#localhost student_program]# python modify_student.py
Connection successful!!
Enter the id of the student record you wish to modify: 21
Is this student personal information you want to modify - y or n: y
Enter the first name: Jake
Enter the last name: Mc Intyre
Enter the email address: jake#noemail.com
Enter the address: 300 Main Street, New York
Enter the DOB in YYYY-MM-DD: 1960-01-01
Traceback (most recent call last):
File "modify_student.py", line 38, in <module>
modify_student()
File "modify_student.py", line 29, in modify_student
cur.execute(sql, [firstname, lastname, email, address, DOB, student_id])
File "/usr/local/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/usr/local/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 893, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 1103, in _read_query_result
result.read()
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 1396, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 1059, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.6/site-packages/pymysql/connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.6/site-packages/pymysql/err.py", line 109, 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 MariaDB server version for the right syntax to use near '(firstname, lastname, email, address, DOB)VALUES ('Jake','Mc Intyre','jake#noema' at line 1")
My code:
import os,pymysql
db_root = '/var/lib/mysql/'
db_to_create = 'students'
db_to_use = 'students'
conn = pymysql.connect(host='localhost', user='root', passwd='dbadmin', cursorclass=pymysql.cursors.DictCursor)
print('Connection successful!!')
def modify_student():
student_id = input("Enter the id of the student record you wish to modify: ")
student_info = input("Is this student personal information you want to modify - y or n: ")
if student_info == 'y':
firstname = input("Enter the first name: ")
lastname = input("Enter the last name: ")
email = input("Enter the email address: ")
address = input("Enter the address: ")
DOB = input("Enter the DOB in YYYY-MM-DD: ")
cur = conn.cursor()
command = "use %s; " %db_to_use
cur.execute(command)
sql = 'UPDATE students_info SET (firstname, lastname, email, address, DOB)VALUES (%s,%s,%s,%s,%s) WHERE ID = (%s);'
cur.execute(sql, [firstname, lastname, email, address, DOB, student_id])
print(cur.execute)
conn.commit()
cur.close()
conn.close()
else:
print("else")
modify_student()
The syntax for update is:
UPDATE tablename SET name='%s', email='%s' WHERE id='%s'
You are trying to UPDATE like an INSERT. But UPDATE only supports setting each column name independently, Not with a column list.
Try:
sql = "UPDATE students_info SET firstname='%s', lastname='%s', email='%s', address='%s', DOB='%s' WHERE ID='%s'"
cur.execute(sql, [firstname, lastname, email, address, DOB, student_id])
See https://mariadb.com/kb/en/library/update/
Query statement is nor right. Try this-
sql = 'UPDATE students_info SET firstname="'+firstname+'", lastname=="'+lastname+'", email="'+email+'", address="'+address+'", DOB="'+address+'") Where id="'+student_id+'"'
Hope this helps.
Related
I don't get what's the problem here. I want to build a web scraper that scrapes amazon and takes the price and the name into a database. But for some reason, it tells me that the columns and values are not matching. I do have one additional column in my database called "timestamp" where I automatically put in the time, but that is handled by the database. I am using MariaDB. A friend said I can use the MySQL API for MariaDB as well.
P.S. preis = price, coming from Germany, switching between English and German sometimes, just in case anyone is wondering.
import requests, time, csv, pymysql
from bs4 import BeautifulSoup as bs
#URL = input("URL")
URL = "https://www.amazon.de/gp/product/B075FTXF15/ref=crt_ewc_img_bw_3?ie=UTF8&psc=1&smid=A24FLB4J0NZBNT"
def SOUPIT (tempURL):
URL = tempURL
page = requests.get(URL,headers={"User-Agent":"Defined"})
soup = bs(page.content, "html.parser")
raw_price = soup.find(id="priceblock_ourprice").get_text()
price = raw_price[:-2]
raw_name = soup.find(id="productTitle").get_text()
name = raw_name.strip()
for i in range(0,len(name)-1):
if name[i] == "(":
name = name[:i]
break
data = [name, price, time.strftime("%H:%M:%S"), time.strftime("%d.%m.%Y")]
return(data)
data = SOUPIT(URL)
while True:
data = SOUPIT(URL)
db = pymysql.connect("localhost", "root", "root", "test")
cursor = db.cursor()
if (data == None):
break
print("break")
else:
name = data[0]
preis = data[1]
sql = """INSERT INTO amazon_preise (Name, Preis) VALUES ('{}',{})""".format(name,preis)
cursor.execute(sql)
db.commit()
print("success")
print(data)
time.sleep(60)
error message:
Traceback (most recent call last):
File "amazonscraper_advanced.py", line 43, in <module>
cursor.execute(sql)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1136, "Column count doesn't match value count at row 1")
The problem is caused, at least partially, by a using string formatting to insert values into an SQL statement.
Here is the scraped data:
>>> data = ['Sweatshirt Alien VS. Predator Z100088', '32,99', '14:08:43', '08.09.2019']
>>> name, preis, *_ = data
Let's create the SQL statement
>>> sql = """INSERT INTO amazon_preise (Name, Preis) VALUES ('{}',{})""".format(name,preis)
And display it:
>>> sql
"INSERT INTO amazon_preise (Name, Preis) VALUES ('Sweatshirt Alien VS. Predator Z100088',32,99)"
Observe that the VALUES clause contains three comma-separated values; this is because the web page displays currency in the German style, that is with commas separating the cents from the euros. When interpolated into the SQL statement
preis becomes two values instead of one.
The right way to fix this would be to convert preis from a string to a float or decimal, and use parameter substitution instead of string formatting to interpolate the values..
>>> fpreis = float(preis.replace(',', '.'))
>>> sql = """INSERT INTO amazon_preise (Name, Preis) VALUES (%s, %s)"""
>>> cursor.execute(sql, (name, fpreis))
This is my Script:
import mysql.connector
def loginsystem():
db = mysql.connector.connect(host="127.0.0.1",
user="root",
passwd="",
db="dbpython")
cursor = db.cursor()
loop = 'true'
while (loop == 'true'):
username = str(input("Username : "))
password = str(input("Password : "))
if (cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s"%(username,password))):
print("Logged İn")
else:
print("Failure")
db.commit()
loginsystem()
I am installing an online system, but I have encountered a problem with the login system. How to Fix "Unknown column 'x' in where clause" or do you have any other code suggestions?
if (cursor.execute("SELECT * FROM users WHERE (username =? password = ?) VALUES(?,?)"(username,password))):
I tried to do it with this method but didn't
Traceback (most recent call last):
File "C:/Users/artun/PycharmProjects/DENEMELER/Login System.py", line 21, in <module>
loginsystem()
File "C:/Users/artun/PycharmProjects/DENEMELER/Login System.py", line 15, in loginsystem
if (cursor.execute("SELECT * FROM users WHERE username = %s AND password = %s"%(username,password))):
File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\connection.py", line 553, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\artun\PycharmProjects\DENEMELER\venv\lib\site-packages\mysql\connector\connection.py", line 442, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'oziboran123' in 'where clause'
Process finished with exit code 1
this is the output of the code, but I expected the output of the code to be "Logged In" or "Failure"
Consider parameterization and not string interpolation which involves a two-step process of prepared statement and then execution that binds parameters. Below uses two arguments of execute call: cursor.execute(query, params). Also, user and password are reserved words in MySQL which should be escaped with backticks.
Please note the parameter placeholder, %s, for the mysql.connector API should not be confused with Python's modulo string format symbol (which by the way is the less preferred string formatting method in Python for more preferred str.format).
# PREPARED STATEMENT
sql = """SELECT * FROM `users`
WHERE `username` = %s AND `password` =%s
"""
# EXECUTE WITH PARAMS
cursor.execute(sql, (username, password))
Try this code below:
"""SELECT * FROM `users` WHERE `username` '{}' AND `password` '{}'""".format(username,password))
I have a problem that I couldn't solve
I want to update data in a MysQl table but i get this error:
cursor.execute("UPDATE employees SET PhPath='~/Desktop/test/server/dataSet/%s' WHERE id=%s; ",(generate_names(UserID,1),UserID))
File "/home/chiheb/.virtualenvs/cv/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/chiheb/.virtualenvs/cv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
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 'User.2.1.jpg'' WHERE id=2' at line 1")
and this is a part of my code:
data = recv_msg(conn)
data = json.loads(data)
UserName = input("enter user's name: ")
UserLastName = input("enter user's last name: ")
UserPost = input("enter user's post: ")
cursor.execute("INSERT INTO employees VALUES (NULL, %s, %s, %s, %s, NULL);",(UserName, UserLastName, UserPost, data['RasID']))
db.commit()
cursor.execute("SELECT LAST_INSERT_ID(); ")
UserIDL = cursor.fetchone()
UserID = UserIDL[0]
JL= data['Jliste']
for i in range(0,10) :
cel = json.loads(JL[i])
file_name = generate_names(UserID,i+1)
img = base64.b64decode(cel['img'])
with open(file_name,'wb') as _file:
_file.write(img)
print "image {} Received ".format(i+1)
cursor.execute("UPDATE employees SET PhPath='~/Desktop/test/server/dataSet/%s' WHERE id=%s; ",(generate_names(UserID,1),UserID))
response = "images Received "
conn.send(response)
db.commit()
The problem is that you can't do partial replacement with a parameter. Generate the path in code and only use "%s" (without the quotes) as the value.
I'm running sqlite3 and I cannot, for the life of me work out what is going wrong in my code. Baring in mind that it doesn't crash when creating other tables that come before it in the database. Part of me suspects it may be a case of capitalisation somewhere on an SQL statement but it was working normally a few days ago.
Create Table function
def create_table(db_name,table_name,sql):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
cursor.execute("select name from sqlite_master where name=?",(table_name,))
result = cursor.fetchall()
keep_table = True
if len(result) == 1:
response = input("The table {0} already exists, do you wish to recreate (y/n): ".format(table_name)) #option to recreate the database
if response == 'y':
keep_table = False
print("The {0} table will be recreated - all existing data will be lost".format(table_name))
cursor.execute("drop table if exists {0}".format(table_name))
db.commit()
elif response == 'n':
print("The existing table was kept")
else:
print("Incorrect input, please try again.") #validation measure
if len(result) == 1:
response = input("The table {0} already exists, do you wish to recreate (y/n): ".format(table_name))
if response == 'y':
keep_table = False
print("The {0} table will be recreated - all existing data will be lost".format(table_name))
cursor.execute("drop table if exists {0}".format(table_name))
db.commit()
else:
keep_table = False
if not keep_table:
cursor.execute(sql)
db.commit()
Tables that are causing the problem?
def create_customer_table():
sql = """create table Customer
(CustomerID integer,
FirstName text,
LastName text,
Street text,
Town text,
Postcode text,
TelephoneNumber text,
EmailAddress text
primary key(CustomerID))"""
create_table(db_name, "Customer", sql)
*ignore the fact it isn't indented, it is on my program.
def create_customer_order_table():
sql = """create table CustomerOrder
(OrderID integer,
CustomerID integer,
Date date,
Time integer
primary key(OrderID)
foreign key(CustomerID) references Customer(CustomerID))"""
create_table(db_name, "CustomerOrder", sql)
Here is the error I receive:
Traceback (most recent call last):
File "/Users/X/Downloads/manage_table_March_2015.py", line 110, in <module>
create_customer_table()
File "/Users/X/Downloads/manage_table_March_2015.py", line 78, in create_customer_table
create_table(db_name, "Customer", sql)
File "/Users/X/Downloads/manage_table_March_2015.py", line 33, in create_table
cursor.execute(sql)
ysqlite3.OperationalError: near "(": syntax error
I'm having a trouble when trying to use function to query data in MSSQL using pyodbc, the function is:
def getUserList(connection):
li = []
cur = connection.cursor()
query = 'select username, password, firstname, lastname, description, phone,'+
'email, isAdmin, isAutoBoot from users_tbl'
cur.execute(query)
for usr, pwd, fn, ln, des, ph, em, ad, au in cur.fetchall():
temp = User(usr, pwd, fn, ln, des, ph, em, ad, au)
cur.close()
#con.close()
return li
When I import the module and run this function, it encounter the TypeError:
Traceback (most recent call last):
File "<pyshell#71>", line 1, in <module>
import_user.getUserList(s_con)
File "c:/python27/mymodule\import_user.py", line 12, in getUserList
cur.execute(query)
TypeError: string indices must be integers
However if i copy and run each of these line in Python IDLE, it runs fine and no error when execute the cursor. It happens both when I pass pyodbc or sqlite connection as input parameter.
Thanks
To resolve syntax problem, change these lines:
query = 'select username, password, firstname, lastname, description, phone,'+
'email, isAdmin, isAutoBoot from users_tbl'
to either (newline not preserved):
query = ('select username, password, firstname, lastname, description, phone, '
'email, isAdmin, isAutoBoot from users_tbl')
or (newline preserved):
query = """select username, password, firstname, lastname, description, phone,
email, isAdmin, isAutoBoot from users_tbl"""
See the Maximum Line Length section of the style guide for more info on line wrapping.