Re: [pysqlite] How To Insert A Variable Number of Values
Roger Binns <[email protected]> Fri, 30 May 2008 20:14:25 -0700
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Rich Shepard wrote:
> Now there should be 3 values for each row at this stage. How can I write
> the SQL stmt so that the row/column cells with values are written to the
> database table?
You could create statements as appropriate but it is a pain. I don't
know if pysqlite lets you supply a dictionary as the bindings, but apsw
does. For example:
>>> import apsw
>>> c=apsw.Connection("mydb").cursor()
>>> c.execute("create table foo(comp, var, curr1, curr2, curr3, curr4)")
>>> c.execute("insert into foo (comp, var, curr1, curr2, curr3, curr4)
values(:comp, :var, :curr1, :curr2, :curr3, :curr4)", {"comp": 1,
"curr3": 2})
You can just supply the dictionary as the second parameter to execute
with whatever values you do have. Missing ones will be treated as
NULL/None.
Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFIQMKRmOOfHg372QQRAtVHAJ9PSoBH+9Yh/Pdp6vpvrRZVKPlOmACfbLU7
19+si/Bnx/ED3DdL7V+mEn0=
=GHPV
-----END PGP SIGNATURE-----