加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite数据库的使用

发布时间:2020-12-12 23:48:18 所属栏目:百科 来源:网络整理
导读:已经安装了 SQLite 数据库,接下来在命令行窗口中输入命令 创建数据库: sqlite3test.db 创建表: sqlite create table mytable(id integer primary key ,valuetext); 2columnswerecreated. 注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据
已经安装了 SQLite 数据库,接下来在命令行窗口中输入命令

创建数据库:

 
 
  1. sqlite3test.db

创建表:

 
 
  1. sqlite>createtablemytable(idintegerprimarykey,valuetext);
  2. 2columnswerecreated.

注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据库保存到磁盘中,否则数据库不会被创建。

插入数据:

 
 
  1. sqlite>insertintomytable(id,value)values(1,'Micheal');
  2. sqlite>insertintomytable(id,value)values(2,'Jenny');
  3. sqlite>insertintomytable(value)values('Francis');
  4. sqlite>insertintomytable(value)values('Kerk');

查询数据:

 
 
  1. sqlite>select*fromtest;
  2. 1|Micheal
  3. 2|Jenny
  4. 3|Francis
  5. 4|Kerk

设置格式化查询结果:

 
 
  1. sqlite>.modecolumn;
  2. sqlite>.headeron;
  3. sqlite>select*fromtest;
  4. idvalue
  5. ------------------------
  6. 1Micheal
  7. 2Jenny
  8. 3Francis
  9. 4Kerk

.mode column 将设置为列显示模式,.header 将显示列名。

修改表结构,增加列:

 
 
  1. sqlite>altertablemytableaddcolumnemailtextnotnull''collatenocase;;

创建视图:

 
 
  1. sqlite>createviewnameviewasselect*frommytable;

创建索引:

 
 
  1. sqlite>createindextest_idxonmytable(value);

显示表结构:

 
 
  1. sqlite>.schema[table]

获取所有表和视图:

 
 
  1. sqlite>.tables

获取指定表的索引列表:

 
 
  1. sqlite>.indices[table]

导出数据库到 SQL 文件:

 
 
  1. sqlite>.output[filename]
  2. sqlite>.dump
  3. sqlite>.outputstdout

从 SQL 文件导入数据库:

 
 
  1. sqlite>.read[filename]

格式化输出数据到 CSV 格式:

 
 
  1. sqlite>.output[filename.csv]
  2. sqlite>.separator,
  3. sqlite>select*fromtest;
  4. sqlite>.outputstdout

从 CSV 文件导入数据到表中:

 
 
  1. sqlite>createtablenewtable(idintegerprimarykey,valuetext);
  2. sqlite>.import[filename.csv]newtable

备份数据库:

 
 
  1. /*usage:sqlite3[database].dump>[filename]*/
  2. sqlite3mytable.db.dump>backup.sql

恢复数据库:

 
 
  1. /*usage:sqlite3[database]<[filename]*/
  2. sqlite3mytable.db<backup.sql

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读