I am trying to insert records to a redshift table using a lambda function. I am using
boto3.client('redshift-data')
for the same. Now I have the query as below.
query1 = "insert into dbname.tablename values('aaaa','bbbb','cccc')"
response = rsclient.execute_statement(
ClusterIdentifier='xxxxx',
Database='yyyy',
DbUser='zzzz',
Sql= query1,
StatementName='examplestatement'
)
This works fine. But I want to pass variables here instead of values. For instance,
var1 = 'aaaa'
var2 = 'bbbb'
var3 = 'cccc'
Then try the query as below but it it doesn't work, I think it something silly to do with quotes.
query1 = "insert into dbname.tablename values(var1,var2,var3)"
How can I achieve this. I write lambda function using python3. Any help is appreciated.
You can use f-strings:
query1 = f"insert into dbname.tablename values('{var1}','{var2}','{var3}')"
Related
I am trying to use the following SQL query through pyodbc:
SELECT * FROM table WHERE variable1 LIKE '%test1%' and variable2 LIKE '%test2%'
I found a way to do it for a single parameters on the link
filter = 'test1'
sql = "SELECT * FROM table WHERE variable1 LIKE ?"
param = f'%{filter}%'
rows = cursor.execute(sql, param).fetchall()
Can you please help me to write the SQL query on pyodbc?
You may go with the same approach of your example, just with 2 parameters:
filter1 = 'test1'
filter2 = 'test2'
sql = "SELECT * FROM table WHERE variable1 LIKE ? AND variable2 LIKE ?"
params = (f'%{filter1}%',f'%{filter2}%')
rows = cursor.execute(sql, params).fetchall()
Or simplify it a bit:
filter1 = 'test1'
filter2 = 'test2'
sql = f"SELECT * FROM table WHERE variable1 LIKE %{filter1}%? AND variable2 LIKE %{filter2}%?"
rows = cursor.execute(sql).fetchall()
Have you tried this? It's related to named parameters binding and should support any arbitrary number or params.
I'm trying to create a dynamic PostgreSQL query in Python:
import psycopg2
import pandas as pd
V_ID=111
conn = psycopg2.connect(host="xxxx", port = 5432, database="xxxx", user="xxxxx", password="xxx")
df= pd.read_sql_query
("""SELECT u.user_name, sum(s.sales_amount)
FROM public.users u left join public.sales s on u.id=s.user_id
WHERE USER_ID = v_ID
Group by u.user_name""",con=conn)
df.head()
When I set a fixed ID then a query ran fine, but if I set a variable "V_ID", then get an error.
Help me please, how to properly place a variable within a query...
You can use string formatting to pass the value in the query string. You can read more about string formatting here: https://www.w3schools.com/python/ref_string_format.asp
v_ID = "v_id that you want to pass"
query = """SELECT u.user_name, sum(s.sales_amount)
FROM public.users u left join public.sales s on u.id=s.user_id
WHERE USER_ID = {v_ID}
Group by u.user_name""".format(v_ID=v_ID)
df= pd.read_sql_query(query ,con=conn)
As mentioned by #Adrian in the comments, string formatting is not the right way to do this.
More details here: https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
As per the docs, params can be list, tuple or dict.
The syntax used to pass parameters is database driver dependent. Check
your database driver documentation for which of the five syntax
styles, described in PEP 249’s paramstyle, is supported. Eg. for
psycopg2, uses %(name)s so use params={‘name’ : ‘value’}.
Here is how you can do this in the case of psycopg2.
v_ID = "v_id that you want to pass"
query = """SELECT u.user_name, sum(s.sales_amount)
FROM public.users u left join public.sales s on u.id=s.user_id
WHERE USER_ID = %(v_ID)s
Group by u.user_name"""
df= pd.read_sql_query(query ,con=conn, params={"v_ID":v_ID})
I am working in Python/Django with a MySQL Db. This sql query works fine in my Workbench
SELECT * FROM frontend_appliance_sensor_reading
WHERE id = (SELECT max(id) FROM frontend_appliance_sensor_reading WHERE sensor_id = hex(x'28E86479972003D2') AND
appliance_id = 185)
I am returning the latest record for a sensor. The hex string is the sensor ID that I need to pass in as a variable in python.
Here is my python function that would return this object
def get_last_sensor_reading(appliance_id, sensor_id):
dw_conn = connection
dw_cur = dw_conn.cursor()
appliance_id_lookup = appliance_id
dw_cur.execute('''
SELECT * FROM frontend_appliance_sensor_reading as sr
WHERE id = (SELECT max(id) FROM frontend_appliance_sensor_reading WHERE sensor_id = hex(x{sensor_id_lookup}) AND
appliance_id = {appliance_id_lookup})
'''.format(appliance_id_lookup=appliance_id_lookup, sensor_id_lookup = str(sensor_id)))
values = dw_cur.fetchall()
dw_cur.close()
dw_conn.close()
print values
However it seems to concat the x with the variable like this:
(1054, "Unknown column 'x9720032F0100DE86' in 'where clause'")
I have tried various string combinations to get it to execute correctly with no luck. What am I missing? Does the x actually get interpreted as a str? Or should I be converting it to something else prior?
Also, I cannot not use Django's ORM for this as the sensor id is stored in a BinaryField as BLOB data. You cannot filter by BLOB data in Django. This is the reason I am using a sql command instead of just doing SensorReading.objects.filter(sensor_id = sensor).latest('id)
number_tuple = (1,4,6,3)
sensex_quaterly_df = psql.sqldf("SELECT * FROM sensex_df
WHERE 'Num' IN ('number_tuple')")
"HERE number_tuple has the values that I want to retrieve from sensex_df database"
Because pandasql allows you to run SQL on data frames, you can build SQL with concatenated values of tuple into comma-separated string using string.join().
number_tuple = (1,4,6,3)
in_values = ", ".join(str(i) for i in number_tuple)
sql = f"SELECT * FROM sensex_df WHERE Num IN ({in_values})"
sensex_quaterly_df = psql.sqldf(sql)
However, concatenated SQL strings is not recommended if you use an actual relational database as backend. If so, use parameterization where you develop a prepared SQL statement with placeholders like %s of ? and in subsequent step binding values. Below demonstrates with pandas read_sql:
number_tuple = (1,4,6,3)
in_values = ", ".join('?' for i in number_tuple)
sql = f"SELECT * FROM sensex_df WHERE Num IN ({in_values})"
sensex_quaterly_df = pd.read_sql(sql, conn, params=number_tuple)
Let's say we have the following sql statement:
INSERT INTO myTable VALUES (1,"test")
In my python code the first value is either an integer or NULL. How can I insert NULL value using python code?
Code snippet:
var1 = getFirstValue()
var2 = getSecondValue()
qry = "INSERT INTO myTable VALUES (%d,%s)" % (var1,var2)
Whenever var1 is None it is throwing error, but I want NULL to be inserted.
Since you marked this question with the tag Django, you must be aware that you don't just write queries and save them in the Database, Django handles this.
Just check the Tutorial that is available here : https://docs.djangoproject.com/en/1.6/intro/tutorial01/
Since you mentioned Sybase, you must get the Django driver from (https://github.com/sqlanywhere/sqlany-django) and modify the DATABASES entry inside your settings.py project. (first finish the tutorial)
You can use:
qry = "INSERT INTO myTable VALUES ({0}, {1})".format(var1, var2)
Here is one possible option:
var1 = getFirstValue()
var2 = getSecondValue()
var2 = "'{0}'".format(var2) if var2 is not None else 'NULL'
qry = "INSERT INTO myTable VALUES ({0},{1})".format(var1,var2)