#include #include #include "sqlite3.h" /** * gcc -o test_error test_error.c sqlite3.c * ./test_error */ int callback( void *unused, int argc, char **argv, char **col_name ) { return 0; } void test_error( sqlite3_context* context, int argc, sqlite3_value **argv) { char* msg = (char*)sqlite3_value_text( argv[0] ); int code = sqlite3_value_int( argv[1] ); fprintf( stderr, "Writting error msg '%s' and error_code %d to context\n", msg, code ); sqlite3_result_error( context, msg, -1 ); sqlite3_result_error_code( context, code); return; } int main( int argc, char **argv ) { sqlite3 *db; sqlite3_stmt *stmt; const char *tail; char *errmsg; int rc; printf("SQLite Version: %s\n", sqlite3_libversion() ); rc = sqlite3_open_v2( ":memory:", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL ); if ( SQLITE_OK != rc ) { fprintf(stderr, "Failure opening database: %s\n", sqlite3_errmsg( db ) ); sqlite3_close( db ); exit( 1 ); } rc = sqlite3_create_function( db, "test_error", 2, SQLITE_UTF8, NULL, test_error, NULL, NULL ); if ( SQLITE_OK != rc ) { fprintf( stderr, "Failure creating function: %s\n", sqlite3_errmsg( db ) ); sqlite3_close( db ); exit( 1 ); } rc = sqlite3_prepare_v2( db, "SELECT test_error('This is an error', 42)", -1, &stmt, &tail ); if ( SQLITE_OK != rc ) { fprintf( stderr, "Failure preparing statement: %s\n", sqlite3_errmsg( db ) ); sqlite3_close( db ); exit( 1 ); } rc = sqlite3_create_function( db, "test_error", 2, SQLITE_UTF8, NULL, NULL, NULL, NULL ); printf( "remove rc = %d\n", rc ); rc = sqlite3_create_function( db, "test_blah", -1, SQLITE_UTF8, NULL, NULL, NULL, NULL ); printf( "remove rc 2 = %d\n", rc ); rc = sqlite3_step( stmt ); if ( SQLITE_ROW != rc ) { printf( "Return code from sqlite3_step : %d\n", rc ); printf( "Before finalizing error code is : %d\n", sqlite3_errcode( db )); printf( "Before finalizing error message is : %s\n", sqlite3_errmsg( db )); } rc = sqlite3_reset( stmt ); printf( "Return value from sqlite3_finalize : %d\n", rc ); printf( "After finalizing errcode is : %d\n", sqlite3_errcode( db )); printf( "After finalizing error message is : %s\n", sqlite3_errmsg( db )); sqlite3_close( db ); return 0; }