The main purpose of this article is to serve as an entry-level tutorial that will teach you some basic statements about how to use Pysqlite to manipulate sqite, and in more detail refer to the documentation you want to work with and write the appropriate test procedures. I hope this article is helpful to you.
Pysqlite's home Address: http://pysqlite.sourceforge.net/with documentation on using Pysqlite
First, installation
Download the installation package on the Pysqlite home page, with a version of Windows that now supports the Python version 2.2 and 2.3.
Second, create a database/open a database
SQLite use files as a database, you can specify the location of the database files.
>>> import sqlite
>>> cx = sqlite.connect("d:/test.db", encoding='cp936')
Using SQLite Connect, you can create a database file, which I indicate the path. When the database file does not exist, it is created automatically. If the file already exists, open the file. Encoding indicates the encoding used to save the data, where cp936 is encoded in Python, which is actually GBK encoding. CX connects objects to the database.
Basic object of Operation database
3.1 Database Connection objects
Like the previous CX is a database connection object, it can have the following actions:
commit()--事务提交
rollback()--事务回滚
close()--关闭一个数据库连接
cursor()--创建一个游标
3.2 Cursor Objects
The execution of all SQL statements is performed under the cursor object.
cu = cx.cursor()
This defines a cursor. The cursor object has the following actions:
execute()--执行sql语句
executemany--执行多条sql语句
close()--关闭游标
fetchone()--从结果中取一条记录
fetchmany()--从结果中取多条记录
fetchall()--从结果中取出多条记录
scroll()--游标滚动
The approach to objects can be viewed on the Python home page for detailed documentation of the DB API. But I don't know what the pysqlite is doing to support the DB API. The actions I have listed are supported, but I have not used them all.