> On 24 Dec 2014, at 10:50am, Jim Carroll <j...@carroll.com> wrote:
> 
> #!/usr/bin/env python
> import sqlite3 as sq

Sorry, I may have just realised what's wrong.  You are misusing your python 
library.  See the documentation at

<https://docs.python.org/2/library/sqlite3.html>

You cannot always use .execute against the connection to the database.  You 
need to create a Cursor to the connection first, and use .execute against that 
as documented in 11.13.3.  Your code works fine here

> db = sq.connect(':memory:')
> db.execute('CREATE TABLE tbl (col INTEGER)')
> db.execute('CREATE TABLE tbl2 (col INTEGER)')
> db.executemany('INSERT INTO tbl (col) VALUES (?)', [(0,), (1,), (2,)])
> db.commit()

because there is only ever one thing accessing the connection and, as noted in 
11.13.7.1 you can do this as a shortcut.  However, in your later code

> for col in db.execute('SELECT col FROM tbl'):
>    print(col)
>    db.execute('INSERT INTO tbl2 VALUES (?)', col)
>    db.commit()

You are trying to use the connection to the database for two cursors at the 
same time.  One is the iteration through the SEELCT, the other is the INSERT 
command.  The python library can't handle this and gets confused.  You should 
instead be doing like

cursorSelect = conn.cursor()
cursorInsert = conn.cursor()
for col in cursorSelect.execute('SELECT col FROM tbl'):
   print(col)
   cursorInsert.execute('INSERT INTO tbl2 VALUES (?)', col)
   cursorInsert.commit()

I don't know python, and I cannot test the above code but it might be enough to 
point you in the right direction for an eventual solution.

Simon.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to