Python to operate SQLite

Source: Internet
Author: User
I. Installation

Go to pysqlite? Download the installation package on the home page. It has a Windows version and supports Python 2.3 and 2.5.

2. Create a database/open a database

SQLite uses files as databases. You can specify the location of database files.

    >>> import sqlite3
>>> cx = sqlite.connect("d:/test.db", encoding='cp936')

You can use SQLite connect to create a database file. The path is specified above. When the database file does not exist, it is automatically created. If the file already exists, open the file. Encoding indicates the encoding used to save the data. cp936 is a self-contained encoding in Python, which is actually GBK encoding. CX is the database connection object.

III. Basic database objects

3.1 database connection object

Like the preceding CX is a database connection object, it can perform the following operations:

  • Commit () -- transaction commit
  • Rollback () -- transaction rollback
  • Close () -- close a database connection
  • Cursor () -- create a cursor

3.2 cursor object

All SQL statements must be executed under the cursor object.

    cu = cx.cursor()

This defines a cursor. The cursor object has the following operations:

  • Execute () -- execute an SQL statement
  • Executemany -- execute multiple SQL statements
  • Close () -- close the cursor
  • Fetchone () -- get a record from the result
  • Fetchrecords () -- retrieve multiple records from the results
  • Fetchall () -- retrieve multiple records from the result
  • Scroll () -- cursor scroll

For object methods, go to the python homepage to view detailed database API documentation. But pysqlite? I don't know what programs support dB APIs. All the operations I listed are supported, but I have not used them all.

Iv. Examples

4.1 database creation

We already have it. We will not repeat it. (If you are interested in these examples, you can try it directly in the python interaction environment)

4.2 create a table

    >>> cu=cx.cursor()
>>> cu.execute("""create table catalog (
id integer primary key,
pid integer,
name varchar(10) UNIQUE
)""")

The preceding statement creates a table named catalog, which has a primary key ID, a PID, and a name and name that cannot be repeated.

For the data types supported by SQLite, see version 2 datatypes?

4.3 insert)

    >>> cu.execute("insert into catalog values(0, 0, 'name1')")
>>> cu.execute("insert into catalog values(1, 0, 'hello')")
>>> cx.commit()

If you want to, you can always use the Cu cursor object. Note that the transaction statement commit () or rollback () must be used to modify the data, and the object is the database connection object. CX is used here.

4.4 select)

    >>> cu.execute("select * from catalog")
>>> cu.fetchall()
[(0, 0, 'name2'), (1, 0, 'hello')]

Fetchall () returns all the data in the result set, and the result is a list of tuple. Each tuple element is arranged in the order of fields in the table creation. Note that a cursor is stateful and records the first few records of the result. Therefore, you can traverse the result set only once. In the preceding case, if fetchone () is executed, null is returned. Note This during testing.

    >>> cu.execute("select * from catalog where id = 1")
>>> cu.fetchone()
(1, 0, 'hello')

If no statement is modified for the database, you do not need to execute the transaction statement after execution.

4.5 Update (modify)

    >>> cu.execute("update catalog set name='name2' where id = 0")
>>> cx.commit()
>>> cu.execute("select * from catalog")
>>> cu.fetchone()
(0, 0, 'name2')

4.6 Delete)

    >>> cu.execute("delete from catalog where id = 1")
>>> cx.commit()
>>> cu.execute("select * from catalog")
>>> cu.fetchall()
[(0, 0, 'name2')]

The above is about how to use pysqlite? To operate SQLite.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.