Sqlite3入门简记

Posted cunning007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sqlite3入门简记相关的知识,希望对你有一定的参考价值。

一,安装Sqlite3

1.入门时看http://www.runoob.com/sqlite/sqlite-intro.html,说的简单,但是适合入门

2.在终端输入sqlite3,没有返回信息,表示系统没有安装sqlite3,否则系统已安装(系统一般附带有安装)

3.需要自己安装时,到http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz下载,

 然后:(1) tar -zxvf sqlite-autoconf-3070500.tar.gz

    (2) cd  sqlite-autoconf-3070500

    (3)  ./configure --prefix=/xx/xxxx(/xx/xxxx表示文件生成目录)

    (4)  make & make install (成功后在/xx/xxxx目录下生成(bin include lib share 4个目录))

        其中bin放置可执行文件sqlite3,./sqlite3 可进入sqlite命令行界面

        include放置头文件

        lib放置库文件

 

二,用的较多的命令

    (1)获取点命令清单:.help

    (2)退出提示符:.quit     .exit

    (3)输出到屏幕:output stdout

    (4)列出数据库名称:.databases

    (5)以SQL文本格式转存数据库:.dump

 

三,用的较多的存储类

    NULL、INTEGER、REAL、TEXT

 

四,很有用的语法

    (1)创建数据库:sqlite3 xxx.db

    (2)创建表

    (3)删除表

    (4)INSERT INTO语法

    (5)SELECT语法

    (6)WHERE语法

    (7)UPDATE语法

    (8)DELETE语法

 

 

五,Sqlite3 API 使用

    (1)最重要的三个函数:

      int sqlite3_open(const char*, sqlite3**);

      int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**); 

      int sqlite3_close(sqlite3*);

      还有2个:

          const char *sqlite3_errmsg(sqlite3*);

          void sqlite3_free(void*);

      查看更多函数,下载sqlite源码包,只需要其中的sqlite3.c、sqlite.h即可。


    (2)sqlite3_open 返回一个整数错误代码,=0 表示成功码,> 0都是错误码,详情看Sqlite3手册说明

      函数用于打开/创建一个函数库

      const char* 指定文件名,sqlite3** 指定数据库句柄,用户通过数据库句柄操作数据库

      

 

    (2)sqlite3_exec返回0表示sql指令执行完毕,否则说明这次执行没有成功

      函数用于执行一条或多条SQL语句,SQL语句之间用“;”隔开

      sqlite3*指定已打开的数据库句柄,const char *sql   指定SQL指令,sqlite_callback  在回调函数中可以获得SQL执行的结果

      void*  指定传给回调函数的数据 , char**   指定命令执行失败的详细错误信息

    (3)回调函数

      

    (4) sqlite3_close   关闭数据库文件,参数是数据库句柄

    (5)sqlite3_errmsg  返回错误码所对应的文字说明,参数是数据库句柄

    (6)sqlite3_free  释放存放错误信息的内存空间,sqlite3_errmsg  返回的errmsg必须用此函数释放

    (7)简单测试代码

      

 1 #include <stdio.h>
 2 #include <sqlite3.h>
 3 
 4 int callback(void *pv,int argc,char **argv,char **col)
 5 {
 6     int cnt_i = 0;
 7     for(cnt_i =0;cnt_i < argc;cnt_i++)
 8     {
 9         printf("%s\\t%s\\n",col[cnt_i],argv[cnt_i]);
10     }
11     printf("\\n");
12     return 0;
13 }
14 
15 int main(void)
16 {
17     sqlite3 *db;
18     int result = 0;
19     char *rerrmsg = NULL;
20     char *sql = NULL;
21     char *data = "callback";
22 
23     result = sqlite3_open("sample.db",&db);
24     if(result > 0)
25     {
26         printf("open database err:%s\\n",sqlite3_errmsg(db));
27         return -1;
28     }
29 
30     else
31     {
32         printf("open database successfully!\\n");
33 
34         sql = "CREATE TABLE STUDENT("    \\
35               "NUM INT PRIMARY KEY NOT NULL,"    \\
36               "NAME TEXT NOT NULL,"        \\
37               "AGE INT NOT NULL,"        \\
38               "SORCE REAL);";
39 
40         result = sqlite3_exec(db,sql,callback,NULL,&rerrmsg);
41         if(result != 0)
42         {
43             printf("creat table err:%s\\n",rerrmsg);
44             sqlite3_free(rerrmsg);
45             return -2;
46         }
47 
48         else
49         {
50             printf("create table successfully!\\n");
51 
52             sql = "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)"        \\
53                   "VALUES(1,\'Paul\',13,99.1);"                    \\
54                   "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)"        \\
55                   "VALUES(2,\'Kate\',15,94.1);"                    \\
56                   "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)"        \\
57                   "VALUES(3,\'Jim\',12,95.1);"                    \\
58                   "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)"        \\
59                   "VALUES(4,\'Tom\',13,99.4);"                    \\
60                   "INSERT INTO STUDENT(NUM,NAME,AGE,SORCE)"        \\
61                   "VALUES(5,\'Jack\',13,89.1);";
62 
63             result = sqlite3_exec(db,sql,callback,NULL,&rerrmsg);
64             if(result != 0)
65             {
66                 printf("insert data err:%s\\n",rerrmsg);
67                 sqlite3_free(rerrmsg);
68                 return -3;
69             }
70 
71             else
72             {
73                 printf("insert data successfully!\\n");
74 
75                 sql = "SELECT * FROM STUDENT";
76                 result = sqlite3_exec(db,sql,callback,(void *)data,&rerrmsg);
77                 if(result != 0)
78                 {
79                     printf("select data err:%s\\n",rerrmsg);
80                     sqlite3_free(rerrmsg);
81                     return -4;
82                 }
83 
84                 else
85                 {
86                     printf("select data successfully!\\n");
87                 }
88             }
89         }
90     }
91 
92     sqlite3_close(db);
93 
94     return 0;
95 }
View Code

 

 

