您的位置:首页 > 编程语言 > Python开发

【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事

2017-05-24 20:12 1121 查看

如何使用SQLite

下载地址(SQLite v3.19.0.管理数据库文件的命令行工具包,包括3个可执行程序:sqldiff.exe, sqlite3.exe,sqlite3_analyzer.exe):

https://www.sqlite.org/2017/sqlite-tools-win32-x86-3190000.zip

将3个程序解压到需要使用的位置即可使用;

使用命令行工具

创建数据库

sqlite3.exe python




创建表;向表中插入内容

CREATE TABLE people (name VARCHAR(30), age INT, sex CHAR(1))


INSERT INTO people VALUES ('Tom', 20, 'M')
INSERT INTO people VALUES ('Jack', 21, 'M')


查看表中内容

SELECT * FROM people




退出

.exit


使用Python

# -*- coding:utf-8 -*-
#
import sqlite3
con = sqlite3.connect('python')   # connect to db
cur = con.cursor()                # get the db cursor
cur.execute('insert into people (name, age, sex) values (\'Jee\',21, \'F\')')
# execute SQL, add record
r = cur.execute('delete from people where age=20')
# execute SQL, del record
con.commit()                      # commit change
cur.execute('select * from people') # execute SQL, get record
s = cur.fetchall()                # get data
print s                           # print data
cur.close()                       # close cursor
con.close()                       # close connection








什么是SQLite

SQLite是一款轻型的嵌入式数据库,相对于其他的庞大数据库软件,SQLite显得十分轻巧。

SQLite不需要MySQL的守护进程,也不需要安装Access那么庞大的软件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