A simple introduction
SQLite database is a very small embedded open source database software, that is, there is no independent maintenance process, all the maintenance is from the program itself. It is a relational database management system that adheres to acid, it is designed to be embedded, and has been used in many embedded products, it occupies very low resources, in the embedded device, may only need hundreds of K of memory is enough. It can support Windows/linux/unix and so on mainstream operating system, and can be combined with many programming languages, such as TCL, C #, PHP, Java, and ODBC interface, also compared to MySQL, PostgreSQL, the two open source world-renowned database management system, is processing faster than they do. The first alpha version of SQLite was born in May 2000. So far there have been 10 years, SQLite also ushered in a version of SQLite 3 has been released.
Installation and use
1. Import
Python SQLite Database module
After the Python2.5, built-in SQLite3, has become a built-in module, which gives us to save the installation of Kung Fu, just import can ~
Import Sqlite3
2. Create/Open Database
When calling the Connect function, specify the name of the library, if the specified database exists, open the database directly, and if it does not exist, create a new one and open it.
CX = Sqlite3.connect ("e:/test.db") can also create databases in memory. con = Sqlite3.connect (": Memory:")
3. Database Connection objects
The object that is returned when you open the database CX is a database connection object that can do the following:
- Commit ()--Transaction commit
- Rollback ()--Transaction rollback
- Close ()--Close a database connection
- Cursor ()--Create a cursor
With respect to commit (), if the isolation_level isolation level defaults, then each operation on the database needs to use the command, you can also set isolation_level=none, This changes to autocommit mode.
4. Querying the database with cursors
We need to query the database using the Cursor object SQL statement to get the query object. Define a cursor by using the following method.
Cu=cx.cursor ()
The cursor object has the following actions:
- Execute ()--Execute SQL statement
- executemany--executing multiple SQL statements
- Close ()--close cursor
- Fetchone ()--Takes a record from the result and points the cursor to the next record
- Fetchmany ()--take multiple records from the results
- Fetchall ()--Remove all records from the results
- Scroll ()--cursor scrolling
1. Build a table
Cu.execute ("CREATE TABLE catalog (ID integer primary key,pid integer,name varchar (ten) unique,nickname text NULL)")
The above statement creates a table called catalog, which has a primary key ID, a PID, and a name,name that cannot be duplicated, and a nickname default is null.
2. Inserting Data
Be careful to avoid the following wording:
# never do this--insecure will cause an injection attack
pid=200
C.execute ("... where pid = '%s '"% pid) the correct approach is as follows, if T is just a single value, also in the form of t= (n,), because tuples are immutable.
For T in[(0,10, ' abc ', ' Yu '), (1,20, ' CBA ', ' Xu ')]:
Cx.execute (INSERT into catalog values (?,?,?,?), T) simply inserts two rows of data, but you need to be reminded that only after you have submitted it, To take effect. We use the database connection object CX to commit commit and rollback rollback operations.
Cx.commit ()
3. Enquiry
Cu.execute ("SELECT * from Catalog")
To extract the queried data, use the FETCH function of the cursor, such as:
In [ten]: Cu.fetchall ()
OUT[10]: [(0, Ten, u ' abc ', U ' Yu '), (1, +, U ' CBA ', U ' Xu ')]
If we use Cu.fetchone (), we first return the first item in the list and use it again, then we return to the second item and go down.
4. Modifications
In [Page]: Cu.execute ("Update Catalog set name= ' boy ' where id = 0")
In []: Cx.commit ()
Note that after you modify the data, submit
5. Delete
Cu.execute ("Delete from catalog where id = 1")
Cx.commit ()
6. Use Chinese
Please make sure your IDE or system default encoding is Utf-8, and add u before Chinese
X=u ' Fish '
Cu.execute ("Update Catalog set name=?") WHERE id = 0 ", x)
Cu.execute ("SELECT * from Catalog")
Cu.fetchall ()
[(0, Ten, U ' \u9c7c ', U ' Yu '), (1, +, U ' CBA ', U ' Xu ')]
If you want to display a Chinese font, you need to print out each string in turn
in [+]: for item in Cu.fetchall ():
....: for element in item:
.....: Print Element,
.....: Print
....:
0 10 Fish Yu
1 CBA Xu
Use Python for SQLite database operations