[Tutor] sqlite3 place holder problem

Alex Kleider akleider at sonic.net
Fri Oct 4 04:14:44 CEST 2013


On 2013-10-03 19:07, bob gailer wrote:
> On 10/3/2013 9:52 PM, Alex Kleider wrote:
>> The following class method
>> """
>> 
>>     def insert(self, DataBase=DataBase, Table=Table):
>>         """
>>         Insert instance of ClassMate into <DataBase> <Table>.
>>         """
>>         with sqlite3.connect(DataBase) as con:
>>             cur = con.cursor()
>>             row = \
>>             ('NULL', self.first, self.last, self.partner,
>>             self.address, self.phone, self.email, )
>>             directive = "INSERT INTO %s VALUES ?;" % (Table, )
>>             cur.execute(directive, row)
>> """
>> gives the following error:
>> 
>> """
>> Traceback (most recent call last):
>>   File "./v_temp.py", line 155, in <module>
>>     go_on.insert()
>>   File "./v_temp.py", line 70, in insert
>>     cur.execute(directive, row)
>> sqlite3.OperationalError: near "?": syntax error
>> """
>> (explanation: 'go_on' is an instance of my class in the __main__)
>> 
>> It was working fine when I was using string formatting but I can't 
>> seem to get the syntax right using the qmark method which the 
>> documentation suggests is the better way to do it.
>> 
>> Can anyone spot what should be changed?

> Try: directive = "INSERT INTO %s VALUES (?,?,?,?,?,?,?);" % (Table, )

>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>> 

Thanks, I discovered that when I went I read further in the 
documentation.
This is the part that lead me a stray:
"""
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
              ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
              ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
             ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
"""
I made the incorrect assumption that individual question marks 
represented tuples, (as they do here with 'executemany') but I guess not 
so with 'execute'.


More information about the Tutor mailing list