How to pass postgre sql schema in Python Dataset - python

I have a code to insert row:
try:
con = dataset.connect("postgresql://abc:abc#localhost:5432/dbname")
con.begin()
result = con.load_table("schema1.table_name").insert(document)
con.commit()
return result
except Exception as e:
return False
But I always get the error like this:
raise DatasetException("Table does not exist: %s" % self.name)
It's the issue related to schema, how can I pass schema name into this insert query?

For someone who need:
con = dataset.connect("postgresql://abc:abc#localhost:5432/dbname", schema=schema)

Related

Getting error while inserting data in sqlite3

I am new to Python and started off with sqlite.
I have two csv transaction.csv and users.csv from where I am reading the data and writing to the sqlite database.Below is the snippet
import csv
import sqlite3 as db
def readCSV_users():
with open('users.csv',mode='r') as data:
dr = csv.DictReader(data, delimiter=',')
users_data = [(i['user_id'], i['is_active']) for i in dr if i['is_active']=='True']
#---------------------
return users_data
def readCSV_transactions():
with open('transactions.csv',mode='r') as d:
dr = csv.DictReader(d, delimiter=',')
trans_data = [(i['user_id'], i['is_blocked'],i['transaction_amount'],i['transaction_category_id']) for i in dr if i['is_blocked']=='False']
#---------------------
return trans_data
def SQLite_connection(database):
try:
# connect to the database
conn = db.connect(database)
print("Database connection is established successfully!")
conn = db.connect(':memory:')
print("Established database connection to a database\
that resides in the memory!")
cur = conn.cursor()
return cur,conn
except exception as Err:
print(Err)
def dbQuery(users_data,trans_data,cur,conn):
try:
cur.executescript(""" CREATE TABLE if not exists users(user_id text,is_active text);
CREATE TABLE if not exists transactions(user_id text,is_blocked text,transaction_amount text,transaction_category_id text);
INSERT INTO users VALUES (?,?),users_data;
INSERT INTO transactions VALUES (?,?,?,?),trans_data""")
conn.commit()
a=[]
rows = curr.execute("SELECT * FROM users").fetchall()
for r in rows:
a.append(r)
return a
except Err:
print(Err)
finally:
conn.close()
if __name__ == "__main__":
database='uit'
users_data=readCSV_users()
trans_data=readCSV_transactions()
curr,conn=SQLite_connection(database)
print(dbQuery(users_data,trans_data,curr,conn))
But I am facing below error.I believe the ? is throwing the error in executescript
cur.executescript(""" CREATE TABLE if not exists users(user_id text,is_active text);
sqlite3.OperationalError: near "users_data": syntax error
Any pointers to resolve this?
Putting users_data directly in query is wrong. It treats it as normal string.
But it seems executescript can't use arguments.
You would have to put values directly in place of ?.
Or you have to use execute()
cur.execute("INSERT INTO users VALUES (?,?);", users_data)
cur.execute("INSERT INTO transactions VALUES (?,?,?,?)", trans_data)

How to send a variable that have string data read from a file to an sql update query in python

Im trying to pass a string in my update query which is read from a text file(this have only one string)
Also im trying to update a oracle database table
My code looks like this
try:
with open('update.txt') as file1:
upd_value = up.read() # This gives me upd_value = 'HELLO_THERE'
conn = cx_Oracle.connect('usr', 'pwd', dsn_tns)
cursor = conn.cursor()
upd_query = """ update some_table set something = %s where some_column = 'some_value',
(upd_value,) """
cursor.execute(upd_query)
except cx_Oracle.DatabaseError as exc:
err, = exc.args
print("Oracle-Error-Code:", err.code)
print("Oracle-Error-Message:", err.message)
finally:
cursor.close()
conn.close()
Im getting the following error
Oracle-Error-Code: 911
Oracle-Error-Message: ORA-00911: invalid character
It would be great if somebody help me out with this, Thanks in advance!

Why does this SQL query fail?

I have a database class which abstracts some basic crud logic.
The issue lies in the fetch_single method:
The sql_insecure query works fine, and returns the expected results.
The sql_prepared query doesn't return any errors, but also doesn't return any results which match the parameters, when they clearly do exist within the db.
sql_prepared follows the same approach to prepared statements that the insert_single method implements, and this method also returns the expected results.
My question is; why is the sql_prepared query not returning any results?
import sqlite3
class Database:
def __init__(self, db: str):
try:
self.conn = sqlite3.connect(db)
self.cursor = self.conn.cursor()
except sqlite3.Error as e:
print(e)
self.__del__
def fetch_all(self, table: str):
try:
query = self.cursor.execute("SELECT * FROM ?", table)
rows = self.cursor.fetchall()
return rows
except sqlite3.Error as e:
print(e)
return False
def fetch_single(self, table: str, column_name: str, column_value):
sql_formatted_value = "'{value}'".format(value=column_value)
placeholder = ":{column_name}".format(column_name=column_name)
sql_insecrue = "SELECT * FROM %s WHERE %s=%s Limit 1" % (
table, column_name, sql_formatted_value)
sql_prepared = "SELECT * FROM %s WHERE %s=%s LIMIT 1" % (
table, column_name, placeholder)
# try:
# self.cursor.execute(sql_insecrue)
# rows = self.cursor.fetchall()
# return rows
# except sqlite3.Error as e:
# print(e)
# return False
try:
self.cursor.execute(sql_prepared, [sql_formatted_value, ])
rows = self.cursor.fetchall()
return rows
except sqlite3.Error as e:
print(e)
return False
def insert_single(self, table: str, data: list):
columns = ""
placeholders = ""
values = []
data_length = len(data)
for index, (key, value) in enumerate(data):
# we need to dynamically build some strings based on the data
# let's generate some placeholders to execute prepared statements
columns += "{column_name}".format(column_name=key)
placeholders += ":{column_name}".format(column_name=key)
# let's fill the insert values into a list to use with execute
values.append(value)
# only add a comma if there is another item to assess
if index < (data_length - 1):
columns += ', '
placeholders += ', '
sql = "INSERT INTO %s (%s) VALUES (%s)" % (
table, columns, placeholders)
try:
self.cursor.execute(sql, values)
self.conn.commit()
except sqlite3.Error as e:
print(e)
You cannot substitute table name using ? in prepared statements because it is not considered a query parameter.
I recommend doing something like this:
self.cursor.execute(f"DELETE FROM {table} WHERE id=?", [id])
In other words, use standard python format statements to specify your table name, but use prepared statement anchors like ? for any query parameters.
okay, i found the problem.
its was my sloppy sql syntax.
using backticks around the table and column name solved the issue.
def fetch_single(self, table: str, column_name: str, column_value):
sql_formatted_value = "'{value}'".format(value=column_value)
placeholder = ":{column_name}".format(column_name=column_name)
sql_insecure = "SELECT * FROM %s WHERE %s=%s" % (
table, column_name, sql_formatted_value)
sql_prepared = "SELECT * FROM `%s` WHERE `%s`=%s" % (
table, column_name, placeholder)
print(sql_insecure)
print(sql_prepared)
# try:
# self.cursor.execute(sql_insecure)
# row = self.cursor.fetchall()
# print(row)
# return row
# except sqlite3.Error as e:
# print(e)
# return False
try:
self.cursor.execute(sql_prepared,
[column_value, ])
row = self.cursor.fetchone()
return row
except sqlite3.Error as e:
print(e)
return False

How to check the existence of a column in the Postgres database I am connected to in Python?

I am using postgres in Python with the library psycopg2. After connecting to a database, I am trying to check if a table with a given name exists. In postgres, I am doing this with the following lines:
\connect myDB
select exists(select * from pg_tables where schemaname='public' AND tablename='mytable';)
This works if the table exists but also if it doesn't.
In python I am doing this with the following lines:
import psycopg2 as pg
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT;
from psycopg2 import sql;
conn = pg.connect(user='postgres', host='localhost', password="pwd");
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
conn.autocommit = True
sql_table_check = sql.SQL("select exists(select * from pg_tables where schemaname='public' AND tablename={});")\
.format(sql.Identifier("mytable"));
cur = conn.cursor()
but this is returning the error
psycopg2.errors.UndefinedColumn: column "mytable" does not exist
LINE 1: ...m pg_tables where schemaname='public' AND tablename="mytable");
because such table has not been created yet.
What is the correct way to check if a column exists in psycopg2?
EDIT
Note that I would like to check the existence of the table in the database I am connected to, I don't mind if it exists in another database.
My comment as an answer:
import psycopg2
con = psycopg2.connect("dbname=production host=localhost user=postgres")
tbl_sql = "SELECT count(*) FROM pg_tables WHERE schemaname='public' AND tablename= %s"
cur = con.cursor()
cur.execute(tbl_sql, ('cell_per',))
cur.fetchone()
(1,)
cur.execute(tbl_sql, ('cell_p',))
cur.fetchone()
(0,)
so you can go over error by using try and except to continue
sql_table_check = sql.SQL("select exists(select * from pg_tables where schemaname='public' AND tablename={});")\
.format(sql.Identifier("mytable"));
try:
cur = conn.cursor()
print('Table exists')
except:
print('Table does not exist')
Edited based on comment
you can also catch the error to check it later by
sql_table_check = sql.SQL("select exists(select * from pg_tables where schemaname='public' AND tablename={});")\
.format(sql.Identifier("mytable"));
try:
cur = conn.cursor()
print('Table exists')
except Exception as e:
print(e)
print('Table does not exist')
for example, and simply, if we try:
try:
a = 5 / 0
except Exception as e:
print(e)
We will get output
division by zero
you can get the exact format string through debugging e content in the exception part.
so we can then, use this to identify the error, to be used again like:
try:
a = a / 0
except Exception as e:
print(e)
if e.args[0] == 'division by zero':
print('it is division by zero Error')
else:
raise(e)
so if the error is not the intended one the other error will raise.
you may get the error exception from psycopg2 documentation as that for python in https://docs.python.org/3/library/exceptions.html
as in the follwoing code:
try:
a = 5 / 0
except ZeroDivisionError:
print('it is division by zero Error')
so we get:
it is division by zero Error
but when we have another error like:
try:
a = 5 / 0
except ZeroDivisionError:
print('it is division by zero Error')
we get the other error
NameError: name 'b' is not defined

Update statement not working in python?

I've written a simple python program, which get Data form database successfully. but unable to update table in DB.
When executing update statement it's get stuck and nothing happen, no any exception.
My code is as follows. Any idea whyis this ?
from java.sql import DriverManager
def updateDB():
url = "jdbc:oracle:thin:#192.1.1.1:1521:auid"
uname = "dbtstj1"
pword = "dbtstj321"
conn = None
stmt = None
try:
conn = DriverManager.getConnection(url,uname,pword)
stmt = conn.createStatement()
rs = stmt.executeQuery("select PKG_NAME from PkgData")
while rs.next():
print rs.getString(1)
pkgName = "'Test Pkg Name'"
pkgID = "'T1234'"
updateQuary = "UPDATE PkgData SET PKG_NAME =%s WHERE PKG_ID =%s" %(pkgName, pkgID)
stmt.execute(updateQuary)
except Exception , e:
print 'Error:', e[0]
finally:
if stmt is not None:
stmt.close()
if conn is not None:
conn.close()
updateDB()
you need to commit your changes to the database:
stmt.execute(updateQuary)
conn.commit()
These type of issues can be happen when query request datatype and required datatype is difference.
It seems to be there was a mismatch with database's datatype and your query. Can you recheck with database's datatype with your query.
For Ex: PKG_ID =%s can be another data type in database as digit or etc...

Categories

Resources