I am trying to call a string value from a container tn (input given by user) to complete a sql query
# lists is a list in which all table names in the database is stored
ct=0
while ct!=2:
print("1.Enter the table name you want access")
print("2.Exit")
ct=int(input("Enter your choice (1 or 2) :"))
if ct==1:
tn=input("Enter table name :")
break
elif ct==2:
sys.exit(0)
else:
print("INVALID INPUT")
if tn in lists:
print()
print("Table found!")
mycursor.execute("SELECT * FROM (%s)",(tn,))
for i in mycursor:
print(i)
i want that sql query becomes complete it executes and show me the results back (that is the table should be displayed which the user have input)
i get this error message when running the code:-
Traceback (most recent call last):
File "C:\Users\11c1.System2-PC\Desktop\MYSQL\Mysql (Software).py", line 59, in <module>
mycursor.execute("SELECT * FROM (%s)",(tn,))
File "C:\Program Files (x86)\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\Program Files (x86)\Python37-32\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Program Files (x86)\Python37-32\lib\site-packages\mysql\connector\connection.py", line 395, 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 ''11c')' at line 1
assuming that '11c' is the input given by the user and a table by name 11c really exists in the database
When i tried the same code with ? like :-
ct=0
while ct!=2:
print("1.Enter the table name you want access")
print("2.Exit")
ct=int(input("Enter your choice (1 or 2) :"))
if ct==1:
tn=input("Enter table name :")
break
elif ct==2:
sys.exit(0)
else:
print("INVALID INPUT")
if tn in lists:
print()
print("Table found!")
mycursor.execute("SELECT * FROM (?)",(tn,))
for i in mycursor:
print(i)
this gives the error as:-
Traceback (most recent call last):
File "C:\Users\11c1.System2-PC\Desktop\MYSQL\Mysql (Software).py", line 59, in <module>
mycursor.execute("SELECT * FROM (?)",(tn,))
File "C:\Program Files (x86)\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 543, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
Try below query syntex:
query= "SELECT * FROM %s"
mycursor.execute(query%(tn,))
Hope this helps!
Related
def connectxmlDb(dbparams):
conn_string = "host='{}' dbname='{}' user='{}' password='{}' port='{}'"\
.format(dbparams['HOST'], dbparams['DB'], dbparams['USERNAME'], dbparams['PASSWORD'], dbparams['PORT'])
try:
conn = psycopg2.connect(conn_string)
pass
except Exception as err:
print('Connection to Database Failed : ERR : {}'.format(err))
return False
print('Connection to Database Success')
return conn
dbconn = connectxmlDb(params['DATABASE'])
The above mentioned is the code I use to connect to postgresql database
sql_statement = """ select a.slno, a.clientid, a.filename, a.user1_id, b.username, a.user2_id, c.username as username2, a.uploaded_ts, a.status_id
from masterdb.xmlform_joblist a
left outer join masterdb.auth_user b
on a.user1_id = b.id
left outer join masterdb.auth_user c
on a.user2_id = c.id
"""
cursor.execute(sql_statement)
result = cursor.fetchall()
This is my code to extract data from a postgres table using python.
I want to know if it is possible to view this data using Pandas
This is the code I used:
df = pd.read_sql_query(sql_statement, dbconn)
print(df.head(10))
but it is showing error.
C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-
packages\pandas\io\sql.py:762: UserWarning: pandas only support SQLAlchemy connectable(engine/connection) ordatabase string URI or sqlite3 DBAPI2 connectionother DBAPI2 objects are not tested, please consider using SQLAlchemy
warnings.warn(
Traceback (most recent call last):
File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2023, in execute
cur.execute(*args, **kwargs)
psycopg2.errors.SyntaxError: syntax error at or near ";"
LINE 1: ...ELECT name FROM sqlite_master WHERE type='table' AND name=?;
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\Lukmana\Desktop\user_count\usercount.py", line 55, in <module>
df = pd.read_sql_table(result, dbconn)
File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 286, in read_sql_table
if not pandas_sql.has_table(table_name):
File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2200, in has_table
return len(self.execute(query, [name]).fetchall()) > 0
File "C:\Users\Lukmana\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\sql.py", line 2035, in execute
raise ex from exc
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': syntax error at or near ";"
LINE 1: ...ELECT name FROM sqlite_master WHERE type='table' AND name=?;
I am trying to add multiple records into a database using mysql-connector-python. I was able to add one record initially but for multiple records, this error has been persistent.
class DataBase:
def __init__(self):
try:
self.connection = mysql.connector.connect(host='xx.xxx.xx.xx',
database='XXX',
user='XXXX',
password='xxxx')
except Error as error:
print("Failed to connect: {}".format(error))
def store_into_table(self, df):
mySql_insert_query = """INSERT INTO data ('data_id', 'a', 'b', 'c') VALUES ('%S','%S', '%S', '%S') """
records_to_insert = df
cursor = self.connection.cursor()
cursor.executemany(mySql_insert_query, records_to_insert)
self.connection.commit()
print(cursor.rowcount, " Records inserted successfully into data table")
The variable that I am passing to an instance of DataBase is:
data = [('101', 'name_1', '3', 'sample'), ('102', 'name_2', '5', 'sample_1')]
Exception being thrown:
Traceback (most recent call last):
File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 317, in _batch_insert
"Not all parameters were used in the SQL statement")
ProgrammingError: Not all parameters were used in the SQL statement
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-123-44fdf194cd10>", line 1, in <module>
db.store_into_table(data)
File "/Users/dev/Detector/database.py", line 30, in store_into_table
cursor.executemany(mySql_insert_query, records_to_insert)
File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 350, in executemany
stmt = self._batch_insert(operation, seq_params)
File "/Users/dev/anaconda3/envs/sql/lib/python3.7/site-packages/mysql/connector/cursor_cext.py", line 329, in _batch_insert
"Failed executing the operation; %s" % err)
InterfaceError: Failed executing the operation; Not all parameters were used in the SQL statement
Any clues as to why this isn't working?
Try this:
mySql_insert_query = """INSERT INTO data (data_id, a, b, c) VALUES (%s, %s, %s, %s) """
I'm trying to add data to a database from a GUI and using SQLite and getting this error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call
return self.func(*args)
File "C:\Users\max\Documents\compsci stuff\NEW COMPSCI.py", line 196, in get_items
C.execute(sql ,vari)
sqlite3.OperationalError: near ")": syntax error
def get_items(self,*args,**kwargs): #this function gets the items from the entry boxes
self.carmake= self.carmake_e.get()
self.carmodel= self.carmodel_e.get()
self.regi= self.regi_e.get()
self.colour= self.colour_e.get()
self.cost= self.cost_e.get()
self. tcost= self. tcost_e.get()
self.sellprice= self.sellprice_e.get()
self.assumedprofit= self.assumedprofit_e.get()
if self.carmake == " or self.carmodel" == "== self.colour == ":
print ("WRONG")
TKInter.messagebox.showinfo("error", "please enter values for car make, model and colour")
else:
print ("solid m8")
sql= "INSERT INTO inventory(car_make,car_model, registration_plate,colour,cost,total_cost,selling_price,assumed_profit) VALUES) (?,?,?,2,?,?,2,?)"
vari=(self.carmake, self.carmodel,self.regi,self.colour,self.cost,self.tcost,self.sellprice,self.assumedprofit)
C.execute(sql ,vari)
C.execute(sql(self.name,self.carmake, self.carmodel, self.regi,self.colour, self.cost,self.tcost,self.sellprice,self.assumedprofit))
conn.commit()
tkinter.messagebox.showinfo("success", "succesfully added to databse!")
You have an extra parenthesis in this line
sql= "INSERT INTO inventory(car_make,car_model, registration_plate,colour,cost,total_cost,selling_price,assumed_profit) VALUES) (?,?,?,2,?,?,2,?)"
VALUES) should be VALUES.
i keep getting the error
Traceback (most recent call last):
File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "G:\computing project\add to db.py", line 114, in get_items
C.execute(sql ,vari)
sqlite3.OperationalError: table inventory has no column named car_make
when attempting to insert values into mydatabse taken from tkinter
i have tried changing the names of the columns in order to see if that would solve the issue bit nothing has changed, i am using db browser in order to edit my database
from tkinter import*
import sqlite3
conn = sqlite3.connect("G:\computing project\database of cars.db")
C = conn.cursor()
import tkinter.messagebox
def get_items(self,*args,**kwargs): #this function gets the items from the entry boxes
self.carmake= self.carmake_e.get()
self.carmodel= self.carmodel_e.get()
self.regi= self.regi_e.get()
self.colour= self.colour_e.get()
self.cost= self.cost_e.get()
self.tcost= self.tcost_e.get()
self.sellprice= self.sellprice_e.get()
self.assumedprofit= self.assumedprofit_e.get()
self.assumedprofit= float(self.sellprice)- float(self.tcost)
if self.carmake == '' or self.carmodel == '' == self.colour == '':
print ("WRONG")
tkinter.messagebox.showinfo("error","please enter values for car make, model and colour")
else:
print("solid m8 ")
sql = "INSERT INTO inventory(car_make,car_model,registration_plate,colour,cost,total_cost,selling_price,assumed_profit) VALUES (?,?,?,?,?,?,?)"
vari=(self.carmake,self.carmodel,self.regi,self.colour,self.cost,self.tcost,self.sellprice,self.assumedprofit)
C.execute(sql ,vari)
# C.execute(sql(self.name,self.carmake,self.carmodel,self.regi,self.colour,self.cost,self.tcost,self.sellprice,self.assumedprofit))
conn.commit()
tkinter.messagebox.showinfo("success","succesfully added to databse")
Having an issue inserting a value into a mysql table using a dynamic list. I keep having a problem with the quotes pointing to a table instead of a value.
Here is the code:
lst = ['Pid', 'Base', 'Size', 'LoadCount', 'Path','Casename']
lst2 =['888', '1213726720', '61440', '65535', '\\SystemRoot\\System32\\smss.exe', 'wean']
table_name ="test"
insert_intotable = "INSERT INTO " + table_name + "(" + ",".join(lst) + ") VALUES (" + ",".join(lst2) + ")"
print insert_intotable
c.execute(insert_intotable)
conn.commit()
c.close()
conn.close()
This causes the following error:
> Traceback (most recent call last): File "pp.py", line 53, in
> <module>
> c.execute(insert_intotable) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in
> execute
> self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in
> defaulterrorhandler
> raise errorclass, errorvalue
> _mysql_exceptions.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
> '\\SystemRoot\\System32\\smss.exe,test)' at line 1")
What is causing this syntax issue?
Use 'single quotes' when inserting string values
UPD:
Change second line to this
lst2 =['888', '1213726720', '61440', '65535', '\'\\SystemRoot\\System32\\smss.exe\'', '\'wean\'']
Because, it is wrong SQL query:
INSERT INTO test (Path) VALUES(\SystemRoot\System32\smss.exe)
And your code generates such query.
You should quote varchar values:
INSERT INTO test (Path) VALUES('\SystemRoot\System32\smss.exe')