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 包含了 年、月、日、時、分、秒、千分之一秒。

 

參考:

 

type description
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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM