John K Masters
johnmasters at oxtedonline.net
Tue Jul 17 23:47:33 CEST 2007
On 17:30 Tue 17 Jul , Carsten Haese wrote:
> 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.
Thanks, I'll try that tomorrow. Number of columns are known and static;
I'm extracting the data from a report generated by an old DOS program. I
had been converting it to CSV and plugging it into Open Office Calc but
it is now getting beyond that and I need to do a bit more with the data.
War is God's way of teaching Americans geography
Ambrose Bierce (1842 - 1914)
More information about the Python-list