python 中使用sqlite3数据库
来源:互联网 发布:js 非严格模式 编辑:程序博客网 时间:2024/05/17 04:09
SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。
移植过程简单,具体参考相关文档。
以下是简单的教程:参考http://database.51cto.com/art/201205/335411.htm
1、 创建 SQLite 数据库
现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为 test.db 的数据库
- sqlite3 test.db
创建表:
- sqlite> create table if not exists dataCatlog(id varchar primary key ,content varchar
该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。
接下来往表里中写入一些数据:
- sqlite> insert into mytable(id, value) values(1, 'Micheal');
- sqlite> insert into mytable(id, value) values(2, 'Jenny');
- sqlite> insert into mytable(value) values('Francis');
- sqlite> insert into mytable(value) values('Kerk');
查询数据:
- sqlite> select * from test;
- 1|Micheal
- 2|Jenny
- 3|Francis
- 4|Kerk
设置格式化查询结果:
- sqlite> .mode column;
- sqlite> .header on;
- sqlite> select * from test;
- id value
- ----------- -------------
- 1 Micheal
- 2 Jenny
- 3 Francis
- 4 Kerk
.mode column 将设置为列显示模式,.header 将显示列名。
修改表结构,增加列:
- sqlite> alter table mytable add column email text not null '' collate nocase;;
创建视图:
- sqlite> create view nameview as select * from mytable;
创建索引:
- sqlite> create index test_idx on mytable(value);
4. 一些有用的 SQLite 命令
显示表结构:
- sqlite> .schema [table]
获取所有表和视图:
- sqlite > .tables
获取指定表的索引列表:
- sqlite > .indices [table ]
导出数据库到 SQL 文件:
- sqlite > .output [filename ]
- sqlite > .dump
- sqlite > .output stdout
从 SQL 文件导入数据库:
- sqlite > .read [filename ]
格式化输出数据到 CSV 格式:
- sqlite >.output [filename.csv ]
- sqlite >.separator ,
- sqlite > select * from test;
- sqlite >.output stdout
从 CSV 文件导入数据到表中:
- sqlite >create table newtable ( id integer primary key, value text );
- sqlite >.import [filename.csv ] newtable
备份数据库:
- /* usage: sqlite3 [database] .dump > [filename] */
- sqlite3 mytable.db .dump > backup.sql
恢复数据库:
结合自己的需求,写了一个简单的demo,
- /* usage: sqlite3 [database ] < [filename ] */
- sqlite3 mytable.db < backup.sql
功能描述:建立数据库,insert函数每次插入一条数据。deldata每次从数据库中取出一条数据并删除之。
import sqlite3class dbManage(): def insert(self, key, value): conn = sqlite3.connect('test.db') cu = conn.cursor() cu.execute('create table if not exists dataCatlog(id varchar primary key ,content varchar )') cu.execute('insert into dataCatlog values (?,?)', (key, value)) cu.execute('select * from dataCatlog') print cu.fetchall() conn.commit() conn.close() def deldata(self): conn = sqlite3.connect('test.db') cu = conn.cursor() cu.execute('create table if not exists dataCatlog(id varchar primary key ,content varchar )') cu.execute('select * from dataCatlog') allrecords = cu.fetchall() print "before del:", allrecords for record in allrecords: #record = cu.fetchone()[0] cu.execute("delete from dataCatlog where id = ?", (record[0],)) cu.execute('select * from dataCatlog') print "after del:", cu.fetchall() conn.commit() #conn.close() conn.close()
0 0
- python 中使用sqlite3数据库
- python 中使用sqlite3和mysql数据库
- Python使用sqlite3操作数据库
- iphone中使用SQLITE3数据库
- Android中使用SQLite3数据库
- python sqlite3 查看数据库中所有表
- java中使用jdbc连接SQLite3数据库
- android中sqlite3数据库的使用
- 在vc中使用sqlite3数据库
- iOS 中 使用 sqlite3 创建数据库
- Xcode中使用sqlite3访问数据库
- python 在linux中使用sqlite3
- 如何在python中使用sqlite3
- python 在linux中使用sqlite3
- 使用python抓取csdn博客访问量并保存在sqlite3数据库中
- Python压缩Sqlite3数据库
- sqlite3 python 数据库
- Python 数据库 sqlite3模块
- opencv中的cvWaitKey
- 数学建模--回归与预测
- ubuntu安装vsftp服务器详解教程
- UVA571- Jugs
- C++重载箭头操作符
- python 中使用sqlite3数据库
- 小手莫贱,防患未然
- Linux命令简介(目录操作命令、文件操作命令)
- ACM 线性表操作
- JVM
- Java的包访问权限(friendly)
- 【HDU】4632 Palindrome subsequence (DP)
- set 的使用例子
- 有关delegate