472,363 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,363 software developers and data experts.

variable expansion with sqlite

Hi I'm using SQlite and the CSV module and trying to create a class
that converts data from CSV file into a SQLite table.

My script curently uses functions for everything and I'm trying to
improve my class programming. The problem I'm having is with variable
expansion.

self.cursor.executemany('INSERT INTO test VALUES (?)', CSVinput)

If CSVinput is a tuple with only 1 value, everything is fine. If I
want to use a tuple with more than 1 value, I need to add more
question marks. As I'm writing a class I don't want to hard code a
specific number of ?s into the INSERT statement.

The two solutions I can think of are;
using python subsitution to create a number of question marks, but
this seems very dirty
or
finding someway to substitue tuples or lists into the statement - I'm
not sure if this should be done using Python or SQLite substitution
though.

Any tips on where to start looking?

Thanks, Marc.
Jul 30 '08 #1
4 2272
marc wyburn wrote:
Hi I'm using SQlite and the CSV module and trying to create a class
that converts data from CSV file into a SQLite table.

My script curently uses functions for everything and I'm trying to
improve my class programming. The problem I'm having is with variable
expansion.

self.cursor.executemany('INSERT INTO test VALUES (?)', CSVinput)

If CSVinput is a tuple with only 1 value, everything is fine. If I
want to use a tuple with more than 1 value, I need to add more
question marks. As I'm writing a class I don't want to hard code a
specific number of ?s into the INSERT statement.

The two solutions I can think of are;
using python subsitution to create a number of question marks, but
this seems very dirty
or
finding someway to substitue tuples or lists into the statement - I'm
not sure if this should be done using Python or SQLite substitution
though.

I do this kind of thing sometimes:

<test.csv>
a,b,c
1,2,3
4,5,6
</test.csv>

<code>
import csv
import sqlite3

reader = csv.reader (open ("test.csv", "rb"))
csv_colnames = reader.next ()

db = sqlite3.connect (":memory:")
coldefs = ", ".join ("%s VARCHAR (200)" % c for c in csv_colnames)
db.execute ("CREATE TABLE test (%s)" % coldefs)

insert_cols = ", ".join (csv_colnames)
insert_qmarks = ", ".join ("?" for _ in csv_colnames)
insert_sql = "INSERT INTO test (%s) VALUES (%s)" % (insert_cols, insert_qmarks)

db.executemany (insert_sql, list (reader))
for row in db.execute ("SELECT * FROM test"):
print row

</code>

Obviously, this is a proof-of-concept code. I'm (ab)using
the convenience functions at database level, I'm hardcoding
the column definitions, and I'm making a few other assumptions,
but I think it serves as an illustration.

Of course, you're only a few steps away from something
like sqlalchemy, but sometimes rolling your own is good.

TJG
Jul 30 '08 #2
Tim Golden wrote:
marc wyburn wrote:
>Hi I'm using SQlite and the CSV module and trying to create a class
that converts data from CSV file into a SQLite table.

My script curently uses functions for everything and I'm trying to
improve my class programming. The problem I'm having is with variable
expansion.

self.cursor.executemany('INSERT INTO test VALUES (?)', CSVinput)

If CSVinput is a tuple with only 1 value, everything is fine. If I
want to use a tuple with more than 1 value, I need to add more
question marks. As I'm writing a class I don't want to hard code a
specific number of ?s into the INSERT statement.

The two solutions I can think of are;
using python subsitution to create a number of question marks, but
this seems very dirty
or
finding someway to substitue tuples or lists into the statement - I'm
not sure if this should be done using Python or SQLite substitution
though.


I do this kind of thing sometimes:

<test.csv>
a,b,c
1,2,3
4,5,6
</test.csv>

<code>
import csv
import sqlite3

reader = csv.reader (open ("test.csv", "rb"))
csv_colnames = reader.next ()

db = sqlite3.connect (":memory:")
coldefs = ", ".join ("%s VARCHAR (200)" % c for c in csv_colnames)
db.execute ("CREATE TABLE test (%s)" % coldefs)

insert_cols = ", ".join (csv_colnames)
insert_qmarks = ", ".join ("?" for _ in csv_colnames)
insert_sql = "INSERT INTO test (%s) VALUES (%s)" % (insert_cols,
insert_qmarks)

db.executemany (insert_sql, list (reader))
for row in db.execute ("SELECT * FROM test"):
print row

</code>

Obviously, this is a proof-of-concept code. I'm (ab)using
the convenience functions at database level, I'm hardcoding
the column definitions, and I'm making a few other assumptions, but I
think it serves as an illustration. [..]
My code would probably look very similar. Btw you don't need to use
list() on an iterable to pass to executemany(). pysqlite's executemany()
accepts anything iterable (so generators work fine, too).

Also, with SQLite you can just skip data type definitions like
VARCHAR(200). They're ignored anyway.

-- Gerhard

Jul 30 '08 #3
Gerhard Häring wrote:
My code would probably look very similar. Btw you don't need to use
list() on an iterable to pass to executemany(). pysqlite's executemany()
accepts anything iterable (so generators work fine, too).
Thanks for that. My finger-memory told me to do that, possibly
because some *other* dbapi interface only accepts lists. Can't
quite remember. I'm usually all in favour of non-crystallised
iterators.
Also, with SQLite you can just skip data type definitions like
VARCHAR(200). They're ignored anyway.
Heh. Once again, finger memory forced me to put *something*
in there. I've been developing Enterprise databases for too
long :)

TJG
Jul 30 '08 #4
Hi and thanks,

I was hoping to avoid having to weld qmarks together but I guess
that's why people use things like SQL alchemy instead. It's a good
lesson anyway.

Thanks, Marc.
On Jul 30, 2:24*pm, Tim Golden <m...@timgolden.me.ukwrote:
Gerhard Häring wrote:
My code would probably look very similar. Btw you don't need to use
list() on an iterable to pass to executemany(). pysqlite's executemany()
accepts anything iterable (so generators work fine, too).

Thanks for that. My finger-memory told me to do that, possibly
because some *other* dbapi interface only accepts lists. Can't
quite remember. I'm usually all in favour of non-crystallised
iterators.
Also, with SQLite you can just skip data type definitions like
VARCHAR(200). They're ignored anyway.

Heh. Once again, finger memory forced me to put *something*
in there. I've been developing Enterprise databases for too
long :)

TJG
Jul 30 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
12
by: John Salerno | last post by:
I've been looking around and reading, and I have a few more questions about SQLite in particular, as it relates to Python. 1. What is the current module to use for sqlite? sqlite3? or is that not...
4
by: Jim Carlock | last post by:
I added the following lines to PHP.INI. extension=php_pdo.dll extension=php_pdo_sqlite.dll extension=php_sqlite.dll specifically in that order. I noticed the extensions getting loaded are...
10
by: Luigi | last post by:
Hello all! I'm a newbie in PHP. I have written a short script that tries to update a SQLite database with the user data. It is pretty simple, something like this: <?php $sqlite =...
9
by: Gilles Ganault | last post by:
Hello I was looking for a lighter web server than Apache, and installed Lighttpd on CentOS through yum. It works fine, but I now need to use SQLite from a PHP script. I seem to understand that...
3
by: Daniel Fetchinson | last post by:
Does Python 2.5.2's embedded SQLite support full text searching? Sqlite itself is not distributed with python. Only a python db api compliant wrapper is part of the python stdlib and as such it...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
20
by: timotoole | last post by:
Hi all, On a (sun) webserver that I use, there is python 2.5.1 installed. I'd like to use sqlite3 with this, however sqlite3 is not installed on the webserver. If I were able to compile sqlite...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.