13.07.2015 Views

The wxPython tutorial

The wxPython tutorial

The wxPython tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working with databaseshttp://www.zetcode.com/wxpython/databases/What went wrong? We did not commit our changes. When weexecuted the select statement using the python sqlite api, wereceived result within a transaction context. <strong>The</strong> changes were notreally written to the database. When we checked the data in thesqlite3 utility, we got age 18. <strong>The</strong> data was not changed.>>> cur.execute("update neighbours set age=29 where name='lucy'")>>> con.commit()>>> cur.close()>>> con.close()>>> (CTRL + D)$ sqlite3 peoplesqlite> select age from neighbours where name='lucy';29After committing our changes with the commit() method of theconnection object, the data changes are really written to thedatabase.In the next example we demonstrate that the DCL statements arecommitted automatically. We will use create table command, whichis a part of the DCL language.>>> cur.execute('create table relatives(name text, age numeric)')>>> cur.close()>>> con.close()>>> (CTRL + D)$ sqlite3 peoplesqlite> .tablesneighbours relatives<strong>The</strong>re is one more thing to mention. We can create a connection,which will automatically commit all our changes. This is done, whenwe set the isolation_level parameter to None.>>> import sqlite3 as lite>>> con = lite.connect('people', isolation_level=None)>>> cur = con.cursor()>>> cur.execute("insert into neighbours values ('rebecca', 16, 'shy')")>>> cur.close()>>> con.close()>>> (CTRL + D)$ sqlite3 peoplesqlite> select * from neighbours where name='rebecca';rebecca|16|shysqlite>AutoincrementAutoincremental primary key is a handy feature. We insert new rows8 de 12 27/04/2008 1:06

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!