views:

34

answers:

3

I have a python3 program that I'm making which uses a sqlite database with several tables, I want to create a selector module to allow me to chose which table to pull data from.

I have found out that I can't use paramater substitution for a table name as shown bellow, so I'm looking for some alternative methods to accomplish this.

c.execute("SELECT * FROM ? ", DB)

Any ideas?

+1  A: 

Right. You can not use parameter substitution to specify the table. So instead you must do string manipulation:

c.execute("SELECT * FROM {t} ".format(t=tablename))
unutbu
+1  A: 

I don't know if this is a python3 thing but it seems easiest to just do this:

c.execute("SELECT * FROM %s "% tablename)
Joshkunz
No it is a sqlite thing, and it is pretty typical for most db interfaces. Query parameters are usually just for plugging in in place of literal values, in INSERT or WHERE clauses, not for table or column references.
Paul McGuire
By python3 I means I'm using python version 3. I specidfied because the syntax is a little different from version 2.
Higbasa
Paul is right though, which is why i'm looking for an alternate course of action. so far all I have come up with is a nested if statment each with its own sqlite query.
Higbasa
The problem I have with this however is I wanted my app to be more dynamic and allow me to continue adding databases without having to modify the python code every time. Any insight would be appreciated.
Higbasa
add more tables, not databases :s
Higbasa
A: 

Blockquote * Right. You can not use parameter substitution to specify the table. So instead you must do string manipulation: c.execute("SELECT * FROM {t} ".format(t=tablename))* Blockquote

Thanks unutbu, this is just what I needed.

Higbasa
You might want to accept his answer by clicking the check underneath it. That way he'll get the credit.
Joshkunz