问题
I am implementing SQLite Prepare-Statement in vs2012 using C.
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.
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
回答1:
- 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.
来源:https://stackoverflow.com/questions/18656053/vs2012-sqlite-access-violation-reading-location-0x0000000c