问题描述
- c语言数读取据库用sqlite3_prepare
-
int g_XLZTA_Array[255][9] ;/************************************************
- 功能:从数据库里读信令状态
- 输入:无
- 输出:无
- 返回:无
************************************************/
int getPD_Key_StateByState(int State)
{
char sql[128] = { 0 };
const char *errmsg = NULL;
sqlite3_stmt *pstmt;
int ret = -1;
int i = 0;sprintf(sql,
"select State,Key_ID,Long_Red,Long_Green,Long_Yellow,Red,Green,Yellow ,Con_State form XLZT_A where State= %d",
State);
ret = sqlite3_prepare(pd_db, sql, strlen(sql), &pstmt, &errmsg);
if (ret != SQLITE_OK)
{
printf("data base error : %sn", errmsg);
sqlite3_free((void *) errmsg);
return 0;
}for (i = 0; i < 17; i++)
{
ret = sqlite3_step(pstmt);if (ret != SQLITE_ROW) { return 0; } g_XLZTA_Array[i][0] = sqlite3_column_int(pstmt, 0); g_XLZTA_Array[i][1] = sqlite3_column_int(pstmt, 1); g_XLZTA_Array[i][2] = sqlite3_column_int(pstmt, 2); g_XLZTA_Array[i][3] = sqlite3_column_int(pstmt, 3); g_XLZTA_Array[i][4] = sqlite3_column_int(pstmt, 4); g_XLZTA_Array[i][5] = sqlite3_column_int(pstmt, 5); g_XLZTA_Array[i][6] = sqlite3_column_int(pstmt, 6); g_XLZTA_Array[i][7] = sqlite3_column_int(pstmt, 7); g_XLZTA_Array[i][8] = sqlite3_column_int(pstmt, 8); LOG(LV_DEBUG, "State = %d,Key_ID = %d, Long_Red = %d,Log_Green = %d,Long_Yellow = %d,Red = %d,Green = %d,Yellow = %d,Con_State = %d", g_XLZTA_Array[i][0], g_XLZTA_Array[i][1], g_XLZTA_Array[i][2], g_XLZTA_Array[i][3], g_XLZTA_Array[i][4], g_XLZTA_Array[i][5], g_XLZTA_Array[i][6], g_XLZTA_Array[i][7], g_XLZTA_Array[i][8]);
}
return 1;
}
void init_data_base(void)
{
int db_ret = -1;db_ret = sqlite3_open(PD_DATA_BASE_FILE_PATH, &pd_db); if (db_ret != SQLITE_OK) { LOG(LV_FATAL,"init_data_base sqlite3_open pd_db error"); } db_ret = sqlite3_open(PDT_DATA_BASE_FILE_PATH, &pdt_db); if (db_ret != SQLITE_OK) { LOG(LV_FATAL,"init_data_base sqlite3_open pdt_db error"); } getCurrentPdSignalTableName(XLB_Name); //getCurrentPdSignalTableName(XLZT_Name); //信令状态
}
错误:
data base error : select State,Key_ID,Long_Red,Long_Green,Long_Yellow,Red,Green,Yellow ,Con_State form XLZT_A where State= 2
*** glibc detected *** ./PD_task: munmap_chunk(): invalid pointer: 0xb6d5c8f0 ***
解决方案
是不是有的字段名写的不对,或是大小写问题
解决方案二:
首先你有没有打开数据库的连接,其次你直接运行select State,Key_ID,Long_Red,Long_Green,Long_Yellow,Red,Green,Yellow ,Con_State form XLZT_A where State= 2 是否会报错?