inbetween cPickle and MySQL

Gerhard Häring gerhard at bigfoot.de
Wed May 29 16:19:12 EDT 2002


* Kevin Dahlhausen <kdahlhaus at yahoo.com> [2002-05-29 11:36 -0700]:
> m2 at plusseven.com wrote in message news:<mailman.1022665786.12266.python-list at python.org>...
> > I need a simple way to store lots of simple objects (compsed of
> > strings only). Right now I'm using cPickle, which means I have to read
> > in the intire file to find all object.attribute == X. 
> 
> There is a python inteface to Sqlite, which is a file-based SQL
> database.  You use the module, no server is needed and it does not
> load the entire file into memory.

Yes, such a module is available at http://pysqlite.sf.net/

> Currently it returns all results as strings.

Not true for the CVS version of the _above_ PySQLite. You _can_ use
custom converters, and int, float and string are supported by default.
It requires some magic because SQLite is typeless, this should show the
features for people familiar with the DB-API:

>>> import sqlite
>>> db = sqlite.connect("/tmp/db")
>>> cursor = db.cursor()
>>> cursor.execute("create table test(id int, name varchar(20))")
>>> cursor.execute("insert into test(id, name) values (%s, %s)", (5, "foo"))
>>> cursor.execute("pysqlite_pragma expected_types = int, str")
>>> cursor.execute("select id, name from test")
>>> res = cursor.fetchone()
>>> print res.id, res.name, res[0], res[1], res["id"], res["name"]
5 foo 5 foo 5 foo
>>> print type(res.id), type(res.name)
<type 'int'> <type 'string'>

> http://members.nccw.net/kdahlhaus/pysqlite/index.html

Kevin, you're very welcome to contribute to our SQLite project at
Sourceforge, if you like.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id AD24C930
public key fingerprint: 3FCC 8700 3012 0A9E B0C9  3667 814B 9CAA AD24 C930
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))





More information about the Python-list mailing list