pjunited
pjunited

Reputation: 11

pyodbc in Python to run an existing access query that asks for user input

I have an existing access query for example called *RunExampleQuery:

select name from table_x where date = [start];

But I can't seem to find the sql code that will run this query i.e

sql = """SELECT * FROM *RunExampleQuery WHERE [start] = ?"""

params = (datetime.date(2016,11,25))

cursor.execute(sql,params)

Thanks for your help in advance.

Upvotes: 1

Views: 581

Answers (1)

Parfait
Parfait

Reputation: 107727

Couple of items are the issue:

  1. [start] field must exist in RunExampleQuery in order to use it in WHERE clause
  2. To bind parameters to prepared SQL statements, you must pass values in tuple or list. This requires converting scalar strings to these type:

RunExampleQuery

select name, [start] from table_x;

Tuple Parameterization

sql = """SELECT * FROM [RunExampleQuery] WHERE [start] = ?"""
params = datetime.date(2016,11,25)
cursor.execute(sql, (params,))

List Parameterization

sql = """SELECT * FROM [RunExampleQuery] WHERE [start] = ?"""
params = datetime.date(2016,11,25)
cursor.execute(sql, [params])

Upvotes: 1

Related Questions