Access SQLite using Python

Source: Internet
Author: User
1. first, go to www.sqlite.org to download an SQLite database. It is an embedded database without the concept of a server. For Windows, it is an EXE and you can put it in a suitable directory, then add the directory to the system's PATH variable.
2. Then find a pysqlite, which is the python access SQLite interface, address here: http://initd.org/tracker/pysqlite
Currently, for different Python versions, pysqlite has two versions: 2.3 and 2.4. Select one based on your Python version.
3. Then you can open your favorite editor and write a test code.
4. During Chinese processing, SQLite is stored in UTF-8 encoding by default.
5. In addition, SQLite only supports file locks. In other words, it is not suitable for concurrent processing and is not recommended in the network environment. It is suitable for single-host environments. Import pysqlite2.dbapi2 as SQLite

Def runtest ():
Cx = SQLite. Connect ('test. db ')
Cu = Cx. cursor ()

# Create
Cu.exe cute (''' create table catalog (
Id integer primary key,
PID integer,
Name varchar (10) unique
)''')

# Insert
Cu.exe cute ('insert into catalog values (0, 0, "Zhang Shan ")')
Cu.exe cute ('insert into catalog values (1, 0, "hello ")')
Cx. Commit ()

# Select
Cu.exe cute ('select * From catalog ')
Print '1 :',
Print cu. rowcount
Rs = Cu. fetchmany (1)
Print '2 :',
Print rs
Rs = Cu. fetchall ()
Print '3 :',
Print rs

# Delete
Cu.exe cute ('delete from catalog where id = 1 ')
Cx. Commit ()


Cu.exe cute ('select * From catalog ')
Rs = Cu. fetchall ()
Print '4 :',
Print rs

# Select count
Cu.exe cute ("select count (*) from catalog ")
Rs = Cu. fetchone ()
Print '5 :',
Print rs


Cu.exe cute ("select * From catalog ")
Cu.exe cute ('drop table catalog ')

If _ name _ = '_ main __':
Runtest ()
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.