pythonsqlite视图_python SQLite3

本文详细介绍了如何在CentOS系统上安装SQLite3,并演示了创建数据库、插入数据、查询数据、修改表结构、创建视图、创建索引等基本操作。此外,还列举了一些实用的SQLite命令,如显示表结构、获取表和视图列表、导出与导入数据库等。
摘要由CSDN通过智能技术生成

一、centos 安装SQLite

# yum install SQLite3 sqlite3-dev

二、连接SQLite3

#sqlite3 test.db

三. 创建首个 SQLite 数据库

现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为 test.db 的数据库。 sqlite3 test.db

sqlite3 test.db

sqlite> create table mytable(id integer primary key, value text); 2 columns were created.

该表包含一个名为 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

恢复数据库: /* usage: sqlite3 [database ] < [filename ] */ sqlite3 mytable.db < backup.sql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值