很高興加入開源中國社區,開源可讓咱們成長更快、更強大,若是每一個人都能多多交流。。。咱們的將來必將不可限量!毫無疑問,這並不誇張,可是如今我仍是從一個菜鳥級程序員開始入門吧!
進入正題,開始咱們的SQLite之旅吧!其實原來對SQLite一無所知,絕不誇張的能夠說是聽都沒聽過吧,一次偶然的機會我發現了這個數據庫,可是發現網上資料不是不少而後慢慢研究它,最後總結出一些小知識吧,拿出來和你們分享分享,供你們參考。。。若有錯誤,請各位大蝦多多指正!html
在該頁面上下載Source Code中的sqlite-amalgamation-3071300.zip,該包有含有兩個頭文件,兩個實現文件。ios
下載Precompiled Binaries For Windows中的sqlite-dll-win32-x86-3071300.zip,該包中含有一個def文件,一個dll文件。程序員
新建一個win32控制檯應用程序sqlite,選擇空項目。sql
方法一:將所得dll、sqlite3.h、sqlite3.lib文件拷貝到../工程/sqlite/下,點擊頭文件,選擇添加現有項,選擇拷貝的sqlite.h文件,選擇工程->屬性->連接器->輸入->附加依賴項,填寫sqlite3.lib,再在原文件中編寫本身的主程序。數據庫
方法二:將sqlite3.h、sqlite3.c、sqlite3.lib文件拷貝../工程/sqlite/下,點擊頭文件,選擇添加現有項,選擇拷貝的sqlite3.h文件。點擊源文件添加現有項,選擇拷貝的sqlite3.c文件,選擇工程->屬性->連接器->輸入->附加依賴項,填寫sqlite3.lib,而後便攜本身的主程序。數組
頭文件:my_db.h、sqlite3.h編輯器
源文件:sqlitedb.cpp、main.cpp函數
——————————————————————————————————————————————工具
My_db.h代碼:spa
#ifndef MY_DB_H
#define MY_DB_H
int open_db();
int create_table();
int drop_table();
int insert_data(int id,char *name,int age);
int search_data(int id);
int search_data(char *name);
int delete_data(int age);
#endif
——————————————————————————————————————————————
Sqlitedb.cpp代碼:
#include "sqlite3.h"
#include "my_db.h"
#include <iostream>
using namespace std;
sqlite3 *db = NULL; //定義數據庫鏈接
const char *errMsg = 0; //定義錯誤信息
char *zerrMsg = 0; //定義錯誤信息
//打開數據庫
int open_db()
{
int rc = sqlite3_open("E:/Program code/SQLite code/sqlitejdbc.db",&db); //打開數據庫
if(rc != SQLITE_OK) //數據庫打開失敗
{
errMsg = sqlite3_errmsg(db); //獲取錯誤信息
cout<<errMsg<<endl;
sqlite3_close(db); //關閉數據庫鏈接
return -1;
}
cout<<"open database successfully!"<<endl;
return 0;
}
//建立表
int create_table()
{
if(open_db() != 0)
{
open_db();
}
char *sql = "create table tab(id int primary key ,name varchar(20) ,age int)";
/**
//執行建立表語句 參數一:數據庫鏈接 參數二:須要執行的SQL語句 參數三:回調函數 參數四:回調函數的第一個參數 參數五:錯誤消息。
回調函數的格式以下:
int sqlite_callback(
void* pvoid, 由 sqlite3_exec() 的第四個參數傳遞而來
int argc, 表的列數
char** argv, 指向查詢結果的指針數組, 能夠由 sqlite3_column_text() 獲得
char** col 指向表頭名的指針數組, 能夠由 sqlite3_column_name() 獲得
);
說明:
sqlite3_exec() 的回調函數必須按照此格式, 固然形參的名字任意.
若是某列的數據類型不是char*, 則能夠對結果執行相關的轉換, 如:用atoi()把結果轉換爲整數(integer), 若是是二進制數據, 則能夠直接強制類型轉換, 如:(void*)argv[i].
該回調函數有兩種返回值類型.
1.返回零:sqlite3_exec() 將繼續執行查詢.
2.返回非零:sqlite3_exec()將當即中斷查詢, 且 sqlite3_exec() 將返回 SQLITE_ABORT.
*/
int rc = sqlite3_exec(db,sql,NULL,NULL,&zerrMsg);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl; // cout<<zerrMsg<<endl;
sqlite3_close(db);
return -1;
}
cout<<"建立表成功!"<<endl;
return 0;
}
//刪除表
int drop_table()
{
if(open_db() != 0)
{
open_db();
}
char *sql = "drop table tab";
int rc = sqlite3_exec(db,sql,NULL,NULL,&zerrMsg);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl; // cout<<zerrMsg<<endl;
sqlite3_close(db);
return -1;
}
cout<<"刪除表成功"<<endl;
return 0;
}
//數據添加
int insert_data(int id,char *name,int age)
{
if(open_db() != 0)
{
open_db();
}
sqlite3_stmt *stmt = NULL; //準備語句對象
char *sql = "insert into tab(id,name,age) values(?,?,?)";
/**
參數一:數據庫鏈接 參數二:須要執行的SQL語句 參數三:SQL的長度 參數四:準備語句對象
參數三:若是nByte小於0,則函數取出zSql中從開始到第一個0終止符的內容;若是nByte不是負的,那麼它就是這個函數能從zSql中讀取的字節數的最大值。若是nBytes非負,zSql在第一次碰見’/000/或’u000’的時候終止
參數五:上面提到zSql在碰見終止符或者是達到設定的nByte以後結束,假如zSql還有剩餘的內容,那麼這些剩餘的內容被存放到pZTail中,不包括終止符
*/
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,id);
//參數一:準備語句對象 參數二:序號(從1開始)參數三:字符串值 參數四:字符串長度 參數五:函數指針,SQLITE3執行完操做後回調此函數,一般用於釋放字符串佔用的內存。(這個函數指針參數具體怎麼使用,我如今還不清楚
sqlite3_bind_text(stmt,2,name,strlen(name),NULL);
sqlite3_bind_int(stmt,3,age);
/**
statement準備好了之後,就是操做的執行了
它的返回值相對有些特殊。返回SQLITE_BUSY表示暫時沒法執行操做,SQLITE_DONE表示操做執行完畢(執行update, delete, insert等語句),
SQLITE_ROW表示執行完畢而且有返回(執行select語句時)。當返回值爲SQLITE_ROW時,咱們須要對查詢結果進行處理,
SQLITE3提供sqlite3_column_*系列函數。
*/
if(sqlite3_step(stmt) != SQLITE_DONE)
{
sqlite3_finalize(stmt);
sqlite3_close(db);
return -1;
}
cout<<"數據插入成功!"<<endl;
sqlite3_reset(stmt);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//數據查詢 根據id惟一性查詢
int search_data(int id)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "select * from tab where id = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,id);
int nColumn = sqlite3_column_count(stmt); //獲取數據庫表的列數
int type; //表字段所對應的類型
rc = sqlite3_step(stmt);
if(rc == SQLITE_ROW)
{
for(int i=0;i<nColumn;i++)
{
type = sqlite3_column_type(stmt,i);
if(type == SQLITE_INTEGER)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<<sqlite3_column_int(stmt,i)<<endl;
}
if(type == SQLITE_TEXT)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<<sqlite3_column_text(stmt,i)<<endl;
}
else if(type == SQLITE_NULL)
{
cout<<"no value"<<endl;
}
}
}
else if(rc == SQLITE_DONE)
{
cout<<"select finish!"<<endl;
}
else
{
cout<<"select fail"<<endl;
sqlite3_finalize(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//數據查詢 根據姓名批量查詢
int search_data(char *name)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "select * from tab where name = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_text(stmt,1,name,strlen(name),NULL);
int nColumn = sqlite3_column_count(stmt);
int type;
rc = sqlite3_step(stmt);//若是是select語句,且有還有記錄,則應該返回SQLITE_ROW
while(true) {
if(rc == SQLITE_ROW)
{
cout<<"--------------"<<endl;
for(int i=0;i<nColumn;i++)
{
type = sqlite3_column_type(stmt,i);
if(type == SQLITE_INTEGER)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<< sqlite3_column_int(stmt,i)<<endl;
}
if(type == SQLITE_TEXT)
{
cout<<sqlite3_column_name(stmt,i)<<"\t"<< sqlite3_column_text(stmt,i)<<endl;
}
else if(type == SQLITE_NULL)
{
cout<<"no value"<<endl;
}
}
}
else if(rc == SQLITE_DONE)
{
cout<<"select finish!"<<endl;
}
else
{
cout<<"select fail"<<endl;
sqlite3_finalize(stmt);
}
if (sqlite3_step(stmt) != SQLITE_ROW)
break;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
//刪除數據
int delete_data(int age)
{
if(open_db() != 0)
{
open_db();
}
char *sql = "delete from tab where age = ?";
sqlite3_stmt *stmt = NULL;
int rc = sqlite3_prepare_v2(db,sql,strlen(sql),&stmt,NULL);
if(rc != SQLITE_OK)
{
errMsg = sqlite3_errmsg(db);
cout<<errMsg<<endl;
if(stmt)
{
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return -1;
}
sqlite3_bind_int(stmt,1,age);
rc = sqlite3_step(stmt);//若是是update, delete, insert等語句,正常應該返回 SQLITE_DONE
if(rc == SQLITE_DONE)//SQLITE_DONE意味着已成功完成執行該語句
{
cout<<"刪除數據成功"<<endl;
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
——————————————————————————————————————————————
main.cpp代碼:
#include "my_db.h"
#include <iostream>
using namespace std;
int main()
{
//open_db();
//create_table();
//drop_table();
//insert_data(3,"wang",25);
//search_data(1);
//search_data("wang");
//delete_data(28);
return 0;
}
——————————————————————————————————————————————
最後再爲你們推薦幾款管理工具,但願對你們有更好地幫助。
管理工具也有很多,這裏介紹幾款:
1、SQLite Manager是開放源代碼的SQLite管理工具,用來管理本地電腦上的SQLite數據庫,能夠獨立運行(以XULRunner方式),也能夠做爲Firefox、Thunderbird、Seamonkey、Songbird、Komodo、Gecko等的插件。
2、SQLite Administrator是一個用來管理 SQLite 數據庫文件的圖形化工具,可進行建立、設計和管理操做。提供代碼編輯器具備自動完成和語法着色,支持中文,適合初學者。
3、SQLite Database browser是一個 SQLite 數據庫的輕量級GUI客戶端,基於Qt庫開發,界面清潔,操做簡單,主要是爲非技術用戶建立、修改和編輯SQLite數據庫的工具,使用嚮導方式實現。