sqlite3 C language API簡單例子

來源:互聯網
上載者:User

標籤:

功能:

簡單C語言使用sqlite3 API程式
注意:  (1)為了節省篇幅,程式中沒有寫處理錯誤的代碼
(2)環境centOS6.5系統,資料庫sqlite3,編譯器gcc

 

 

 

 

 

/*    檔案名稱 : test.c        用到的介面:         開啟資料庫:sqlite3_open()        關閉資料庫:sqlite3_close()        執行語句 :sqlite3_exec()*/#include <stdio.h>#include <sqlite3.h>    // sqlite3 API庫// sqlite3_exec()中使用的回呼函數static int callback_selectAll(void *, int, char **, char **);int main(void){    // 用來儲存資料庫名稱    const char * db_name = "company.db";        // 開啟資料庫,如果沒有該資料庫則建立    sqlite3 * pdb = NULL;    sqlite3_open(db_name, &pdb);        // 建立一個表格    sqlite3_exec(pdb, "create table person(id integer primary key autoincrement, name char(32) )", NULL, NULL, NULL);            // 在表格中插入資料    sqlite3_exec(pdb, "insert into person (name) values (\"shang\")", NULL, NULL, NULL);    sqlite3_exec(pdb, "insert into person (name) values (\"guan\")", NULL, NULL, NULL);    sqlite3_exec(pdb, "insert into person (name) values (\"yuan\")", NULL, NULL, NULL);    sqlite3_exec(pdb, "insert into person (name) values (\"xia\")", NULL, NULL, NULL);        // 用select * 來全部顯示    sqlite3_exec(pdb, "select * from person", callback_selectAll, NULL, NULL);        // 關閉資料庫    sqlite3_close(pdb);    return 0;}// 回呼函數中有兩個參數沒有用到// n_clumn 儲存列數 (id, name)共兩列// column_value 列值 例如 1 shang,2 guan// 其他兩個參數沒有用到int callback_selectAll(void * params, int n_clumn, char **column_value, char ** column_name){    int i = 0;        // 列印表中的值    for (; i<n_clumn; i++)    {        printf("%s    ", column_value[i]);    }    printf("\n");        return 0;}

 

# makefile檔案
# -lsqlite3是編譯連結資料庫必須的選項must=-lsqlite3 -Wall# 依賴檔案src=test.c# 編譯.PHONY:test: gcc $(src) $(must) -o test# 虛目標.PHONY:clean: rm -rf test# 在終端輸入 make test 進行編譯# 最終產生可執行檔 test# 在終端戶如 ./test 查看運行結果# 在終端輸入 make clean 清理不用的test檔案

運行過程

sqlite3 C language API簡單例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.