Ⅰ 請教如何把sqlite嵌入到我的c程序里去
示例代碼:
// name: query.c
// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
// Author : zieckey All rights reserved.
// data : 2006/11/18
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define _DEBUG_
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打開指定的資料庫文件,如果不存在將創建一個同名的資料庫文件
if( rc )
{
fprintf(stderr, "Can't open database: %s
", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
");
//創建一個表,如果該表存在,則不創建,並給出提示信息,存儲在 zErrMsg 中
char *sql = " CREATE TABLE SensorData(
ID INTEGER PRIMARY KEY,
SensorID INTEGER,
SiteNum INTEGER,
Time VARCHAR(12),
SensorParameter REAL
);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
printf("zErrMsg = %s
", zErrMsg);
#endif
//插入數據
sql = "INSERT INTO "SensorData" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "INSERT INTO "SensorData" VALUES(NULL , 1 , 1 , '200605011306', 16.4 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
int nrow = 0, ncolumn = 0;
char **azResult; //二維數組存放結果
//查詢數據
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
int i = 0 ;
printf( "row:%d column=%d
" , nrow , ncolumn );
printf( "
The result of querying is :
" );
Ⅱ sqlite3 怎麼查詢資料庫中所有的表 用C語言實現
SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;
在C語言中用這個查詢語句
Ⅲ sqlite3中用c語言寫資料庫的時候,假如我建立一個學生信息庫時候,在現查詢功能時,如果沒有這個
select * from yourtabel where xuehao = yournum
不存在 會返回空
Ⅳ c語言實現sqlite3設計學生信息管理系統
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
voidlook();/*聲明查看函數*/
voidsave();/*聲明保存函數*/
voidsearch_name();/*聲明按姓名查看函數*/
voidsearch_number();/*聲明按學號查看函數*/
voidorder();/*聲明排序函數*/
voiddel();/*聲明刪除函數*/
intopenl();/*聲明打開函數*/
voidwelcome();/*聲明我的個人信息函數*/
voidtype();/*聲明輸入函數*/
voidsee();/*聲明打開並查看文件函數*/
structstudent/*定義學生信息的結構體類型*/
{
charnum[15];/*學號*/
Ⅳ 怎麼用C語言動態往sqlite3裡面插入數據
首先說明這個問題困擾了我很長時間了,嚴格地說應該有兩天,不過終於通過sqlite的官方文檔解決了。
For example, assume the string variable zText contains text as follows:
char *zText = "It's a happy day!";
One can use this text in an SQL statement as follows:
char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText); sqlite3_exec(db, zSQL, 0, 0, 0); sqlite3_free(zSQL);
摘自liudong123的專欄
Ⅵ SQLITE資料庫C語言API 想使當資料庫不存在時 sqlite3_open 不創建資料庫
改用sqlite3_open_v2函數打開
int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags,const char *zVfs);
前兩個參數與sqllite3_open一樣,flags設置為SQLITE_OPEN_READWRITE,如果資料庫不存在就不創建,只返回一個error
參數zVfs允許應用程序命名一個虛擬文件系統(Virtual File System)模塊,用來與資料庫連接。VFS作為SQlite library和底層存儲系統(如某個文件系統)之間的一個抽象層,通常客戶應用程序可以簡單的給該參數傳遞一個NULL指針,以使用默認的VFS模塊。
Ⅶ 怎樣用C語言訪問Sqlite資料庫
SQLITE 官網就有例子、文檔,,,,,,,SQLITE本身就是C寫的
Ⅷ C語言如何調用SQLite3中的介面
下載 sqlite3 庫,放在項目文件夾中,包含 sqlite3.h 頭文件,包含 sqlite3.lib 導入庫,將 sqlite3.dll 復制到 .exe 所在目錄。
代碼如下:
#include<stdio.h>
#include<stdlib.h>
//包含sqlite3頭文件
#include"../sqlite-3.24.0/include/sqlite3.h"
//添加sqlite3lib庫
#pragmacomment(lib,"../sqlite-3.24.0/x86/sqlite3.lib")
inttable_exist_callback(void*data,intargc,char**argv,char**azColName)
{
intval=atoi(argv[0]);
if(val==0){
return-1;
}
return0;
}
intprint_callback(void*data,intargc,char**argv,char**azColName)
{
for(inti=0;i<argc;i++){
printf("%s=%s ",azColName[i],argv[i]);
}
printf(" ");
return0;
}
intmain()
{
sqlite3*db=NULL;
char*sql=NULL;
char*errMsg=NULL;
//打開資料庫
intret=sqlite3_open("data.db",&db);
if(ret!=SQLITE_OK){
printf("打開數據失敗,%s ",sqlite3_errmsg(db));
exit(-1);
}
//判斷students是否存在,如果不存在,則創建表students
sql="selectcount(*)fromsqlite_masterwheretype='table'andname='students';";
ret=sqlite3_exec(db,sql,table_exist_callback,NULL,NULL);
if(ret!=SQLITE_OK){
//創建students表
sql="createtablestudents(numbervarchar(10),namevarchar(20),ageinteger);";
ret=sqlite3_exec(db,sql,NULL,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("創建表失敗,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
}
//插入數據到students表
sql="insertintostudents(number,name,age)values('100001','Barry',23);"
"insertintostudents(number,name,age)values('100002','Oliver',33);";
ret=sqlite3_exec(db,sql,NULL,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("插入表數據失敗,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
//查詢students表數據
sql="select*fromstudents";
ret=sqlite3_exec(db,sql,print_callback,NULL,&errMsg);
if(ret!=SQLITE_OK){
printf("查詢數據失敗,%s. ",errMsg);
sqlite3_close(db);
sqlite3_free(errMsg);
exit(-1);
}
//關閉資料庫
sqlite3_close(db);
system("pause");
return0;
}
項目源碼:網頁鏈接
Ⅸ 用C語言做個sqlite資料庫~
). 打開VC新建一個「Win32 Dynamic-Link Library」工程,命名為:sqlite32). 在接下來的對話框中選擇"An empty DLL project",點 FINISH->OK3). 將源碼中所有的 *.c *.h *.def 復制到工程文件夾下4). 在工程的Source File中添加你下載到的SQLite源文件中所有*.c文件,注意這里不要添加shell.c和tclsqlite.c這兩個文件。5). 將 SQLite 源文件中的 sqlite3.def 文件添加到在工程的Source File中6). 在Header File中添加你下載到的SQLite源文件中所有*.h文件,7). 開始編譯,Build(F7)一下也許到這里會遇到一個錯誤:e:\zieckey\sqlite\sqlite3\sqlite3ext.h(22) : fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory經檢查發現,源碼中包含sqlite3.h都是以 #include <sqlite3.h> 方式包含的,這就是說編譯器在系統默認路徑中搜索,這樣當然搜索不到 sqlite3.h 這個頭文件啦,這時可以改為 #include "sqlite3.h" ,讓編譯器在工程路徑中搜索,但是如果還有其他地方也是以 #include <sqlite3.h> 方式包含的,那麼改源碼就顯得有點麻煩,好了,我們可以這樣,在菜單欄依次選擇:Tools->Options...->Directeries在下面的Directeries選項中輸入你的 sqlite3.h 的路徑,這里也就是你的工程目錄.添加好後,我們在編譯一下就好了,最後我們在工程目錄的 Debug 目錄生成了下面兩個重要文件:動態鏈接庫文件 sqlite3.dll 和引入庫文件 sqlite3.lib二. 使用動態鏈接庫下面我們來編寫個程序來測試下我們的動態鏈接庫.在VC下新建一個空的"Win32 Console Application" Win32控制台程序,工程命名為:TestSqliteOnWindows再新建一個 test.cpp 的C++語言源程序,源代碼如下:// name: test.cpp// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !// Author : zieckey// data : 2006/11/28#include <stdio.h>#include <stdlib.h>#include "sqlite3.h" #define _DEBUG_int main( void ){sqlite3 *db=NULL;char *zErrMsg = 0;int rc;rc = sqlite3_open("zieckey.db", &db); //打開指定的資料庫文件,如果不存在將創建一個同名的資料庫文件if( rc ){fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));sqlite3_close(db);return (1);}else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
Ⅹ 在C語言下,如何備份一個sqlite3資料庫,如何使用備份命令操作能給一段代碼最好,謝謝!
sqlite3資料庫就是一個文件,將該文件拷貝一份即完成備份。