Ryan Baugh
Ryan Baugh

Reputation: 21

Python pyodbc is not recognizing parameter marker. I've supplied 2 markers and two values but its not recognizing them

Any idea why the code below would not recognize the first placeholder? I'm assuming I have to put a special character in front of it but i've been unable to find any documentation around it. I've also tried just a simple "create table ?" with no success.

for champ in champion_list:
    UPDATE_SQL = """\
        if not exists (select * from sysobjects where name=? and xtype='U')
            CREATE TABLE [dbo].[?](
                [champId] [varchar](50) NOT NULL,
                [championName] [varchar] NOT NULL,
                [version] [varchar](50) NOT NULL
            ) ON [PRIMARY]
     """
    values=(champ,champ)
    try:
        cursorprod.execute(UPDATE_SQL, values)
        print str(champ),'table added.'
    except Exception as e:
        print(e)

I get the error

The SQL contains 1 parameter markers, but 2 parameters were supplied

Upvotes: 2

Views: 630

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123654

Query parameters are for specifying column values in DML statements; they cannot be used to specify object (e.g., column or table) names in DDL statements. You will need to use dynamic SQL (string substitution) for that ...

... assuming that you really want to create separate tables for each item in the list. If the structure of those tables is identical then that is a bad design. You'd be better served with one table that includes an extra column to identify the list item associated with each row.

Upvotes: 2

Related Questions