嵌入式 sqlite3数据库创建、插入、更新、查询、删除、多线程等C
/* Author : kj Time : 2014-09-07 Function : joseph cvr manage by sqlite3 db */ #include <fcntl.h> #include <stdio.h> #include <ctype.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <unistd.h> #include <signal.h> #include <dirent.h> #include <pthread.h> #include <asm/types.h> #include <arpa/inet.h> #include <sys/vfs.h> #include <sys/time.h> #include <sys/wait.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/utsname.h> #include <netdb.h> #include <net/if.h> #include <netinet/in.h> #include <net/route.h> #include <net/if_arp.h> #include <linux/fs.h> #include <linux/sockios.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ether.h> #include <netinet/ip_icmp.h> #include "sqlite3.h" #define JDB_SQLITE3 sqlite3 #define JDB_CVR_NAME "JIPNC_CVR.DB" #define JDB_CVR_CONFIG_TABLE_NAME "JIPNC_CVR_CFG" #define JDB_CVR_RECORD_TABLE_NAME "JIPNC_CVR_RECORD" #define JDB_CVR_CONFIG_INTACT_THRESHOLD 28 #define JDB_CVR_DB_DEFAULT_VALUES "0" #define JDB_CVR_CONFIG_PKEY_ONE "cWeekDay" #define JDB_CVR_CONFIG_PKEY_TWO "cSection" #define JDB_CVR_RECORD_PKEY "cStSeconds" /* the member of cvr config table */ typedef struct Joseph_Db_Cvr_Config { char cWeekDay[4];//range 1 ~ 7 not null primary key char cSection[4];//range 1 ~ 4 not null primary key char cStpSection[64];//start time per section,contaion hour minute second char cEtpSection[64];//end time per section,contaion hour minute second char cCvrType[4];//0~? 0 ~ plan,1 ~ md alarm; char cGDayPlan[4];//0 ~ close global;1 ~ open char cCvrStaus[4];//0 ~ novalid;1 ~ valid }JOSEPH_DB_CVR_CONFIG; #define JOSEPH_CREATE_CVRCONFFIG_CMD "create table IF NOT EXISTS %s( %s char(4) NOT NULL, %s char(4) NOT NULL, %s char(64) default 0, %s char(4) default 0, constraint PK_WS primary key(cWeekDay,cSection) )" #define JOSEPH_DB_CVRCONFIG_FIELD JDB_CVR_CONFIG_TABLE_NAME, "cWeekDay", "cSection", "cStpSection", "cEtpSection", "cCvrType", "cGDayPlan", "cCvrStaus" #define JOSEPH_INSERT_CVRCONFIG_DEFAULT_CMD "insert into %s(%s,%s) values("%s","%s")" #define JOSEPH_UPDATE_CVRCONFIG_CMD "update %s set cStpSection = "%s", cEtpSection = "%s", cCvrType = "%s", cGDayPlan = "%s", cCvrStaus = "%s" where %s = %s and %s = %s " #define JOSEPH_SELECT_CVRCONFIG_CMD "select * from %s where %s = %s and %s = %s " #define JOSEPH_SELECT_CFG_ONE_CMD "select %s from %s where %s = %s and %s = %s " #define JOSEPH_SELECT_CFG_TWO_CMD "select %s,%s from %s where %s = %s and %s = %s " /* the member of cvr record table */ typedef struct Joseph_Db_Cvr_Record { char cStSeconds[64];//start total second,that relative to 1970-01-01 08:00:00 char cEtSeconds[64];//start total second,that relative to 1970-01-01 08:00:00 char cDirFileName[256];//the cvr record file dir and name char cFileSpShift[4];//real cvr file start addr shift in large file char cFileEpShift[4];//real cvr file end addr shift in large file char cFileStatus[4];//the file is exist or not,check when client requst history record list,and update sql values char cFileIntact[4];// make sure avi or mp4 intact }JOSEPH_DB_CVR_RECORD; #define JOSEPH_CREATE_CVRRECORD_CMD "create table IF NOT EXISTS %s( %s char(64) NOT NULL, %s char(64) NOT NULL, %s char(256) NOT NULL default 0, %s char(4) default 0 )" #define JOSEPH_DB_CVRRECORD_FIELD JDB_CVR_RECORD_TABLE_NAME, "cStSeconds", "cEtSeconds", "cDirFileName", "cFileSpShift", "cFileEpShift", "cFileStatus", "cFileIntact" #define JOSEPH_INSERT_CVRRECORD_CMD "insert into %s values("%s","%s","%s")" #define JOSEPH_UPDATE_CVRRECORD_CMD "update %s set cEtSeconds = "%s", cFileEpShift = "%s", cFileStatus = "%s", cFileIntact = "%s" where cStSeconds = %s " #define JOSEPH_SELECT_CVRRECORD_CMD "select * from %s where %s = %s " #define JOSEPH_SELECT_CRD_ONE_CMD "select %s from %s where %s = %s " #define JOSEPH_SELECT_CRD_TWO_CMD "select %s,%s from %s where %s = %s " #define JOSEPH_UPDATE_ONE_CMD "update %s set %s = "%s" where %s = %s and %s = %s " #define JOSEPH_UPDATE_TWO_CMD "update %s set %s = "%s", %s = "%s" where %s = %s and %s = %s " #define JOSEPH_DELETE_CMD "delete from %s where %s = %s" #define JOSEPH_SELECT_CMD "select * from %s" JDB_SQLITE3 *GP_JosephDB = NULL; typedef struct { pthread_t thread; int ( *func)(void *);//function pointer *alter by kj void *param;//send the param to the function *alter by kj char lock; } tsk_args; void *tmp_func(void *in) { tsk_args tmp,*par; par=in; memcpy(&tmp,in,sizeof(tsk_args)); par->lock = 0; tmp.func(tmp.param); pthread_exit(NULL);//father thread exit *alter by kj return NULL; } void run_pthread(void *func,void *param) { tsk_args tmp_pa; pthread_t thread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); tmp_pa.func = func; tmp_pa.param = param; tmp_pa.lock = 1; pthread_create(&thread,&attr,(void *)&tmp_func,(void *)&tmp_pa); while(tmp_pa.lock){}; return; } /* Author : kj Time : 2014-09-07 Function : get db point of cvr.db */ JDB_SQLITE3 *Joseph_Get_DB_Point(void) { return GP_JosephDB; } /* Author : kj Time : 2014-09-07 Function : Open cvr db,no the db will create auto R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Open_DB(JDB_SQLITE3 *db_in) { int Qy_Ret = 0; #if 0 Qy_Ret = sqlite3_open(JDB_CVR_NAME,&GP_JosephDB); if(Qy_Ret) { printf("%s:[%d] Can't open database: %sn", __FUNCTION__,__LINE__,JDB_CVR_NAME); sqlite3_close(GP_JosephDB); Qy_Ret = -1; return Qy_Ret; } else if(Qy_Ret == 0) { printf("%s:[%d] Open database:%s Succeed !n",JDB_CVR_NAME); } #else sqlite3_initialize(); Qy_Ret = sqlite3_open_v2(JDB_CVR_NAME,&GP_JosephDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX,NULL); if(Qy_Ret) { printf("%s:[%d] Can't open database: %sn",JDB_CVR_NAME); sqlite3_close(GP_JosephDB); Qy_Ret = -1; return Qy_Ret; } else if(Qy_Ret == 0) { printf("%s:[%d] Open database:%s Succeed !n",JDB_CVR_NAME); } #endif db_in = GP_JosephDB; return Qy_Ret; } /* Author : kj Time : 2014-09-07 Function : close db R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Close_DB(JDB_SQLITE3 *db_in) { int Qy_Ret = 0; #if 0 Qy_Ret = sqlite3_close(GP_JosephDB); if(Qy_Ret) { printf("%s:[%d] Can't Close database: %sn",JDB_CVR_NAME); Qy_Ret = -1; return Qy_Ret; } #else Qy_Ret = sqlite3_close_v2(GP_JosephDB); if(Qy_Ret) { printf("%s:[%d] Can't Close database: %sn",JDB_CVR_NAME); Qy_Ret = -1; return Qy_Ret; } #endif return Qy_Ret; } /* Author : kj Time : 2014-09-07 Function : create table R_Value: 0 ~ succeed -1 ~ failed 1 ~ have exist */ int Joseph_Creat_Table(void) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; /* First: Create Cvr Config Table */ sprintf(Joseph_Sqlite_Cmd, JOSEPH_CREATE_CVRCONFFIG_CMD, JOSEPH_DB_CVRCONFIG_FIELD ); Qy_Ret = sqlite3_exec(GP_JosephDB,Joseph_Sqlite_Cmd,&szErrMsg); if(Qy_Ret != 0) { printf("%s:[%d] Qy_Ret is %d,The err info is %sn",Qy_Ret,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Second: Create Cvr Record Table */ memset(Joseph_Sqlite_Cmd,1024); sprintf(Joseph_Sqlite_Cmd, JOSEPH_CREATE_CVRRECORD_CMD, JOSEPH_DB_CVRRECORD_FIELD ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-07 Function : insert cvr config table R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Insert_CvrConfig_Table(char *pWeekDay,char *pSection) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if((NULL == pWeekDay) || (NULL == pSection)) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd, JOSEPH_INSERT_CVRCONFIG_DEFAULT_CMD, JDB_CVR_CONFIG_TABLE_NAME, JDB_CVR_CONFIG_PKEY_ONE, JDB_CVR_CONFIG_PKEY_TWO, pWeekDay, pSection ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : insert cvr record table,when the start the pthread of cvr R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Insert_CvrRecord_Table(JOSEPH_DB_CVR_RECORD *Joseph_Db_Cvr_Record) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == Joseph_Db_Cvr_Record) { return Qy_Ret; } printf("%s:[%d] n",__FUNCTION__,__LINE__); sprintf(Joseph_Sqlite_Cmd, JOSEPH_INSERT_CVRRECORD_CMD, JDB_CVR_RECORD_TABLE_NAME, Joseph_Db_Cvr_Record->cStSeconds, Joseph_Db_Cvr_Record->cEtSeconds, Joseph_Db_Cvr_Record->cDirFileName, Joseph_Db_Cvr_Record->cFileSpShift, Joseph_Db_Cvr_Record->cFileEpShift, Joseph_Db_Cvr_Record->cFileStatus, Joseph_Db_Cvr_Record->cFileIntact ); printf("%s:[%d] n",__LINE__); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } else { printf("%s:[%d] Qy_Ret is %d,sqlite3_errmsg(GP_JosephDB)); } printf("%s:[%d] n",__LINE__); if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-10 Function : init table from cvr db */ int Joseph_Init_DB_Table(void) { int Qy_Ret = -1; Qy_Ret = Joseph_Insert_CvrConfig_Table("1","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("1","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("1","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("1","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("2","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("2","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("2","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("2","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("3","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("3","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("3","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("3","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("4","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("4","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("4","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("4","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("5","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("5","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("5","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("5","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("6","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("6","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("6","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("6","4"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("7","1"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("7","2"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("7","3"); if(Qy_Ret == -1) { return Qy_Ret; } Qy_Ret = Joseph_Insert_CvrConfig_Table("7","4"); if(Qy_Ret == -1) { return Qy_Ret; } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : update cvr config table alter the config of cvr R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Update_CvrConfig_Table(JOSEPH_DB_CVR_CONFIG *Joseph_Db_Cvr_Config) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == Joseph_Db_Cvr_Config) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd, JOSEPH_UPDATE_CVRCONFIG_CMD, Joseph_Db_Cvr_Config->cStpSection, Joseph_Db_Cvr_Config->cEtpSection, Joseph_Db_Cvr_Config->cCvrType, Joseph_Db_Cvr_Config->cGDayPlan, Joseph_Db_Cvr_Config->cCvrStaus, Joseph_Db_Cvr_Config->cWeekDay, Joseph_Db_Cvr_Config->cSection ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : update cvr record table R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Update_CvrReord_File_Status(JOSEPH_DB_CVR_RECORD *Joseph_Db_Cvr_Record) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == Joseph_Db_Cvr_Record) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd, JOSEPH_UPDATE_CVRRECORD_CMD, Joseph_Db_Cvr_Record->cFileIntact, Joseph_Db_Cvr_Record->cStSeconds ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : select from db cvr config table R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Select_CvrConfig_All(JOSEPH_DB_CVR_CONFIG *Joseph_Db_Cvr_Config) { int Qy_Ret = -1; int nColumn = 0; int vtype,i; sqlite3_stmt *stmt = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == Joseph_Db_Cvr_Config) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd, JOSEPH_SELECT_CVRCONFIG_CMD, Joseph_Db_Cvr_Config->cSection ); Qy_Ret = sqlite3_prepare_v2(GP_JosephDB,strlen(Joseph_Sqlite_Cmd),&stmt,NULL); if(Qy_Ret != SQLITE_OK) { if(stmt) { sqlite3_finalize(stmt); } Qy_Ret = -1; return Qy_Ret; } /*get the total Column from db*/ nColumn = sqlite3_column_count(stmt); do{ Qy_Ret = sqlite3_step(stmt); if(Qy_Ret == SQLITE_ROW) { for(i = 0 ; i < nColumn ; i++ ) { vtype = sqlite3_column_type(stmt,i); if(vtype == SQLITE_INTEGER) { printf("%s(vchar) : %d n",sqlite3_column_name(stmt,i), sqlite3_column_int(stmt,i)); } else if(vtype == SQLITE_TEXT) { printf("%s(text) : %s n", sqlite3_column_text(stmt,i)); } else if(vtype == SQLITE_NULL) { printf("no valuesn"); } } printf("****************n"); } else if(Qy_Ret == SQLITE_DONE) { printf("Select finishn"); Qy_Ret = 0; break; } else { printf("Select failen"); sqlite3_finalize(stmt); Qy_Ret = -1; break; } }while(1); sqlite3_finalize(stmt); return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : select from db cvr recordtable R_Value: 0 ~ succeed -1 ~ failed Notice : Select all -> 1 ~ all 0 ~ one */ int Joseph_Select_CvrRecord_All(JOSEPH_DB_CVR_RECORD *Joseph_Db_Cvr_Record,int Select_All) { int Qy_Ret = -1; int nColumn = 0; int vtype,i; sqlite3_stmt *stmt = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == Joseph_Db_Cvr_Record) { return Qy_Ret; } printf("%s:[%d] n",__LINE__); switch(Select_All) { case 0: { sprintf(Joseph_Sqlite_Cmd, JOSEPH_SELECT_CVRRECORD_CMD, JDB_CVR_RECORD_PKEY, Joseph_Db_Cvr_Record->cStSeconds ); } break; case 1: { sprintf(Joseph_Sqlite_Cmd, JOSEPH_SELECT_CMD, JDB_CVR_RECORD_TABLE_NAME ); } break; default: { sprintf(Joseph_Sqlite_Cmd, Joseph_Db_Cvr_Record->cStSeconds ); } break; } printf("%s:[%d] n",__LINE__); Qy_Ret = sqlite3_prepare_v2(GP_JosephDB,NULL); if(Qy_Ret != SQLITE_OK) { if(stmt) { sqlite3_finalize(stmt); } Qy_Ret = -1; return Qy_Ret; } printf("%s:[%d] n",__LINE__); /*get the total Column from db*/ nColumn = sqlite3_column_count(stmt); do{ printf("%s:[%d] n",__LINE__); Qy_Ret = sqlite3_step(stmt); if(Qy_Ret == SQLITE_ROW) { for(i = 0 ; i < nColumn ; i++ ) { vtype = sqlite3_column_type(stmt,i)); } else if(vtype == SQLITE_NULL) { printf("no valuesn"); } } printf("****************n"); } else if(Qy_Ret == SQLITE_DONE) { printf("Select finishn"); Qy_Ret = 0; break; } else { printf("Select failen"); sqlite3_finalize(stmt); Qy_Ret = -1; break; } printf("%s:[%d] n",__LINE__); }while(1); printf("%s:[%d] n",__LINE__); sqlite3_finalize(stmt); printf("%s:[%d] n",__LINE__); return Qy_Ret; } /* Author : kj Time : 2014-09-07 Function : clear cvr cfg per section R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Cancel_CvrConfig_PerSection(char *pWeekDay,char *pSection) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; JOSEPH_DB_CVR_CONFIG Joseph_Db_Cvr_Config; if((NULL == pWeekDay) || (NULL == pSection)) { return Qy_Ret; } memset(&Joseph_Db_Cvr_Config,sizeof(JOSEPH_DB_CVR_CONFIG)); strcpy(Joseph_Db_Cvr_Config.cWeekDay,pWeekDay); strcpy(Joseph_Db_Cvr_Config.cSection,pSection); strcpy(Joseph_Db_Cvr_Config.cStpSection,JDB_CVR_DB_DEFAULT_VALUES); strcpy(Joseph_Db_Cvr_Config.cEtpSection,JDB_CVR_DB_DEFAULT_VALUES); strcpy(Joseph_Db_Cvr_Config.cCvrType,JDB_CVR_DB_DEFAULT_VALUES); strcpy(Joseph_Db_Cvr_Config.cGDayPlan,JDB_CVR_DB_DEFAULT_VALUES); strcpy(Joseph_Db_Cvr_Config.cCvrStaus,JDB_CVR_DB_DEFAULT_VALUES); sprintf(Joseph_Sqlite_Cmd, Joseph_Db_Cvr_Config.cStpSection, Joseph_Db_Cvr_Config.cEtpSection, Joseph_Db_Cvr_Config.cCvrType, Joseph_Db_Cvr_Config.cGDayPlan, Joseph_Db_Cvr_Config.cCvrStaus, Joseph_Db_Cvr_Config.cWeekDay, Joseph_Db_Cvr_Config.cSection ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : delete from cvr record table R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Delete_CvrRecord(char *cStSeconds) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == cStSeconds) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd, JOSEPH_DELETE_CMD, cStSeconds ); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-11 Function : delete from cvr record table R_Value: 0 ~ succeed -1 ~ failed */ int Joseph_Clear_CvrRecord(void) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; sprintf(Joseph_Sqlite_Cmd,"delete from %s",JDB_CVR_RECORD_TABLE_NAME); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == 1) { Qy_Ret = 1; } else { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 201-09-12 Function : get the row info of table */ int Joseph_GetRow_From_Table(char *DB_Tbale_Name) { int Qy_Ret = -1; int nrow = 0; int ncol = 4; char **dbResult = NULL; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == DB_Tbale_Name) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd,"select * from %s;",DB_Tbale_Name); Qy_Ret = sqlite3_get_table(GP_JosephDB,&dbResult,&nrow,&ncol,&szErrMsg); if(Qy_Ret == SQLITE_OK ) { printf("%s:[%d] Find (row->%d*col->%d) record from table:%s !n", nrow,ncol,DB_Tbale_Name); Qy_Ret = nrow; if(nrow == JDB_CVR_CONFIG_INTACT_THRESHOLD) { ;//do nothing } else { ;//delete all ;//init table again Qy_Ret = -1; } } else { printf("%s:[%d] The err info is: %s !n", szErrMsg); } sqlite3_free_table(dbResult); if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } /* Author : kj Time : 2014-09-12 Function : check the table is exist or not,then init table Instrcution: 0 ~ exist -1 ~ inexist */ int Joseph_Check_Table_Status(char *DB_Tbale_Name) { int Qy_Ret = -1; char *szErrMsg = NULL; char Joseph_Sqlite_Cmd[1024] = {0}; if(NULL == DB_Tbale_Name) { return Qy_Ret; } sprintf(Joseph_Sqlite_Cmd,"select * from %s",DB_Tbale_Name); Qy_Ret = sqlite3_exec(GP_JosephDB,sqlite3_errmsg(GP_JosephDB)); if(Qy_Ret == SQLITE_ERROR) { Qy_Ret = -1; } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } else { Joseph_GetRow_From_Table(DB_Tbale_Name); } if(NULL != szErrMsg) { sqlite3_free(szErrMsg); } return Qy_Ret; } int Joseph_DB_WriteTest_Status = 0; int Joseph_DB_WriteTest(void) { int Qy_Ret = 0; int cStSeconds = 1000; int Iteration = 0; JOSEPH_DB_CVR_RECORD Joseph_Db_Cvr_Record; while(1) { printf("%s:[%d] n",__LINE__); memset(&Joseph_Db_Cvr_Record,sizeof(JOSEPH_DB_CVR_RECORD)); sprintf(Joseph_Db_Cvr_Record.cStSeconds,"%d",cStSeconds); sprintf(Joseph_Db_Cvr_Record.cEtSeconds,(cStSeconds + 100)); sprintf(Joseph_Db_Cvr_Record.cDirFileName,0); sprintf(Joseph_Db_Cvr_Record.cFileSpShift,0); sprintf(Joseph_Db_Cvr_Record.cFileEpShift,0); sprintf(Joseph_Db_Cvr_Record.cFileStatus,0); sprintf(Joseph_Db_Cvr_Record.cFileIntact,0); printf("%s:[%d] n",__LINE__); Joseph_Insert_CvrRecord_Table(&Joseph_Db_Cvr_Record); usleep(500000); cStSeconds += 10; Iteration++; printf("%s:[%d] n",__LINE__); if(Iteration > 6) { cStSeconds = 1000; Iteration = 0; printf("%s:[%d] n",__LINE__); Joseph_Clear_CvrRecord(); printf("%s:[%d] n",__LINE__); } printf("%s:[%d] n",__LINE__); } return Qy_Ret; } int Joseph_DB_WriteTest_Pthread(void) { int Qy_Ret = 0; char *param = NULL; run_pthread(&Joseph_DB_WriteTest,param); return Qy_Ret; } int Joseph_DB_ReadTest_Status = 0; int Joseph_DB_ReadTest(void) { int Qy_Ret = 0; JOSEPH_DB_CVR_RECORD Joseph_Db_Cvr_Record; while(1) { printf("%s:[%d] n",__LINE__); memset(&Joseph_Db_Cvr_Record,sizeof(JOSEPH_DB_CVR_RECORD)); Joseph_Select_CvrRecord_All(&Joseph_Db_Cvr_Record,1); usleep(500000); printf("%s:[%d] n",__LINE__); } return Qy_Ret; } int Joseph_DB_ReadTest_Pthread(void) { int Qy_Ret = 0; char *param = NULL; run_pthread(&Joseph_DB_ReadTest,param); return Qy_Ret; } int main(int argc,char **argv) { int Qy_Ret = 0; sqlite3 *pdb = NULL; JOSEPH_DB_CVR_CONFIG Joseph_Db_Cvr_Config; memset(&Joseph_Db_Cvr_Config,sizeof(JOSEPH_DB_CVR_CONFIG)); Qy_Ret = Joseph_Open_DB(pdb); if(Qy_Ret != 0) { Qy_Ret = -1; return Qy_Ret; } Qy_Ret = Joseph_Creat_Table(); if(Qy_Ret < 0) { Qy_Ret = -1; goto Close; } Qy_Ret = Joseph_GetRow_From_Table(JDB_CVR_CONFIG_TABLE_NAME); if(Qy_Ret < 0) { Qy_Ret = Joseph_Init_DB_Table(); if(Qy_Ret < 0) goto Close; } printf("=================================================n"); printf("nAfter Init ... :nn"); sprintf(Joseph_Db_Cvr_Config.cWeekDay,"%s","1"); sprintf(Joseph_Db_Cvr_Config.cSection,"1"); Joseph_Select_CvrConfig_All(&Joseph_Db_Cvr_Config); Joseph_DB_WriteTest_Pthread(); Joseph_DB_ReadTest_Pthread(); while(1) { sleep(1); if((Joseph_DB_ReadTest_Status == 1) && (Joseph_DB_WriteTest_Status == 1)) { break; } } Close: Joseph_Close_DB(pdb); return Qy_Ret; } 编译方法(Makefile): #Author : kj #Time : 2014-09-07 #Function: compile sqlite3 BIN=joseph_cvr L_CFLAGS_X86= -L$(JOSEPH_SQLITE_X86_LIB) -lsqlite3 -ldl -lpthread I_CFLAGS_X86= -I$(JOSEPH_SQLITE_X86_INLCUDE) L_CFLAGS_ARM= -L$(JOSEPH_SQLITE_ARM_LIB) -lsqlite3 -ldl -lpthread I_CFLAGS_ARM= -I$(JOSEPH_SQLITE_ARM_INLCUDE) all:x86 x86: gcc -g -o $(BIN) *.c $(I_CFLAGS_X86) $(L_CFLAGS_X86) arm: arm-hisiv100nptl-linux-gcc -o $(BIN) *.c $(I_CFLAGS_ARM) $(L_CFLAGS_ARM) -lm clean: rm -rf $(BIN) .PHONY:all x86 arm clean (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |