Re: sqlite tuples

Discussion in 'Python' started by Carsten Haese, Jul 17, 2007.

  1. On Tue, 2007-07-17 at 21:49 +0100, John K Masters wrote:
    > I am fairly new to Python and am trying to get to grips with pysqlite2.
    > >From what I have read data is returned as a list of tuples when using

    > SELECT via connection.cursor. But I have not, despite frantic googling,
    > found how to INSERT a list of tuples into a sqlite table. If I convert
    > the tuple to a string and concatenate it to the 'INSERT INTO table etc.'
    > string then it works, but only if all the tuple values are strings and
    > then only if all the table fields are of type TEXT.
    >
    > Is it possible to, and if so how can one, insert a list of tuples into a
    > sqlite table?


    Assuming that each tuple is the same length and goes into the same table
    and columns, something like this will do the trick:

    sql = "insert into tablename(column1,column2,column3) values(?,?,?)"
    cursor.executemany(sql, list_of_tuples)

    If you don't know the number of columns at design time, you'll need to
    fill the values(...) clause on the fly with the correct number of
    question marks, and you'll have to build the list of column names
    somehow, too, but the basic idea is the same.

    HTH,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Jul 17, 2007
    #1
    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. Replies:
    5
    Views:
    563
    Thomas J. Gritzan
    Oct 6, 2006
  2. John K Masters

    sqlite tuples

    John K Masters, Jul 17, 2007, in forum: Python
    Replies:
    0
    Views:
    301
    John K Masters
    Jul 17, 2007
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    596
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    738
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    245
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page