问题描述
我正在使用C在vs2012中实现SQLite Prepare-Statement.
I am implementing SQLite Prepare-Statement in vs2012 using C.
我正在使用此 link 作为教程,以捕获每次我运行代码时机器中正在运行的进程.一切工作正常,除了prepare语句不允许我将其置于do-while循环之外.如果我将它放在do-while循环中,那么我实际上什么也没做,因为对于每个db插入,都会再次执行prepare语句,这是不实际的.但是,当我将prepare语句放入do-while循环中并运行代码时,它可以正常工作(但是它运行的插入次数与我必须插入的次数相同).当我退出do-while循环时,它会给我发出内存警告并中断.
I am using this link as a tutorial to capture the processes that are running in my machine every time I run my code. Everything is working fine except that the prepare-statement does not allow me to put it outside the do-while loop. if I put it inside the do-while loop then I am doing nothing actually because with every db-insertion the prepare-statement is again executed which is not practical. however, when I put the prepare-statement inside the do-while loop and run the code, it works fine(but it runs as many insertion I have to insert). and when I put outside the do-while loop it gives me a memory warning and break.
这是我实现SQLite的方式:
here is how I am implementing the SQLite:
BOOL GetProcessList(sqlite3 *db)
{
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
DWORD dwPriorityClass;
sqlite3_stmt* stmt;
char *errorMessage;
char query[80] = "INSERT INTO Process_list VALUES (?1, ?2, ?3, ?4, ?5);";
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
return( FALSE );
}
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
printError( TEXT("Process32First") ); // show cause of failure
CloseHandle( hProcessSnap ); // clean the snapshot object
return( FALSE );
}
// Now walk the snapshot of processes, and
// display information about each process in turn
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Process_list("
"Process_ID INT PRIMARY KEY , "
"Thread_count INT , "
"Parent_PID INT , "
"Priority_Base INT , "
"Priority_class INT );", NULL, NULL, &errorMessage);
sqlite3_prepare_v2(db, query,strlen(query), &stmt,NULL); // can't put it here
do
{
_tprintf( TEXT("\n\n=====================================================" ));
_tprintf( TEXT("\nPROCESS NAME: %s"), pe32.szExeFile );
_tprintf( TEXT("\n-------------------------------------------------------" ));
// Retrieve the priority class.
dwPriorityClass = 0;
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
{
printError( TEXT("OpenProcess") );
} else {
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
{
printError( TEXT("GetPriorityClass") );
}
CloseHandle( hProcess );
}
_tprintf( TEXT("\n Process ID = 0x%08X"), pe32.th32ProcessID );
_tprintf( TEXT("\n Thread count = %d"), pe32.cntThreads );
_tprintf( TEXT("\n Parent process ID = 0x%08X"), pe32.th32ParentProcessID );
_tprintf( TEXT("\n Priority base = %d"), pe32.pcPriClassBase );
if( dwPriorityClass )
_tprintf( TEXT("\n Priority class = %d"), dwPriorityClass );
_tprintf( TEXT("\n"));
//-------------------------------------------------------------------------------
// database insertion of the process information
sqlite3_bind_int64(stmt,1,pe32.th32ProcessID);
sqlite3_bind_int64(stmt,2,pe32.cntThreads);
sqlite3_bind_int64(stmt,3,pe32.th32ParentProcessID);
sqlite3_bind_int64(stmt,4,pe32.pcPriClassBase);
sqlite3_bind_int64(stmt,5,dwPriorityClass);
if(sqlite3_step(stmt) != SQLITE_DONE)
printf("\nPrepare statement failed!\n");
sqlite3_reset(stmt); // the error appears here
sqlite3_finalize(stmt);
//-------------------------------------------------------------------------------
// List the modules and threads associated with this process
ListProcessModules( pe32.th32ProcessID,db );
ListProcessThreads( pe32.th32ProcessID,db );
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return( TRUE );
}
因此,当我运行代码时,它向我显示了这一点:
so when I run the code, it shows me this:
那么为什么会发生这种情况以及如何避免这种内存异常?谢谢
so why is this happening and how to avoid this memory exception ? thanks
推荐答案
- 您必须检查所有函数的返回值,尤其是
sqlite3_prepare_v2
.(如果出现错误,请调用 sqlite3_errmsg 以获得有用的错误消息.) - 您可以多次使用一条准备好的语句,并且必须重新使用
sqlite3_reset
才能重新使用它,这是正确的操作.但是,sqlite3_finalize
完全释放了该语句.您必须在循环后 调用它.
- You must check the return value of all functions, especially
sqlite3_prepare_v2
.(And if you get an error, call sqlite3_errmsg to get a usefull error message.) - You can use a prepared statement multiple times, and you must call
sqlite3_reset
before reusing it, which you are doing correctly.However,sqlite3_finalize
frees the statement entirely; you must call this after the loop.
这篇关于vs2012:SQLite:访问冲突读取位置0x0000000C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!