Prestodb + Python: Using a List as Query Argument - python

I'm trying to use prestodb in Python and pass a list of numbers as an argument in a query and it's giving this error:
PrestoUserError: PrestoUserError(type=USER_ERROR, name=TYPE_MISMATCH, message="line 208:33: IN value and list items must be the same type: bigint", query_id=20211122_175131_24052_rruhu)
The code is similar to this:
import prestodb
from prestodb import dbapi
import os
conn=prestodb.dbapi.connect(
host=os.environ['aa'],
port=os.environ['bb'],
user=os.environ['cc'],
password=os.environ['dd'],
catalog='hive'
)
date_start = '2021-10-10'
date_end = '2021-10-15'
list_id = (1,2,3,4)
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in ({list_id})
'''.format(date_start=date_start,date_end=date_end,list_id=list_id)
cur = conn.cursor()
cur.execute(sql)
query_result = cur.fetchall()

format will not join the list_id correctly. Try combining ids into comma separated strings with ','.join(map(str, list_id)):
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in ({list_id})
'''.format(date_start=date_start,date_end=date_end,list_id=','.join(map(str, list_id)))
UPD
Or, as suggested by #Tomerikoo - just str(list_id) and remove extra parenthesis from the format:
sql = '''
SELECT
*
FROM
table
WHERE
DATE BETWEEN '{date_start}'
AND '{date_end}'
AND ID in {list_id}
'''.format(date_start=date_start,date_end=date_end,list_id=str(list_id))

Related

Problem in Where clause of SQL Query while comparing String using python variable

Im trying to select from MS Access Database table in python WHERE Itemname is equal to some particular string which is stored in variable , while comparing that string directly in where clause with LIKE operator it is working fine but when i`m trying to pass this string through variable it is showing me syntex error.
query = "SELECT * FROM table1 WHERE table1.Itemname LIKE 'XYZ' "
cursor2.execute(query)
This is working fine
Itemname = "XYZ"
query = "SELECT * FROM table1 WHERE table1.Itemname LIKE %s "
cursor2.execute(query,(Itemname,))
Itemname = "XYZ"
query = "SELECT * FROM table1 WHERE table1.Itemname LIKE {}".format(Itemname)
cursor2.execute(query)
None of the Two above options are working ,Pls point out if there is any syntex problem
This is working finally
Itemname = "XYZ"
query = "SELECT * FROM table1 WHERE table1.Itemname = ? "
cursor2.execute(query,[itemname])

Error with params in Pandas read_sql_query

I have a simple SQl query and i want to use params, but it returns a database error when I add the param. If I change the dynamic param to a fixed value, it works.
My code is quite simple:
sql = """ SELECT p.description, SUM(p.price) as price
FROM product p
WHERE p.extenal_id = ?
GROUP BY p.description """
result = pd.read_sql_query(sql, con=conn, params=[202101])
The error is:
SyntaxError: syntax error at or near "GROUP"
LINE 4: p.external_id = ? GROUP BY p.description...
The problem can't be the query since if I change the ? by 202101 and remove the params in read_sql_query it works.
What am I doing wrong?
I'm using Google Colab
#use f-string is better
params=[202101]
sql = f""" SELECT p.description, SUM(p.price) as price
FROM product p
WHERE p.extenal_id = {params}
GROUP BY p.description """
result = pd.read_sql_query(sql, con=conn)
#use tuple for more items
params=tuple([202101, 202102, 202103])
sql = f""" SELECT p.description, SUM(p.price) as price
FROM product p
WHERE p.extenal_id in {params}
GROUP BY p.description """
result = pd.read_sql_query(sql, con=conn)

Passing variables into a MySQL query using python

I have a database with some records that have a date field of "05221999". I am trying to do a SQL query from the input of the user based on just the month and year. In this case I am interested in all the records with the month of 05 and the year of 1999.
Unfortunately, I can't get the Python/SQL syntax correct. Here is my code so far:
def submitact(self):
date = self.md.get()
month = date[0:2]
year = date[2:7]
db = pymysql.connect("localhost", "username", "password", "database")
cursor = db.cursor()
cursor.execute("SELECT * FROM `table` WHERE `Code` = 'RM' AND `Date` LIKE %s'_'%s", (month, year))
results = cursor.fetchall()
print(results)
cursor.close()
db.close()
I've done several variations on the SELECT statement and they either return errors or nothing.
Thanks!
In the code snippet below, I used f-string style to format the query string
[...]
query = f"SELECT * FROM `table` WHERE `Code` = 'RM' AND LEFT(`Date`, 2) = '{month}' AND RIGHT(`Date`, 4) = '{year}'"
cursor.execute(query)
[...]
try with this:
query = "SELECT * 'table' WHERE 'Code' = 'RM' AND 'Date' LIKE '%{0}_{1}'".format(month, year)
cursor.execute(query)
In this way, 'query' variable value will be:
"SELECT * FROM 'table' WHERE 'Code' = 'RM' AND 'Date' LIKE '%05_1999'"
For more information about string formatting, let's have a look to Python String Formatting Best Practices - Real Python

retrieving data from table based on a value from a python variable

I am writing a function that will retrieve data from sqlite table based on the parameters user provide. This is the function so far
def database_retrieve(db_file, id):
try:
conn = sqlite3.connect(db_file)
with conn:
sql_command = "SELECT * FROM my_table WHERE id = "+id
cur = conn.cursor()
cur.execute(sql_command)
result = cur.fetchall()
return result
except Exception as e:
print(e)
db_file = 'testdb.db'
print(database_retrieve(db_file, 'subject1'))
This gives me the following error
no such column: subject1
None
When I add subject1, which is an entry under the id column in my_table, directly to the sql command like this
sql_command = "SELECT * FROM my_table WHERE id = 'subject1'"
it works fine and prints all the data.
I am new to sqlite3. Please help. Thanks in advance
These are the links I used to come this far
Python sqlite3 string variable in execute
https://www.dummies.com/programming/databases/how-to-retrieve-data-from-specific-rows-in-mysql-databases/
When you do this
sql_command = "SELECT * FROM my_table WHERE id = "+id
The value of sql_command is
"SELECT * FROM my_table WHERE id = subject1"
As you can see, subject1 is not in quotes. sqlite thinks it is a column, that's why you see that error.
Instead, do this
sql_command = "SELECT * FROM my_table WHERE id = ?"
cur.execute(sql_command, [id])
? acts as a placeholder for the variable id.
The official sqlite3 documentation mentions few others methods
https://docs.python.org/2/library/sqlite3.html
The sql_command string being generated should be something like this (Formatted string):
sql_command = "SELECT * FROM my_table WHERE id = %s AND name = %s" % (212212, 'shashank')

Python: display first element of tuple in cursor

I have the following code:
conn = mysql.connector.connect(database='test', user='me', password='pwd')
cursor = conn.cursor()
query = ( "select id from T where project = 10" )
cursor.execute(query)
result = cursor.fetchall()
result is showing as:
[(Decimal('476749'),), (Decimal('478045'),), (Decimal('479713'),)]
is it possible to show it as : [476749, 478045, 479713]
Why use zip/map when python is so much more elegant?
[int(i[0]) for i in cursor.fetchall()]
You can use zip function to get the first items and map to convert the decimals to integer:
>>> import decimal
>>> map(int,zip(*[(decimal.Decimal('476749'),), (decimal.Decimal('478045'),), (decimal.Decimal('479713'),)])[0])
[476749, 478045, 479713]
And in your code :
result = map(int,zip(*cursor.fetchall()))

Categories