织梦CMS - 轻松建站从此开始!

罗索

SQLite入门

落鹤生 发布于 2012-03-23 11:33 点击:次 
One way to use sqlite3 in a shell script is to use “echo” or “cat” to generate a sequence of commands in a file, then invoke sqlite3 while redirecting input from the generated command file. This works fine and is appropriate in many circumstances
TAG:

一. 在shell中导入数据到sqlite3

官方的例子如下

Using sqlite3 in a shell script

One way to use sqlite3 in a shell script is to use “echo” or “cat” to generate a sequence of commands in a file, then invoke sqlite3 while redirecting input from the generated command file. This works fine and is appropriate in many circumstances. But as an added convenience, sqlite3 allows a single SQL command to be entered on the command line as a second argument after the database name. When the sqlite3 program is launched with two arguments, the second argument is passed to the SQLite library for processing, the query results are printed on standard output in list mode, and the program exits. This mechanism is designed to make sqlite3 easy to use in conjunction with programs like “awk”. For example:

    $ sqlite3 ex1 ’select * from tbl1′ |

    > awk ’{printf ”<tr><td>%s<td>%s\n”,$1,$2 }’

    <tr><td>hello<td>10

    <tr><td>goodbye<td>20

    $

    我自己的例子

    sqlite3 ex1.db ‘insert into table1 values(1,”ere”,”erer”,1,”dfdfd”)’;

    注意要把sql语句用单引号括起来,所以原来sql里面单引号相应变换成双引号,搞定。

    如果想调用shell 变量,则需要将变量先单引号再双引号

    sqlite3 ex1.db ‘insert into table1 values(”‘$a’”,”‘$a’”,1,”‘$a’”);’

    但是一旦shell变量里面有单引号,比如I’m jack,那么上述写法就会出错。因为在shell里面不能简单的通过\’ 来转义

    所以只能更改变量 echo $var | sed “s/’/”/g” 将一个单引号改成两个,I”m jack 这样才能正常工作

    其他sqlite的特别用法

    sqlite可以在shell底下直接执行命令:

    sqlite3 film.db "select * from film;"

    输出 HTML 表格:

    sqlite3 -html film.db "select * from film;"

    将数据库「倒出来」:

    sqlite3 film.db ".dump" > output.sql

    利用输出的资料,建立一个一模一样的数据库(加上以上指令,就是标准的SQL数据库备份了):

    sqlite3 film.db < output.sql

    在大量插入资料时,你可能会需要先打这个指令:

    begin;

    插入完资料后要记得打这个指令,资料才会写进数据库中:

    commit;

    在windows下面导入txt数据

    sqlite3 c:\\text.db3 ".import c:\\test.txt test_table"

    如果提示导入不正确,可能是分隔符错误。可以用.show命令查看分隔符。然后用.separator命令来

    指定分隔符,如.separator ","指定用逗号作为分隔符

    sqlite3 c:\\text.db3 ".separator ',' " ".import c:\\test.txt test_table"

    -------------------------------------------------------------

    记录下常用的命令,技巧

    显示记录:

        .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

(HelloPigg)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201203/15866.html]
本文出处:新浪博客 作者:HelloPigg 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容