1

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.

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

3 Comments

So [start] comes from a column in table_x called date. It's asking for user input in the MS Access Query to enter a date. So when I'm calling it in from the sql portion would it be sql = """SELECT * FROM [RunExampleQuery] WHERE date.[start] = ?"""
You cannot run a user prompt query like in MSAccess.exe. Have user enter through Python using input() and pass that as query parameter.
Thanks Parfiat. I didn't want to do that because the query is used by different groups whom will not be using my python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.