加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在工程中使用SQLite3

发布时间:2020-12-12 23:35:47 所属栏目:百科 来源:网络整理
导读:#include Stdio.h //包含SQLite3头文件 #include "sqlite3.h" //连接sqlite3库文件 #pragma comment(lib,"sqlite3.lib") int main( int argc,char* argv[] ) { int nRet; sqlite3 *db=NULL; nRet = sqlite3_open( "xx.db",db ); if ( 0 != nRet ) { printf("

#include <Stdio.h>

//包含SQLite3头文件

#include "sqlite3.h"

//连接sqlite3库文件

#pragma comment(lib,"sqlite3.lib")

int main( int argc,char* argv[] )

{

int nRet;

sqlite3 *db=NULL;

nRet = sqlite3_open( "xx.db",&db );

if ( 0 != nRet )

{

printf("Open db error %s/n",sqlite3_errmsg(db));

sqlite3_close( db );

}else

{

printf( "open db success/n" );

}

//create a table

char *zErrMsg = 0;

char *sql = " CREATE TABLE SensorData( ID INTEGER PRIMARY KEY,SensorID INTEGER,SiteNum INTEGER,Time VARCHAR(12),SensorParameter REAL);";

printf( "%s/n",sql );

nRet = sqlite3_exec( db,sql,&zErrMsg );

if ( 0 != nRet )

{

printf("Create table falure,MSG:%s/n",zErrMsg);

}else

{

printf( "Create table success/n" );

}

//insert data

sql = "INSERT INTO /"SensorData/" VALUES(NULL,1,'200605011206',18.9 );" ;

nRet = sqlite3_exec( db,&zErrMsg );

if ( 0 != nRet )

{

printf("Insert Error:%s",zErrMsg);

}else

{

printf("Insert OK/n");

}

//insert data

sql = "INSERT INTO /"SensorData/" VALUES(NULL,'200605011306',16.4 );" ;

nRet = sqlite3_exec( db,zErrMsg);

}else

{

printf("Insert OK/n");

}

//update data

sql = "UPDATE SensorData SET SensorParameter=1000.01,SensorID=123 WHERE SensorParameter=100.01";

nRet = sqlite3_exec( db,&zErrMsg );

if ( 0 != nRet )

{

printf("Update Error:%s",zErrMsg);

}else

{

printf("Update OK/n");

}

//delete data

sql = "DELETE FROM SensorData WHERE SensorID=123";

nRet = sqlite3_exec( db,&zErrMsg );

if ( 0 != nRet )

{

printf("Delete Error:%s",zErrMsg);

}else

{

printf("Delete OK/n");

}

//query data

int nrow = 0,ncolumn = 0;

char **azResult; //二维数组存放結果

sql = "SELECT * FROM SensorData ";

sqlite3_get_table( db,&azResult,&nrow,&ncolumn,&zErrMsg );

int i = 0 ;

printf( "row:%d column=%d /n",nrow,ncolumn );

printf( "/nThe result of querying is : /n" );

for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )

printf( "azResult[%d] = %s/n",i,azResult[i] );

//释放azResult 的內存空間

sqlite3_free_table( azResult );

//关闭数据库

sqlite3_close( db );

return 0;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读