六,图形界面管理工具

     SQLite Expert - Personal Edition ,简单入门,直接导入数据库文件即可

 

 

以上,

2017/03/30

 

Sqlite3支持的数据类型

NULL,INTEGER,REAL,TEXT,BLOB

以及:
smallint 16 位元的整数。
interger 32 位元的整数。
decimal(p,s) p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数点後有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。
float   32位元的实数。
double   64位元的实数。
char(n)   n 长度的字串,n不能超过 254。
varchar(n) 长度不固定且其最大长度为 n 的字串,n不能超过 4000。
graphic(n) 和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为了支援两个字元长度的字体,例如中文字。
vargraphic(n) 可变长度且其最大长度为 n 的双字元字串,n不能超过 2000
date   包含了 年份、月份、日期。
time   包含了 小时、分钟、秒。

timestamp 包含了 年、月、日、时、分、秒、千分之一秒。

 

参考:

 

typedescription
TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB, BYTEA String types of unlimited length. Binary data must be safely encoded, see text.
CHAR(), VARCHAR(), TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT String types of unlimited length. There is no chopping or padding performed by the database engine.
ENUM String type of unlimited length. In contrast to mysql, choosing ENUM over VARCHAR does not save any storage space.
SET String type of unlimited length. In contrast to MySQL, the input is not checked against the list of allowed values.
YEAR String type of unlimited length. MySQL stores 2 or 4 digit years as a 1 byte value, whereas the SQLite drivers stores the string as provided.
TINYINT, INT1, CHAR A 1 byte type used to store one character, a signed integer between -128 and 127, or an unsigned integer between 0 and 255.
SMALLINT, INT2 2 byte (short) integer type used to store a signed integer between -32768 and 32767 or an unsigned integer between 0 and 65535.
MEDIUMINT 3 byte integer type used to store a signed integer between -8388608 and 8388607 or an unsigned integer between 0 and 16777215.
INT, INTEGER, INT4 4 byte (long) integer type used to store a signed integer between -2147483648 and 2147483647 or an unsigned integer between 0 and 4294967295.
BIGINT, INT8, INTEGER PRIMARY KEY 8 byte (long long) integer type used to store a signed integer between -9223372036854775808 and 9223372036854775807 or an unsigned integer between 0 and 18446744073709551615. See below for a discussion of INTEGER PRIMARY KEY.
DECIMAL, NUMERIC A string type of unlimited length used to store floating-point numbers of arbitrary precision.
TIMESTAMP, DATETIME A string type of unlimited length used to store date/time combinations. The required format is \'YYYY-MM-DD HH:MM:SS\', anything following this pattern is ignored.
DATE A string type of unlimited length used to store a date. The required format is \'YYYY-MM-DD\', anything following this pattern is ignored.
TIME A string type of unlimited length used to store a time. The required format is \'HH:MM:SS\', anything following this pattern is ignored.
FLOAT, FLOAT4, REAL A 4 byte floating-point number. The range is -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. Please note that MySQL treats REAL as an 8 byte instead of a 4 byte float like PostgreSQL.
DOUBLE, DOUBLE PRECISION, FLOAT8 An 8 byte floating-point number. The range is -1.7976931348623157E+308 to -2.2250738585072014E-308, 0, and 2.2250738585072014E-308 to 1.7976931348623157E+308.

以上是关于Sqlite3入门简记的主要内容,如果未能解决你的问题,请参考以下文章

jQuery入门简记(增删改搜)

Dagger2入门简记

Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段

Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段

sqlite3入门之sqlite3_get_table,sqlite3_free_table

推荐net开发cad入门阅读代码片段