怎样从一维数组里读数据并存到sqlite数据库中
我要将a[10]数组的数据插入到表中的sensorid字段中,其他字段直接赋值,程序如下,但是不显示,编译过了,但是在数据库中不显示,如果直接赋初值,是可以显示的
#include <stdio.h>
#include "sqlite3.h"
#pragma comment(lib,"sqlite3.lib")
int main( int argc, char* argv[] )
{
int nRet;
int a[10] = {11,22,33,44,55,66,77,88};
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 , 0 , 0 , &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 , a[1] , 1 , '200605011206', 18.9 );" ;
nRet = sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Insert Error:%s", zErrMsg);
}else
{
printf("Insert OK\n");
}
//insert data
sql = "INSERT INTO \"SensorData\" VALUES(NULL , a[2] , 1 , '200605011306', 16.4 );" ;
nRet = sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Insert Error:%s", 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 , sql , 0 , 0 , &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 , sql , 0 , 0 , &zErrMsg );
if ( 0 != nRet )
{
printf("Delete Error:%s", zErrMsg);
}else
{
printf("Delete OK\n");
}
sqlite3_free_table( azResult );
//关闭数据库
sqlite3_close( db );
return 0;
}
[解决办法]
sqlite3_prepare();
sqlite3_bind_int();
用这两个函数,在前面已经回复你了