12. Data Persistence
- 12.1.pickle - Python object serialization
- 12.2.copyreg - Registerpicklesupport functions
- 12.3.shelve - Python object persistence
- 12.4.marshal - Internal Python object serialization
- 12.5.dbm - Interfaces to Unix "databases"
- 12.6.sqlite3 - DB-API 2.0 interface for SQLite databases
SQLite3
SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It's also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.
con = sqlite3.connect('mydatabase.db')
c.execute("DROP TABLE ratings")
c.execute("CREATE TABLE IF NOT EXISTS ratings(userId int, movieId int, rating float, timestamp int)")
c.execute("INSERT INTO ratings(userId, movieId, rating, timestamp) VALUES (?,?,?,?)", row)
for row in c.execute("SELECT * FROM ratings"):
print(row)
con.commit()
This method commits the current transaction. If you don't call this method, anything you did since the last call tocommit()is not visible from other database connections.