Pysqlite Concise Tutorial

Source: Internet
Author: User
Tags documentation connect sqlite in python

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.

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.