Sqlite shell 应用

记录下常用的命令,技巧

显示记录:
    .mode col
    .headers on
    select * from test limit 10

    上面的命令会打开header,并且自动排版, 像下面这样
    item_id     tag      
    ----------  ----------
    -1          css      
    -2          css      
    -4          e-ink    


插入后获取自增主键的值:
    select last_insert_rowid();   
    得到这样的数据:
    last_insert_rowid()
    -------------------
    3

 

索引操作:
    创建:
    create index test_index on test("name");
    显示:
    .indices test

 

获取数据库中所有表:
    .table
    or
    select * from sqlite_master where type="table"

 

结果输出到文件:
    .output filename.log
    该命令之后的操作,结果都打入上面的文件

 

导出整个数据库:
    .output data
    .dump

 

导入数据库:
    有两种方法可以导入数据,用哪种方法决定于要导入的文件的格式。 如果文件由SQL语句构成,可以使用.read命令导入(执行)文件。 如果文件是由逗号或其它定界符分隔的值(comma-separated values, CSV)组成, 可使用.import [file][table]命令。此命令将解析指定的文件并尝试将数据插入到指定的表中。

 

命令行备份数据库:
    可以不进入shell,直接命令行备份:
    sqlite test.db .dump > dump
    还原
    sqlite test.db<dump

posted @ 2010-12-29 14:26  hjtc  Views(635)  Comments(0Edit  收藏  举报