[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Strange sqlite3 library behavior

Victor Lin

2/2/2008 10:48:00 AM

Now I am now developing a program that base on sqlite3 in python.
But there is a strange problem.
That is, all data I insert into sqlite database do not goes into file
in disk.
It is really strange....
Why do these data just keep in memory and discarded?

All things that really store in file is the table.
If I create a table, it would appears in the sqlite file.

This is the example :

import sqlite3

createSyntax = """CREATE TABLE IF NOT EXISTS category(
id INTEGER NOT NULL PRIMARY KEY
)"""

createSyntax2 = """CREATE TABLE IF NOT EXISTS category2(
id INTEGER NOT NULL PRIMARY KEY
)"""

insertSyntax = """INSERT INTO category VALUES (1234)"""

db = sqlite3.connect('test.db')
cur = db.cursor()
cur.execute(createSyntax)
cur.execute(insertSyntax)
# only by doing so can let inserted data store in file
cur.execute(createSyntax2)
cur.execute('select * from category')
print cur.fetchall()

db.close()

raw_input()


After I tried some way. I found that create table force data goes into
file. Otherwise all data would just keep in memory and discarded with
the program's ending.
Why sqlite3 just keep inserted data in memory? And how to force
inserted data into file?

Thanks.

Victor Lin.
2 Answers

Roel Schroeven

2/2/2008 11:19:00 AM

0

Victor Lin schreef:
> Now I am now developing a program that base on sqlite3 in python.
> But there is a strange problem.
> That is, all data I insert into sqlite database do not goes into file
> in disk.
> It is really strange....
> Why do these data just keep in memory and discarded?

sqlite3 works according to PEP 249, which means among other things that
transactions are by default not auto-committed.

There are two possible solutions:
- manually commit the transaction when it's finished with db.commit()
(perhaps the cursor object also has a commit() method; I'm not sure
about that)
- enable auto-commit.

For more info, see the sqlite3 docs (especially the section on
controlling transactions:
http://docs.python.org/lib/sqlite3-Controlling-Transac...) and
PEP 249 (http://www.python.org/dev/peps...)

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

Guilherme Polo

2/2/2008 11:38:00 AM

0

2008/2/2, Roel Schroeven <rschroev_nospam_ml@fastmail.fm>:
> Victor Lin schreef:
>
> > Now I am now developing a program that base on sqlite3 in python.
> > But there is a strange problem.
> > That is, all data I insert into sqlite database do not goes into file
> > in disk.
> > It is really strange....
> > Why do these data just keep in memory and discarded?
>
>
> sqlite3 works according to PEP 249, which means among other things that
> transactions are by default not auto-committed.
>
> There are two possible solutions:
> - manually commit the transaction when it's finished with db.commit()
> (perhaps the cursor object also has a commit() method; I'm not sure
> about that)
> - enable auto-commit.

While these solutions are correct I wouldn't suggest enabling
auto-commit, you will have serious performance issues.

>
> For more info, see the sqlite3 docs (especially the section on
> controlling transactions:
> http://docs.python.org/lib/sqlite3-Controlling-Transac...) and
> PEP 249 (http://www.python.org/dev/peps...)
>
> --
> The saddest aspect of life right now is that science gathers knowledge
> faster than society gathers wisdom.
> -- Isaac Asimov
>
>
> Roel Schroeven
>
> --
> http://mail.python.org/mailman/listinfo/p...
>


--
-- Guilherme H. Polo Goncalves