python 3.5 selecting and using results from postgres database - python

I need to write a program which can,firstly, for ip adresses of people who saw my campaign on google, and then give me detailed information about each of these persons.
I have all information in postgres database and use python 3.5
Here is my code:
def get_connection(cursor_factory=None):
conn_string_pg = "host= '" + host + "' dbname = '" + dbname + "' user = '" + user + \
"' password = '" + password + "'"
if cursor_factory is None:
conn_pg = psycopg2.connect(conn_string_pg)
else:
conn_pg = psycopg2.connect(conn_string_pg,
cursor_factory=cursor_factory)
return conn_pg
def find_logs():
select = """ select ip_address from log_files o where o.url like
'%my_campaign'
"""
conn = get_connection(cursor_factory = RealDictCursor)
cur = conn.cursor()
cur.execute(select)
records = cur.fetchone()
for item in records:
select_2 = "select * from log_files where ip_address = %(item)s "
cur.execute(select_2)
logs = cur.fetchone()
return logs
print(find_logs())
cur.close()
Unfortunately I get this error:
psycopg2.ProgrammingError: syntax error at or near "%" LINE 1:
...elect * from web_logs.log_data where ip_address = %(item)s o...

Your string interpolation is incorrect. You're trying to insert the value of item into your select_2 statement, but you don't actually do the string interpolation, so you pass psycopg2 an invalid SQL statement. You want to do something like
select_2 = "select * from log_files where ip_address = {}".format(item)

It's because ip_address = %(item)s it's not a valid sql syntax. You should make string formatting before:
select_2 = "select * from log_files where ip_address = %(item)s " % {'item': item}
And the better way to do it is to give all the transformation to the postgres driver
select_2 = "select * from log_files where ip_address = %s "
cur.execute(select_2, (item, ))

Related

python unable to parse table name correctly in sql query

I am newbie to snowflake. I am trying to fetch ddl s of all tables in my db using python script.
import snowflake.connector
import sys
# Gets the version
cnx = snowflake.connector.connect(
user='username',
password='password',
account='account',
database='db',
schema='schema',
warehouse='warehouse',
role='role'
)
cnx.cursor().execute("USE warehouse warehouseName")
cnx.cursor().execute("USE database dbName")
cnx = cnx.cursor()
dbSchema='schema name'
sql_select_objects = "select TABLE_NAME,TABLE_SCHEMA,TABLE_TYPE from INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='" + dbSchema + "';"
print(sql_select_objects)
try:
cnx.execute(sql_select_objects)
print('Query ID=' + cnx.sfqid)
rows = cnx.fetchall()
print("rows count:", len(rows))
for row in rows:
#print(row[0])
file = open(row[0] + ".sql","w")
rw='"' + row[0] + '"'
print(rw)
sql_ddl_object = "SELECT GET_DDL('TABLE', '" + row[0] + "')"
print(sql_ddl_object)
cnx.execute(sql_ddl_object)
print('SQL2 sfqid=' + cnx.sfqid)
row_ddl_table = cnx.fetchall()
#print(row_ddl_table[0][0])
file.write(str(row_ddl_table[0][0]))
file.write("\n")
finally:
cnx.close()
When I execute above script i get error:
snowflake.connector.errors.ProgrammingError: 002003 (02000): SQL compilation error:
Table 'SNOWFLAKE_TEST' does not exist or not authorized.
in line "SELECT GET_DDL('TABLE', '" + row[0] + "')", i believe python is not parsing value of row[0] correctly. Can you please suggest me where the error is?
Please use both schema and table name as you may have the table SNOWFLAKE_TEST in a different schema:
"SELECT GET_DDL('TABLE', '" + row[1] + "." + row[0] + "')"
I would also surround the table name and schema names in double quotes because maybe your schema or table is created with a case-sensitive name:
"SELECT GET_DDL('TABLE', '\"" + row[1] + "\".\"" + row[0] + "\"')"
Most likely the issue is not with the table name but the role that you are using in the code. The role most likely does not have access to the DB and it's table hence the error message is seen.

How to exec multiple queries using mysqldb on flask

I've been trying to make my code works but can't find why won't it does what it does.
I'm trying to put data in my database, by it only seem to exec one ....
The code is :
def setCubesValues(value):
mysql.connection.autocommit(on=True)
tabVal = value.split(';;')
del tabVal[0]
for i in range(0, len(tabVal)):
tabi = tabVal[i]
cur = mysql.connection.cursor()
responseCur = cur.execute('SELECT * FROM idcudetoaction where id_cube = "' + tabi +'"')
if responseCur == 1:
curResultInsert = cur.execute('update idcudetoaction set action = "' + tabi +'" where id_cube = ' + str(i))
else:
curResultInsert = cur.execute('insert into idcudetoaction (id_cube, action) values (' + str(i)+', "' + tabi +'")')
return jsonify(curResultInsert);
Thing is I have 7 values, but only on that get put in the database ...
Any help ? :)
thx !

Why Sqlite3 table wont UPDATE

