I'm trying to read a file with this argument {year} inside it.
Inside this file there is this string:
SELECT * FROM TABLE WHERE YEAR = {year}
I'd like to read this file with Python f-strings to use the query after.
The expected result looks like this:
SELECT * FROM TABLE WHERE YEAR = 2019
I tried this:
year = 2019
with open("test.sql") as query_file:
query = query_file.read()
print(query)
But the output was SELECT * FROM TABLE WHERE YEAR = {year} instead of SELECT * FROM TABLE WHERE YEAR = 2019
I have no idea how I can put the year variable to replace the {year} inside the file.
Use str.format to replace the {year}.
f-strings are literals and must be an expression. Python will not replace data in string, just because there is a variable of the same name in the bracket notation.
query = 'SELECT * FROM TABLE WHERE YEAR = {year}'
query.format(year=2019)
Edit:
To replace values in a SQL query it's better and more secure to use prepared statements like:
c = db.cursor()
year = 2019
c.execute("SELECT * FROM TABLE WHERE YEAR = %s;", (year,))
See also the examples of MySQLdb
Related
I have the following SQL statement where i am reading the database to get the records for 1 day. Here is what i tried in pgAdmin console -
SELECT * FROM public.orders WHERE createdat >= now()::date AND type='t_order'
I want to convert this to the syntax of psycopg2but somehow it throws me errors -
Database connection failed due to invalid input syntax for type timestamp: "now()::date"
Here is what i am doing -
query = f"SELECT * FROM {table} WHERE (createdat>=%s AND type=%s)"
cur.execute(query, ("now()::date", "t_order"))
records = cur.fetchall()
Any help is deeply appreciated.
DO NOT use f strings. Use proper Parameter Passing
now()::date is better expressed as current_date. See Current Date/Time.
You want:
query = "SELECT * FROM public.orders WHERE (createdat>=current_date AND type=%s)"
cur.execute(query, ["t_order"])
If you want dynamic identifiers, table/column names then:
from psycopg2 import sql
query = sql.SQL("SELECT * FROM {} WHERE (createdat>=current_date AND type=%s)").format(sql.Identifier(table))
cur.execute(query, ["t_order"])
For more information see sql.
I want get variable with sql query:
Dt = spark.sql("""select max(dt) from table""")
Script = """select * from table where dt > """ + dt
Spark.sql(script)
but when I try to substitute a variable in the request I get error:
"Can only concatenate str (not dataframe) to str"
How do I get the variable as a string and not a dataframe?
To get the result in a variable, you can use collect() and extract the value. Here's an example that pulls the max date-month (YYYYMM) from a table and stores it in a variable.
max_mth = spark.sql('select max(mth) from table').collect()[0][0]
print(max_mth)
# 202202
You can either cast the value to string in the sql statement, or use str() on the variable while using to convert the integer value to string.
P.S. - the [0][0] is to select the first row-column
I'm trying to get the SQLlite3 variable substitution in Python working, but I always get the error:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.
I've tried:
date_range = '10'
with sqlite3.connect(DATABASE_LOGIN) as connection:
cursor = connection.cursor()
template = "SELECT DISTINCT date FROM schedule WHERE date BETWEEN DATETIME('NOW') AND DATETIME('NOW', '-? DAYS') ORDER BY date"
cursor.execute(template, date_range)
and I know it's very bad form to use:
date_range = '10'
with sqlite3.connect(DATABASE_LOGIN) as connection:
cursor = connection.cursor()
template = f"SELECT DISTINCT date FROM schedule WHERE date BETWEEN DATETIME('NOW') AND DATETIME('NOW', '-{date_range} DAYS') ORDER BY date"
cursor.execute(template)
but for some reason the former query doesn't work.
You must concatenate the placeholder ?:
SELECT DISTINCT date
FROM schedule
WHERE date BETWEEN DATETIME('NOW') AND DATETIME('NOW', '-' || ? || ' DAY') ORDER BY date
I want to use a WHERE statement with two variables within the where clause. I've done research on this looking at how to use variables in SQL statements in Databricks and Inserting Variables Using Python, Not Working. I've tried to implement the solutions provided but it's not working.
a= 17091990
b = 30091990
df = spark.sql(' SELECT * FROM table WHERE date between "a" AND "b" ')
You can use python's formatted string literals
df = spark.sql(f"SELECT * FROM table WHERE date between {a} AND {b} ")
For more about formatted string literals you can refer to https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498
I am trying to use cx_Oracle to query a table in oracle DB (version 11.2) and get rows with values in a column between a datetime range.
I have tried the following approaches:
Tried between clause as described here, but cursor gets 0 rows
parameters = (startDateTime, endDateTime)
query = "select * from employee where joining_date between :1 and :2"
cur = con.cursor()
cur.execute(query, parameters)
Tried the TO_DATE() function and Date'' qualifiers. Still no result for Between or >= operator. Noteworthy is that < operator works. I also got the same query and tried in a sql client, and the query returns results. Code:
#returns no rows:
query = "select * from employee where joining_date >= TO_DATE('" + startDateTime.strftime("%Y-%m-%d") + "','yyyy-mm-dd')"
cur = con.cursor()
cur.execute(query)
#tried following just to ensure that some query runs fine, it returns results:
query = query.replace(">=", "<")
cur.execute(query)
Any pointers about why the between and >= operators are failing for me? (my second approach was in line with the answer in Oracle date comparison in where clause but still doesn't work for me)
I am using python 3.4.3 and used cx_Oracle 5.3 and 5.2 with oracle client 11g on windows 7 machine
Assume that your employee table contains the field emp_id and the row with emp_id=1234567 should be retrieved by your query.
Make two copies of your a program that execute the following queries
query = "select to_char(:1,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(joining_date,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(:2,'YYYY-MM-DD HH24:MI:SS') resultstring from employee where emp_id=1234567"
and
query="select to_char(joining_date,'YYYY-MM-DD HH24:MI:SS')||' >= '||to_char(TO_DATE('" + startDateTime.strftime("%Y-%m-%d") + "','yyyy-mm-dd'),'YYYY-MM-DD HH24:MI:SS') resultstring from employee where emp_id=1234567"
Show us the code and the value of the column resultstring
You are constructing SQL queries as strings when you should be using parameterized queries. You can't use parameterization to substitute the comparison operators, but you should use it for the dates.
Also, note that the referenced answer uses the PostgreSQL parameterisation format, whereas Oracle requires you to use the ":name" format.