Is it possible to view a postgresql table using Pandas? - python

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=?;

Related

cx_Oracle.DatabaseError: ORA-00911: invalid character

Here is my Python code:
x,y,z= input("Enter a Three value: ").split()
comp=input("Company's Name: ")
tkt = intkt
import cx_Oracle
connection = cx_Oracle.connect("PAY", "PAY", "192.200.168.15/XE")
cursor = connection.cursor()
query="select TICKETNO,PERIOD,PARAMETERVALUE from transaction where PARAMETERCODE = '01' and COMPANYCODE = :comp AND ticketno in {:x, :y, :z}"
cursor.execute( query, (comp,x,y,z))
for ticketno,period, PARAMETERVALUE in cursor:
print(ticketno,period, PARAMETERVALUE)
Here is the error when this file is run:
Traceback (most recent call last):
File "C:\python-programs\NoOfDaysquery1.py", line 29, in <module>
cursor.execute( query, (comp,x,y,z))
cx_Oracle.DatabaseError: ORA-00911: invalid character
I'm unable to figure out what the error message is all about and how can I resolve it.
Try using '(' instead of '{':
query="select TICKETNO,PERIOD,PARAMETERVALUE from transaction where PARAMETERCODE = '01' and COMPANYCODE = :comp AND ticketno in (:x, :y, :z)"

How do I rectify the error in mysql-connector where I try to add multiple records at the same time into a MySQL Database?

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) """

How to call a string value into sql query in python

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!

pymysql: MySQL Parameterized LIKE Query

Why am I getting TypeError: not all arguments converted during string formatting when trying to execute this query? I need to be able to append %{}% to the IP I am passing in so that i can run a LIKE mysql query.
If this isn't the correct way to parameterize a LIKE query using the % wildcard, how do you do this?
Class:
class IpCleaner(object):
def __init__(self, ip):
self.ip = ip
self.iplike = '%{}%'.format(self.ip)
def lookup(self):
self.dbconnect()
select_query = (
"SELECT `name`,`source`,`destination` FROM mytable "
"WHERE (`source` LIKE ? OR `destination` ? );"
)
params = [self.iplike, self.iplike]
print params
self.cur.execute(select_query, params)
print self.cur.fetchall()
Instantiation:
a = IpCleaner('74.121.242.2')
a.lookup()
output:
Traceback (most recent call last):
['%74.121.242.2%', '%74.121.242.2%']
File "/home/dobbs/shunlibs/IpCleaner.py", line 87, in <module>
a.palorulelookup()
File "/home/dobbs/shunlibs/IpCleaner.py", line 81, in lookup
self.cur.execute(select_query, params)
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 164, in execute
query = self.mogrify(query, args)
File "/usr/lib64/python2.7/site-packages/pymysql/cursors.py", line 143, in mogrify
query = query % self._escape_args(args, conn)
TypeError: not all arguments converted during string formatting
use %s instead of ? in the select_query variable

Error inserting value with special character into mysql table using python

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')

Categories

Resources