I've tried making an update to my sqlite3 table but it doesn't seem to work.
marks = "My long name here"
conn = sqlite3.connect('mydb.db')
cur=conn.cursor()
cur.execute("UPDATE '" + str(marks) +"' SET (ENG,KIS,MAT,BIO) = (-1,-1,-1,-1) WHERE (ENG,KIS,MAT,BIO) =('nan','nan','nan','nan')")
conn.commit()
conn.close()
I can't see nay error in my code.
You don't need 4 separate statements.
You can do it in 1 statement with CASE expressions:
UPDATE tablename
SET ENG = CASE WHEN ENG = 'nan' THEN -1 ELSE ENG END,
KIS = CASE WHEN KIS = 'nan' THEN -1 ELSE KIS END,
MAT = CASE WHEN MAT = 'nan' THEN -1 ELSE MAT END,
BIO = CASE WHEN BIO = 'nan' THEN -1 ELSE BIO END
WHERE 'nan' IN (ENG,KIS,MAT,BIO)
This worked, separating them.
marks = "My long name here"
conn = sqlite3.connect('mydb.db')
cur=conn.cursor()
cur.execute("UPDATE '" + str(marks) + "' SET ENG=-1 WHERE ENG='nan'")
cur.execute("UPDATE '" + str(marks) + "' SET KIS=-1 WHERE KIS='nan'")
cur.execute("UPDATE '" + str(marks) + "' SET MAT=-1 WHERE MAT='nan'")
cur.execute("UPDATE '" + str(marks) + "' SET BIO=-1 WHERE BIO='nan'")
conn.commit()
conn.close()
Rather than this.
marks = "My long name here"
conn = sqlite3.connect('mydb.db')
cur=conn.cursor()
cur.execute("UPDATE '" + str(marks) +"' SET (ENG,KIS,MAT,BIO) = (-1,-1,-1,-1) WHERE (ENG,KIS,MAT,BIO) =('nan','nan','nan','nan')")
conn.commit()
conn.close()

Python psycopg2 returning False when pgadmin returns True

I have a python function in which I want to check if a PostgreSQL table exists or not (True, False)
it does not return True... even when I am logged into the same DB and checking in PGAdmin4.. and getting True.
Am I missing a commit? I tried adding a commit() to no effect.
def __exists_table(self, table_name):
cursor = self.__get_a_cursor()
try:
string_to_execute = "SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename = '" + table_name + "');"
cursor.execute(string_to_execute)
query_results = cursor.fetchall()
if len(query_results) > 1:
print("__exists_data got back multiple results, using the first")
query_results = query_results[0][0]
return query_results
except Exception as err:
print("Exception on __exists_table: " + str(err))
raise err
finally:
cursor.close()
Your code appears to work as written.
I have a database that contains a single table, table1:
$ psql -h localhost
psql (11.6, server 12.1 (Debian 12.1-1.pgdg100+1))
Type "help" for help.
lars=> \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+-------
public | table1 | table | lars
(1 row)
If I wrap your code up in a runnable script, like this:
import psycopg2
class DBTest:
def __init__(self):
self.db = psycopg2.connect('host=localhost dbname=lars password=secret')
def __get_a_cursor(self):
return self.db.cursor()
def __exists_table(self, table_name):
cursor = self.__get_a_cursor()
try:
string_to_execute = "SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_tables WHERE schemaname = 'public' AND tablename = '" + table_name + "');"
cursor.execute(string_to_execute)
query_results = cursor.fetchall()
if len(query_results) > 1:
print("__exists_data got back multiple results, using the first")
query_results = query_results[0][0]
return query_results
except Exception as err:
print("Exception on __exists_table: " + str(err))
raise err
finally:
cursor.close()
def test_exists_table(self, table_name):
return self.__exists_table(table_name)
db = DBTest()
for table_name in ['table1', 'table2']:
if db.test_exists_table(table_name):
print(f'{table_name} exists')
else:
print(f'{table_name} does not exist')
Running it produces the output I would expect:
table1 exists
table2 does not exist
Having said that, I would make the follow change to your code. First, rather than creating your query string like this:
string_to_execute = """SELECT EXISTS(
SELECT 1 FROM pg_catalog.pg_tables
WHERE schemaname = 'public'
AND tablename = '""" + table_name + "');"
cursor.execute(string_to_execute)
I would let your database driver take care of parameter substitution for you:
string_to_execute = """SELECT EXISTS(
SELECT 1 FROM pg_catalog.pg_tables
WHERE schemaname = 'public'
AND tablename = %s
)"""
cursor.execute(string_to_execute, (table_name,))
This is easier to read and safer, since it will properly quote any special character in the parameter.

Python MySQL: cursor.excute(query,args) fails while cursor.execute(query) passes for the same query

I have a python function that gets me version info like this:
def get_version_info(build_id, table_name='build_info'):
query = "SELECT `build_commit` FROM `" + table_name + "` WHERE `build_id` = %s "
args = (build_id)
#query = "SELECT `build_commit` FROM `" + table_name + "` WHERE `build_id` = " + build_id
build_commit = None
try:
dbconfig = read_db_config()
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
cursor.execute(query, args)
#cursor.execute(query)
row = cursor.fetchone()
if row is not None:
build_commit = row[0]
This functions always returns None no matter what.
And when I change this to use non-parameterized query like below. It always returns correct build_commit.
def get_version_info(build_id, table_name='build_info'):
query = "SELECT `build_commit` FROM `" + table_name + "` WHERE `build_id` = " + build_id
...
cursor.execute(query)
Is there something, I am missing out in the parameterized query for cursor.execute which is why it is failing.

Categories