I'm trying to get the query to return a value but I get None!
selectstatement = "SELECT * FROM customer Where (('ID' =" +"%s"
data = 12345
cursor.execute(selectstatement, (data,))
records = cursor.fetchone()
print(records[0])
Now I know it exists in the table but I get the error:
Traceback (most recent call last):
File "C:\Users\yfevrier\Desktop\EnterRegister.py", line 86, in <module>
StartClean()
File "C:\Users\yfevrier\Desktop\EnterRegister.py", line 28, in StartClean
Analyze(DataReader)
File "C:\Users\yfevrier\Desktop\EnterRegister.py", line 66, in Analyze
print(records[0])
TypeError: 'NoneType' object is not subscriptable
If i do this:
selectstatement = "SELECT * FROM customer
cursor.execute(selectstatement)
records = cursor.fetchall()
I get all the results but I can't find the resource to specify the where clause perfectly.
You are comparing the string ID to the passed value. This is the correct way:
selectstatement = "SELECT * FROM customer Where ID = %s"
But if the ID column name was created in upper case and wrapped in double quotes then it is necessary to always use double quotes:
selectstatement = '''SELECT * FROM customer Where "ID" = %s'''
Related
i have script to retrieve data stored in text file, then use variable query ${} to parse the data.
example:
data kept in text file is abc
below statement will execute query productId = 'abc'
Now, I want to append defined value after the abc. to make the query like below:
productId = 'abc/NDC-1111'
what should be the exact syntax i need to use?
//Read productId
def productId = new File(RunConfiguration.getProjectDir() + "/Data Files/productId.txt")
//SQL statement
dbQuery2 = /SELECT * FROM db.t1 where productId = '${productId.text}'/
You can just do:
dbQuery2 = "SELECT * FROM db.t1 where productId = ${"$productId.text/NDC-1111"}"
I am trying to make a query for my PostgreSQL database.
I think the format of my query is wrong and I can't seem to get it to work, I have posted my code below:
query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table"
WHERE "GENE_NAME" LIKE %(genename)s AND "RESIDUE" LIKE %(location)s')
The aim is to take the kinase name is the gene name and location match.
my error message appears as the following:
ProgrammingError Traceback (most recent call last)
<ipython-input-33-9eae43b913d6> in <module>()
35 cur = connection.cursor()
36
---> 37 query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table" WHERE "GENE_NAME" LIKE%(genename)s AND "RESIDUE" LIKE %(location)s')
Thanks!
Connor
Don't use string operations to build SQL queries. Use the proper %s syntax.
genname = "foo"
location = "bar"
cur.execute("SELECT ... LIKE %s and ... LIKE %s", (genname, location))
No quotes around the values must be used. The quoting will be done by the DB API library.
def main(inputs, output):
sdf = spark.read.csv(inputs, schema=observation_schema)
sdf.registerTempTable('filtertable')
result = spark.sql("""
SELECT * FROM filtertable WHERE qflag IS NULL
""").show()
temp_max = spark.sql(""" SELECT date, station, value FROM filtertable WHERE (observation = 'TMAX')""").show()
temp_min = spark.sql(""" SELECT date, station, value FROM filtertable WHERE (observation = 'TMIN')""").show()
result = temp_max.join(temp_min, condition1).select(temp_max('date'), temp_max('station'), ((temp_max('TMAX')-temp_min('TMIN'))/10)).alias('Range'))
Error:
Traceback (most recent call last):
File "/Users/syedikram/Documents/temp_range_sql.py", line 96, in <module>
main(inputs, output)
File "/Users/syedikram/Documents/temp_range_sql.py", line 52, in main
result = temp_max.join(temp_min, condition1).select(temp_max('date'), temp_max('station'), ((temp_max('TMAX')-temp_min('TMIN')/10)).alias('Range'))
AttributeError: 'NoneType' object has no attribute 'join'
Performing on join operation gives me Nonetype object error. Looking online didn't help as there is little documentation online for pyspark sql.
What am I doing wrong here?
Remove the .show() from temp_max and temp_min because show only prints a string and does not return anything (hence you get AttributeError: 'NoneType' object has no attribute 'join').
I'm using psycopg2 to access a PostgreSQL database through Python 3, and I'm attempting to make a query where I want to select all users whose name are in a list, if the list is not empty. If the provided list is empty, I want to ignore the condition, i.e. select all users regardless of their name.
I've already tried the following three calls:
# Using list
cursor.execute(
"SELECT age FROM user WHERE %(names) = '{}' OR user.name IN %(names)s",
{'names': []},
)
# Using tuple
cursor.execute(
"SELECT age FROM user WHERE %(names) = () OR user.name IN %(names)s",
{'names': ()},
)
# Using both list and tuple
cursor.execute(
"SELECT age FROM user WHERE %(names_l) = '{}' OR user.name IN %(names_t)s",
{'names_l': [], 'names_t': ()},
)
But they all raise an invalid syntax error from one point or another:
# Using list
psycopg2.ProgrammingError: syntax error at or near "'{}'"
LINE 17: user.name IN '{}'
# Using tuple
psycopg2.ProgrammingError: syntax error at or near ")"
LINE 16: () == ()
# Using both list and tuple
psycopg2.ProgrammingError: syntax error at or near ")"
LINE 17: user.name IN ()
For optional parameters you want a SQL where clause like:
where column = :parameter or :parameter is null
With the above when the parameter is null all rows will be returned otherwise only those meeting the condition.
Psycopg adapts a Python list to a Postgresql array. To check if any of the Postgresql array values is equal to a certain value:
where column = any (array[value1, value2])
To get a Python None, which is adapted to a Postgresql null, from an empty Python list:
parameter = [] or None
Passing a dictionary to the cursor.execute method avoids parameter repetition in the parameters argument:
names = ['John','Mary']
query = """
select age
from user
where user.name = any (%(names)s) or %(names)s is null
"""
print (cursor.mogrify(query, {'names': names or None}).decode('utf8'))
#cursor.execute(query, {'names': names or None})
Output:
select age
from user
where user.name = any (ARRAY['John', 'Mary']) or ARRAY['John', 'Mary'] is null
When the list is empty:
select age
from user
where user.name = any (NULL) or NULL is null
http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries
My SQL query looks like this:
product = 'Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)'
retailer = 'Target'
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
cur.execute(query)
When i execute that i get a error saying:
psycopg2.ProgrammingError: syntax error at or near "Basic"
I am not sure why my syntax is wrong
Your statement;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
...builds a complete string from the query and parameters without any quoting around your strings, which makes the entire string invalid SQL which fails at execute;
SELECT * FROM product_info
WHERE product_name = Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)
AND retailer = Target
What you're probably trying to do is parameterizing your query which is instead done in execute by passing the parameters in a tuple;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s"""
...
cur.execute(query, (product, retailer))