1

我在我的 C/C++ 项目中的 ubunt 12.04 中使用 eclipse,尝试执行查询时出现以下错误。我已经发布了堆栈以及要执行的查询和语法:

这个总是成功的

查询 1:CREATE TABLE IF NOT EXISTS HS_TABLE (user TEXT NOT NULL, passwd TEXT NOT NULL, PRIMARY KEY(user) );

此查询第一次成功但第二次失败

查询 2: INSERT OR REPLACE INTO HS_TABLE (user, passwd) SELECT 'user 0','passwd 0' UNION SELECT 'user 1','passwd 1' UNION SELECT 'user 2','passwd 2';

线程 [1] 5556 [核心:3](暂停:信号:SIGSEGV:分段错误)

sqlite3Init() at sqlite3.c:93,817 0x466540  
sqlite3ReadSchema() at sqlite3.c:93,855 0x466667    
sqlite3LocateTable() at sqlite3.c:81,026 0x4506f9   
sqlite3LocateTableItem() at sqlite3.c:81,065 0x45081f   
sqlite3SrcListLookup() at sqlite3.c:85,104 0x457c79 
sqlite3Insert() at sqlite3.c:89,190 0x45e612    
yy_reduce() at sqlite3.c:1,10,576 0x4822d2  
sqlite3Parser() at sqlite3.c:1,11,347 0x484a5d  
sqlite3RunParser() at sqlite3.c:1,12,172 0x4856fc   
sqlite3Prepare() at sqlite3.c:94,032 0x466a48
sqlite3LockAndPrepare() at sqlite3.c:94,124 0x466d77
sqlite3_prepare_v2 at sqlite3.c:94,200 0x466f2e 

另外我 sqlite3_close(sqlite3* db) 函数失败并且程序每次都终止

线程 [1] 5845 [核心:0](暂停:信号:SIGSEGV:分段错误)

sqlite3MemSize() at sqlite3.c:15,594 0x406d41   
sqlite3MallocSize() at sqlite3.c:19,021 0x407a73    
sqlite3_free() at sqlite3.c:19,044 0x407afd 
sqlite3DbFree() at sqlite3.c:19,080 0x407c16    
sqliteDeleteColumnNames() at sqlite3.c:81,237 0x450da7  
sqlite3DeleteTable() at sqlite3.c:81,296 0x450f0e   
sqlite3SchemaClear() at sqlite3.c:85,035 0x457b23   
sqlite3LeaveMutexAndCloseZombie() at sqlite3.c:1,13,533 0x487682    
sqlite3Close() at sqlite3.c:1,13,473 0x487558   
sqlite3_close() at sqlite3.c:1,13,486 0x48757c

代码:

sql_ret = sqlite3_prepare_v2(m_database, CurrQuery, -1, &stmt, NULL);

我的查询中是否有任何错误,如果是,为什么会崩溃,这是我尝试在 EClipse 中调试时发生的。

这是代码:

ErrorCode DBResponder::openDB(const char* DBPath)
{
UINT64 sql_ret;

sql_ret = sqlite3_open(DBPath,&m_database);
cout<<__func__<<": sql_ret = "<<sql_ret<<endl;
if(  sql_ret != SQLITE_OK )
    return DB_ERROR;

return NO_ERROR;
}

ErrorCode DBResponder::closeDB()
{ 
UINT64 sql_ret;

sql_ret = sqlite3_close(m_database);
cout<<__func__<<": sql_ret = "<<sql_ret<<endl;
if( sql_ret != SQLITE_OK )
    return DB_ERROR;

return NO_ERROR;
}

ErrorCode DBResponder::executeQuery()
{
sqlite3_stmt *stmt;
UINT64 sql_ret;
const char *CurrQuery = "INSERT OR REPLACE INTO HS_TABLE (user, passwd) SELECT 'user 0','passwd 0' UNION SELECT 'user 1','passwd 1' UNION SELECT 'user 2','passwd 2';";

ErrorCode err = NO_ERROR;

cout<<endl<<__func__<<": "<<CurrQuery<<endl;
sql_ret = sqlite3_prepare_v2(m_database, CurrQuery, -1, &stmt, NULL);
cout<<__func__<<": sql_ret (sqlite3_prepare_v2) = "<<sql_ret<<endl;
if(sql_ret != SQLITE_OK)
    return DB_ERROR;

HInfo* h;

while(1)
{
    sql_ret = sqlite3_step(stmt);
    cout<<__func__<<": sql_ret (sqlite3_step) = "<<sql_ret<<endl;
    if(sql_ret == SQLITE_ROW)
    {

        hs = (HInfo*)malloc(1*sizeof(HInfo));

        strncpy(hs->user, (const char*)sqlite3_column_text(stmt,0), MAX_STR_LEN );
        strncpy(hs->passwd, (const char*)sqlite3_column_text(stmt,1), MAX_STR_LEN);

        llist.add( (void*) hs);

    }
    else if(sql_ret == SQLITE_DONE)
    {
        //query finished
        //return hs;
        writeResponse(hs);
        break;
    }
    else
    {
        //error
        err = DB_ERROR;
        writeResponse(NULL);
        break;
    }
}//end of while loop

sql_ret = sqlite3_finalize(stmt);
cout<<__func__<<": sql_ret (sqlite3_finalize) = "<<sql_ret<<endl;
if(sql_ret != SQLITE_OK)
    err = DB_ERROR;

return err;
}

我也收到错误: 无效的参数'候选人是:int sqlite3_open(const char *,* *)'

对于每个 sqlite3 函数都是这样。注意 * ,而不是 sqlite3 *,但是该项目正在构建中。我在链接器中包含了 dl 和 pthread。请参阅此错误:http://stackoverflow.com/questions/13859956/how-to-use-sqlite3-c-and-sqlite3-h-in-eclipse-cc-project

请帮忙!谢谢

4

0 回答 0