tags:

views:

263

answers:

3

It is very important to me.

Thanks.

A: 

http://stackoverflow.com/questions/2182774/can-sqlite-load-10m-data-into-the-memory

Richo
http://is.gd/67x9w
Alok
@Alok: Fair enough, although tbh considering the phrasing of the question I was either going to google python sqlite and just post up the first link, or close the question I didn't see the harm. Taken on board, nonetheless.
Richo
@Richo: my link was from http://meta.stackoverflow.com/questions/35256/sorry-posts-cant-contain-that-content/35257#35257. :-)
Alok
@Alok: I got a cheap giggle, and also learned a bit. Thanks mate.
Richo
A: 

Follow these steps:

  1. sudo easy_install pysqlite
  2. open python prompt
  3. from pysqlite2 import dbapi2 as sqlite
  4. connection = sqlite.connect('test.db')
  5. cursor = connection.cursor()
  6. cursor.execute('CREATE TABLE names (id INTEGER PRIMARY KEY, name VARCHAR(50), email VARCHAR(50))')

Thats it.

GeekTantra
A: 

By "into memory", do you mean into the SQL world or into the pure-Python world ? If the latter, here's a snippet to read a sqlite db into a pure-Python dict of namedtuples (records, structs; these make code more readable, especially if you have 10 or 20 columns).

def sqlread( rows, db, tablename, namedtup=None ):
    """ read a sqlite table into a dict of namedtuples
        example:
        Person = namedtuple( "Person", "name city" )
        rows = {}
        db = sqlite3.connect( "my.db" )
        sqlread( rows, db, "Persontable", Person )
        now rows = e.g. { 1: Person( "Jim", "Tokyo" ), 2: ... }
        for key, row in rows.iteritems():
            if row.name == "Jim": ...

    """
    cur = db.cursor()
    cur.execute( "Select * from %s" % tablename )  # where ...
    nrow = 0
    for keyrow in cur.fetchall():
        key, row = keyrow[0], keyrow[1:] 
            # numbers, strings, plain tuples work too --
        row = namedtup._make( row ) if namedtup \
            else row if len(row) > 1 \
            else row[0]
        rows[key] = row
        nrow += 1
    return nrow
Denis