sqlite error?

Discussion in 'Python' started by John Salerno, Nov 6, 2006.

  1. John Salerno

    John Salerno Guest

    Am I using the ? placeholder wrong in this example?


    t = ('hi', 'bye')

    self.connection.execute("INSERT INTO Personal (firstName, lastName)
    VALUES ?", t)



    Traceback (most recent call last):
    File "C:\Python25\myscripts\labdb\dbapp.py", line 93, in OnSaveRecord
    self.save_to_database(textfield_values)
    File "C:\Python25\myscripts\labdb\dbapp.py", line 97, in save_to_database
    self.connection.execute("INSERT INTO Personal (firstName, lastName)
    VALUES ?", t)
    sqlite3.OperationalError: near "?": syntax error
     
    John Salerno, Nov 6, 2006
    #1
    1. Advertising

  2. >> self.connection.execute("INSERT INTO Personal (firstName, lastName)
    VALUES ?", t)

    John,

    I'm no expert, but try

    self.connection.execute("INSERT INTO Personal (firstName, lastName)
    VALUES ?, ?", t)
     
    BartlebyScrivener, Nov 6, 2006
    #2
    1. Advertising

  3. John Salerno

    Guest

    John Salerno wrote:
    > Am I using the ? placeholder wrong in this example?
    >
    >
    > t = ('hi', 'bye')
    >
    > self.connection.execute("INSERT INTO Personal (firstName, lastName)
    > VALUES ?", t)
    >
    >
    >
    > Traceback (most recent call last):
    > File "C:\Python25\myscripts\labdb\dbapp.py", line 93, in OnSaveRecord
    > self.save_to_database(textfield_values)
    > File "C:\Python25\myscripts\labdb\dbapp.py", line 97, in save_to_database
    > self.connection.execute("INSERT INTO Personal (firstName, lastName)
    > VALUES ?", t)
    > sqlite3.OperationalError: near "?": syntax error


    I believe you're missing the parens around your VALUES to insert. Also,
    you need 1 placeholder per argument inserted, not just one for the
    entire argument list. Try:

    self.connection.execute("INSERT INTO Personal (firstName, lastName)
    VALUES (?, ?)", t)

    HTH
     
    , Nov 6, 2006
    #3
  4. John Salerno

    John Salerno Guest

    wrote:
    > John Salerno wrote:
    >> Am I using the ? placeholder wrong in this example?
    >>
    >>
    >> t = ('hi', 'bye')
    >>
    >> self.connection.execute("INSERT INTO Personal (firstName, lastName)
    >> VALUES ?", t)
    >>
    >>
    >>
    >> Traceback (most recent call last):
    >> File "C:\Python25\myscripts\labdb\dbapp.py", line 93, in OnSaveRecord
    >> self.save_to_database(textfield_values)
    >> File "C:\Python25\myscripts\labdb\dbapp.py", line 97, in save_to_database
    >> self.connection.execute("INSERT INTO Personal (firstName, lastName)
    >> VALUES ?", t)
    >> sqlite3.OperationalError: near "?": syntax error

    >
    > I believe you're missing the parens around your VALUES to insert. Also,
    > you need 1 placeholder per argument inserted, not just one for the
    > entire argument list. Try:
    >
    > self.connection.execute("INSERT INTO Personal (firstName, lastName)
    > VALUES (?, ?)", t)
    >
    > HTH
    >


    Thanks guys. I'll try this. I thought the ? stood for the whole tuple.
     
    John Salerno, Nov 6, 2006
    #4
  5. John Salerno wrote:
    > >> Am I using the ? placeholder wrong in this example?
    > >>
    > >>
    > >> t = ('hi', 'bye')
    > >>
    > >> self.connection.execute("INSERT INTO Personal (firstName, lastName)
    > >> VALUES ?", t)
    > >>

    [snip]
    >
    > Thanks guys. I'll try this. I thought the ? stood for the whole tuple.


    Definitely not. You could have a sql command like this -

    cur.execute("UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? AND
    col4 = ?",(1,2,3,4))

    The parameters could be scattered throughout the command. Therefore the
    substitution is one-for-one from left to right using the values in the
    tuple.

    Frank Millman
     
    Frank Millman, Nov 7, 2006
    #5
  6. John Salerno

    John Salerno Guest

    Frank Millman wrote:

    > Definitely not. You could have a sql command like this -
    >
    > cur.execute("UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? AND
    > col4 = ?",(1,2,3,4))
    >
    > The parameters could be scattered throughout the command. Therefore the
    > substitution is one-for-one from left to right using the values in the
    > tuple.
    >


    Thanks! The example I was looking at in the docs didn't use parentheses,
    but I also didn't connect that with the fact that it was only using a
    one-tuple! :)
     
    John Salerno, Nov 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. achan

    sqlite import error

    achan, Nov 12, 2003, in forum: Python
    Replies:
    1
    Views:
    413
    Dennis Lee Bieber
    Nov 12, 2003
  2. Greg Miller

    sqlite utf8 encoding error

    Greg Miller, Nov 17, 2005, in forum: Python
    Replies:
    8
    Views:
    7,695
    Greg Miller
    Nov 21, 2005
  3. Fred

    Re: SQLITE Blob writing error

    Fred, Nov 14, 2008, in forum: C Programming
    Replies:
    3
    Views:
    313
    Ben Bacarisse
    Nov 14, 2008
  4. Carl Youngblood
    Replies:
    1
    Views:
    231
    Carl Youngblood
    Apr 9, 2005
  5. Replies:
    4
    Views:
    350
Loading...

Share This Page