SQLite数据库基础命令

来源:互联网 发布:mac解压软件破解版 编辑:程序博客网 时间:2024/05/21 10:26

安装SQLite数据库

Ubuntu

sudo apt-get install sqlite3

Windows
在http://www.sqlite.org/download.html页面下载sqlite-dll-win***.zip和sqlite-tools-win***zip两个压缩包,在C盘新建一个sqlite文件夹,将两个压缩包解压至C:\sqlite中,最后将C:\sqlite添加至环境变量PATH中。在cmd下输入sqlite3命令测试是否安装成功。

SQLite增删改查

创建数据库和表

# 创建数据库root@iZg38vm81qzumlZ:~# sqlite3 /sqlite/test.db# 创建新表create table person(id integer primary key,name varchar(20),age integer);

增删改查操作

  • 增加
insert into person(name,age) values('admin',20);
  • 修改
update person set age=20 where name='angle';
  • 查询
select * from person;
  • 删除
delete from person where name='admin';

常用SQLite命令

  • 显示表结构
sqlite> .schema [table]
  • 获取所有表和视图
sqlite> .tables
  • 获取指定表的索引列表
sqlite> .indices [table]
  • 导出数据库到SQL文件
sqlite> .output /sqlite/out.sqlsqlite> .dumpsqlite> .output stdout
  • 从SQL文件导入数据库
sqlite> .read /sqlite/out.sql
  • 格式化输出数据到csv文件
sqlite> .output /sqlite/out.csvsqlite> .separator ,sqlite> select * from password;sqlite> .output stdout
  • 从csv文件导入数据到表中
sqlite> create table csv(id integer primary key,name varchar(255),password varchar(255));sqlite> .import /sqlite/out.csv csv
  • 备份数据库
root@iZg38vm81qzumlZ:/sqlite# sqlite3 test.db .dump > backup.sql
  • 恢复数据库
root@iZg38vm81qzumlZ:/sqlite# sqlite3 test.db < backup.sql
原创粉丝点击