SQLite User Forum

sqlite3_open crashing
Login

sqlite3_open crashing

(1) By anonymous on 2022-11-15 03:59:32 [link] [source]

hi,

I am compiling a basic simple 32bit C++ process to open and close a sqlite db but it crashes every time its run via a browser. Can anyone tell me why its crashing when sqlite3_open(myfile,&db) is activated?

thanks in advance. Lloyd

my code used


// A small example program using SQLite with C++

#include <stdio.h>

#include <iostream.h>

#include <sqlite3.h>

using namespace std;

#ifdef __cplusplus

#pragma inline_depth( 0 );

#endif

#ifndef SQLITE_EXTERN //-# define SQLITE_EXTERN extern

define SQLITE_EXTERN __declspec(dllexport)

#endif

static int callback(void *NotUsed, int argc, char **argv, char **azColName) { if(NotUsed){}//avoid error

int i; for(i=0; i<argc; i++) { cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"
n"; } cout<<"n"; return 0; }

int main(int argc, char *argv[]) {

if(argc && argv[0]){}//avoid compiler error

printf("Content-type: text/htmlnn"); printf("<!DOCTYPE html>n");
printf("<html>n");
printf("<head>n");
printf("TestSQLiten");
printf("<style>n");
printf(".tc{ text-align:center; }n");
printf(".notice{ text-size:9pt; color:green; }n");
printf("</style>n");
printf("</head>n");
printf("<body>n"); printf("

C++ TestSQLite Sample

n");

char myfile[200]="c:\inetpub\scripts\ezi\data\sqlite\familyguy.db";

// FILE *fpcy; // if ((fpcy=fopen(myfile, "rb"))==NULL){ printf("familyguy.db error"); goto bot; } // fclose(fpcy);

printf("File : %s

n",myfile);

printf("Step 1. Success : familyguy.db file opened using c++ fopen() and fclose().
n");

int tryit;

tryit=true;

printf("Step 2. sqlite 'tryit' set to %d
n", tryit);

if(tryit) { const int STATEMENTS = 8; sqlite3 *db; char *zErrMsg = 0; const char *pSQL[STATEMENTS]; int rc = 0;

printf("...skipping process sqlite3_open() due to crashing!! ''
n"); goto bot;

rc = sqlite3_open(myfile, &db);//crashed here

if( rc ) { cout<<"Can't open database: "<<sqlite3_errmsg(db)<<"
n"; } else { cout<<"Open database successfully
nn"; } goto bot;

pSQL[0] = "create table myTable (FirstName varchar(30), LastName varchar(30), Age smallint, Hometown varchar(30), Job varchar(30))";

pSQL[1] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Peter', 'Griffin', 41, 'Quahog', 'Brewery')";

pSQL[2] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Lois', 'Griffin', 40, 'Newport', 'Piano Teacher')";

pSQL[3] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Joseph', 'Swanson', 39, 'Quahog', 'Police Officer')";

pSQL[4] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Glenn', 'Quagmire', 41, 'Quahog', 'Pilot')";

pSQL[5] = "select * from myTable";

pSQL[6] = "delete from myTable";

pSQL[7] = "drop table myTable";

for(int i = 0; i < STATEMENTS; i++) { rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg); if( rc!=SQLITE_OK ) { cout<<"SQL error: "<<sqlite3_errmsg(db)<<"
n"; sqlite3_free(zErrMsg); break; } } sqlite3_close(db); } bot: printf("</body>n");
printf("</html>n");
fcloseall(); return 0; }

(2) By oneeyeman on 2022-11-15 04:35:52 in reply to 1 [link] [source]

What do you mean by saying "crashing"? Are you getting some kind of error page? Which one? What browser do you use? Does your user have access to the directory where the db is located? Maybe they are read-only and not read-write?

Thank you.

(7) By anonymous on 2022-11-18 09:23:13 in reply to 2 [link] [source]

its not returning the headers and returning a 500 error page.

Im using 4 different browsers and all return the same result.

if I skip the open it returns the html page

printf("...skipping process sqlite3_open() due to crashing!! ''
n"); goto bot;

rc = sqlite3_open(myfile, &db);//crashed here

(15) By anonymous on 2022-11-21 06:38:59 in reply to 2 [link] [source]

thanks for the assist. I am using firefox running the exe on an abyss web server and returns a 500 error page. I checked out the code in debug mode and when it tries to execute the sqlite3_open the debug stops with an alert "an access violation" however I can not see what the actual violation is.

(16) By Kees Nuyt (knu) on 2022-11-22 12:19:24 in reply to 15 [link] [source]

I agree with Simon, that the user as which Abyss, and thus your program, runs probably doesn't have sufficient access to the directory where the database file is stored.

The process should not only be able to read and write the database file, it should also be able to create and delete files in that directory.

By the way, the same requirement goes for the temp directory.

-- 
Regards,
Kees Nuyt

(3.2) By Larry Brasfield (larrybr) on 2022-11-15 08:07:18 edited from 3.1 in reply to 1 [source]

I am compiling a basic simple 32bit C++ process to open and close a sqlite db but it crashes every time its run via a browser. Can anyone tell me why its crashing when sqlite3_open(myfile,&db) is activated?

As you will see below, the crash is not at the sqlite3_open() call. It occurs when you dereference the NULL pointer that results from failure of that call.

With slight modifications, I compiled and ran your program without a crash, or at least without anything I would label as a "crash".

My interposed comments say how and why I modified it, or would further modify it.

Please note that I have used markup to make your program readable as can be seen if you click the "[source]" link.

// A small example program using SQLite with C++
#include <iostream>
#include "sqlite3.h"

I was too lazy to figure out what retrograde C++ compiler would have a header known as "iostream.h", and I put sqlite3.h in quotes instead of angle-brackets because it is not a system header. I've also commented blank lines because using markup for code in block quotes is easier that way.

using namespace std;
//
#ifdef __cplusplus
#pragma inline_depth( 0 );
#endif
//
#ifndef SQLITE_EXTERN
//-# define SQLITE_EXTERN extern
# define SQLITE_EXTERN __declspec(dllexport)
#endif
//
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
   if(NotUsed){}//avoid error
   int i;
   for(i=0; i<argc; i++)
   {
    cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"<br>\n";
   }
   cout<<"\n";
   return 0;
}
//
int main(int, char *[])
{

It's easier to omit argument names than to fake-use them. Omitted HTML and tryit blatting, fopen()/fclose(), goto bot for relevance.

 char myfile[200]="c:\\inetpub\\scripts\\ezi\\data\\sqlite\\familyguy.db";
 int tryit=true;
 sqlite3 *db;
 //
   rc = sqlite3_open(myfile, &db);//crashed here
 //
   if( rc )
   {
      // Don't have a sqlite3 pointer here, so cannot dereference it.
      // cout<<"Can't open database: "<<sqlite3_errmsg(db)<<"<br>\n";
      cout<<"Can't open as database "<<myfile<<"<br>\n";
   } 
   else
   {
      cout<<"Open database successfully<br>\n\n";
   }

The code which I ran without a crash is like yours after this. The key change is to not dereference the sqlite3 pointer named 'db' after a failed sqlite3_open() call. It is set to 0 in that case; dereferencing it is good for a crash.

(4) By anonymous on 2022-11-15 11:05:25 in reply to 3.2 [link] [source]

(different anonymous to OP)

The key change is to not dereference the sqlite3 pointer named 'db' after a failed sqlite3_open() call. It is set to 0 in that case; dereferencing it is good for a crash.

but from the docs:

A database connection handle is usually returned in *ppDb, even if an error occurs. . . . The sqlite3_errmsg() or sqlite3_errmsg16() routines can be used . . . following a failure of any of the sqlite3_open() routines.

Looking at the code behind sqlite3_errmsg, it explicitly checks for a null pointer and handles that case.

And, in my experience, the pointer can be used to obtain an error message. I suspect the OP's problem actually lies elsewhere.

(5) By Larry Brasfield (larrybr) on 2022-11-15 15:33:33 in reply to 4 [link] [source]

I agree that the sqlite3 pointer may not be 0 on a failed sqlite3_open... call, and that my comment oversimplifies the results.

It is interesting that the sqlite3_errmsg API docs say nothing of accepting a NULL db pointer, but that behavior is implicitly promised by the doc language you quote. It's like spooky action at a distance.

Despite this issue, I am not seeing the OP's crash. It appears that the OP may be building a DLL and somehow linking that to the "crashing" program. This leaves the possibility that the calling convention is mismatched between the caller and callee. Having the very first call into the SQLite library fail egregiously would be consistent with that.

(6) By Keith Medcalf (kmedcalf) on 2022-11-15 15:54:46 in reply to 5 [link] [source]

Works just fine once the obvious errors are fixed:

// A small example program using SQLite with C++

#include <stdio.h>
#include <iostream>

#include <sqlite3.h>

using namespace std;

#ifdef __cplusplus
#pragma inline_depth( 0 );
#endif


#ifndef SQLITE_EXTERN
//-# define SQLITE_EXTERN extern
# define SQLITE_EXTERN __declspec(dllexport)
#endif

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
   if(NotUsed){}//avoid error

   int i;
   for(i=0; i<argc; i++)
   {
    cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"<br>\n";
   }
   cout<<"\n";
   return 0;
}


int main(int argc, char *argv[])
{

 if(argc && argv[0]){}//avoid compiler error

 printf("Content-type: text/html\n\n");
 printf("<!DOCTYPE html>\n");
 printf("<html>\n");
 printf("<head>\n");
 printf("<title>TestSQLite</title>\n");
 printf("<style>\n");
 printf(".tc{ text-align:center; }\n");
 printf(".notice{ text-size:9pt; color:green; }\n");
 printf("</style>\n");
 printf("</head>\n");
 printf("<body>\n");
 printf("<H2 class=tc>C++ TestSQLite Sample</H2>\n");

 char myfile[200]="c:\\inetpub\\scripts\\ezi\\data\\sqlite\\familyguy.db";

// FILE *fpcy;
// if ((fpcy=fopen(myfile, "rb"))==NULL){ printf("familyguy.db error"); goto bot; }
// fclose(fpcy);

printf("File : %s<br><br>\n",myfile);

 printf("Step 1. Success : familyguy.db file opened using c++ fopen() and fclose().<br>\n");

 int tryit;

 tryit=true;

 printf("Step 2. sqlite 'tryit' set to %d<br>\n", tryit);

 if(tryit)
 {
   const int STATEMENTS = 8;
   sqlite3 *db;
   char *zErrMsg = 0;
   const char *pSQL[STATEMENTS];
   int rc = 0;

// printf("...skipping process sqlite3_open() due to crashing!! ''<br>\n"); goto bot;

   rc = sqlite3_open(myfile, &db);//crashed here

   if( rc )
   {
      cout<<"Can't open database: "<<sqlite3_errmsg(db)<<"<br>\n";
   }
   else
   {
      cout<<"Open database successfully<br>\n\n";
   }
goto bot;

   pSQL[0] = "create table myTable (FirstName varchar(30), LastName varchar(30), Age smallint, Hometown varchar(30), Job varchar(30))";

   pSQL[1] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Peter', 'Griffin', 41, 'Quahog', 'Brewery')";

   pSQL[2] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Lois', 'Griffin', 40, 'Newport', 'Piano Teacher')";

   pSQL[3] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Joseph', 'Swanson', 39, 'Quahog', 'Police Officer')";

   pSQL[4] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Glenn', 'Quagmire', 41, 'Quahog', 'Pilot')";

   pSQL[5] = "select * from myTable";

   pSQL[6] = "delete from myTable";

   pSQL[7] = "drop table myTable";

   for(int i = 0; i < STATEMENTS; i++)
   {
    rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg);
    if( rc!=SQLITE_OK )
    {
     cout<<"SQL error: "<<sqlite3_errmsg(db)<<"<br>\n";
     sqlite3_free(zErrMsg);
     break;
    }
   }
   sqlite3_close(db);
 }
 bot:
 printf("</body>\n");
 printf("</html>\n");
// fcloseall();
 return 0;
 }
>g++ test.cc -I/source/bld -lsqlite3.dll -L/source/bld/gcc/64
>a.exe
Content-type: text/html

<!DOCTYPE html>
<html>
<head>
<title>TestSQLite</title>
<style>
.tc{ text-align:center; }
.notice{ text-size:9pt; color:green; }
</style>
</head>
<body>
<H2 class=tc>C++ TestSQLite Sample</H2>
File : c:\inetpub\scripts\ezi\data\sqlite\familyguy.db<br><br>
Step 1. Success : familyguy.db file opened using c++ fopen() and fclose().<br>
Step 2. sqlite 'tryit' set to 1<br>
Can't open database: unable to open database file<br>
</body>
</html>

(8) By anonymous on 2022-11-18 10:11:16 in reply to 6 [link] [source]

thanks for your answer however ....

you say works fine ... but what is it returning to the browser.

also you mentioned once the obvious errors are fixed ....

can you point out those errors please ?

(9) By Keith Medcalf (kmedcalf) on 2022-11-18 14:32:18 in reply to 8 [link] [source]

Look for the changes between what you posted and what I posted. Therein you will find the differences.

(10) By Keith Medcalf (kmedcalf) on 2022-11-18 14:34:23 in reply to 8 [link] [source]

Also, it is not a browser to return to, whateveer that means.

It is a standard program that reads from STDIN and writes to STDOUT. The fact that you are somehow farting about with a web browser is immaterial.

(11) By anonymous on 2022-11-19 10:56:28 in reply to 3.2 [link] [source]

thanks ... however even if I omit the *db reference the program crashes before it reaches it ... and returns a 500 error

I can only think that to execute the script possibly requires a dll as if you notice that I checked to see if there was a write and read error prior.

I am using watcom11 compiler on windows 7 and the script is executed via a web page on a abyss web server.

thanks for your thoughts

(12) By Kees Nuyt (knu) on 2022-11-19 12:19:26 in reply to 11 [link] [source]

I can only think that to execute the script possibly requires ...

Which script? Up to now, you only showed C++ code, that would compile to a .exe or a .ddl .

the script is executed via a web page on a abyss web server.

THe server returns 500 because the program or script crashes without error recovery. You might find a little more information in the web servers' error log.

How is it configured? Is it a CGI? In that case, leave out the web server and run the script or program standalone, or in a shell script, feeding the request parameters via an emulated CGI environment.

Just my thoughts. Until now, I don't see any SQLite problems.

-- 
Regards,
Kees Nuyt

(13) By Simon Slavin (slavin) on 2022-11-19 15:21:50 in reply to 1 [link] [source]

My guess is that the browser you're running it in does not have enough privileges to access to the folder you're keeping your database in.

If this is not clear, instead of opening the database file as if it is a SQLite database, open it as if it's a text file. Can you read the first seven characters from the file ? If not, fix the problem.

(14) By anonymous on 2022-11-20 02:47:41 in reply to 13 [link] [source]

thanks Simon,

I have recompiled the script as you suggested to return the db data and the return was as follows.

-------------------------------return in Browser

C++ TestSQLite Sample File : c:inetpubscriptsezidatasqlitefamilyguy.db

familyguy.db containes ...

SQLite format 3 e (FirstName varchar(30), LastName varchar(30), Age/GtablefoofooCREATE TABLE foo (bar STRING)

Step 1. Success : familyguy.db file opened using c++ fopen() and fclose(). Step 2. sqlite 'tryit' set to 1 ...skipping process sqlite3_open() due to crashing!! ''


my code currently is --------------------------------testsqlite.exe

// A small example program using SQLite with C++ compiled using Windows OS / Watcom11 to create testsqlite.exe

#include <stdio.h>

#include <iostream.h>

#include "sqlite3.h"

using namespace std;

#ifdef __cplusplus

#pragma inline_depth( 0 );

#endif

#ifndef SQLITE_EXTERN //-# define SQLITE_EXTERN extern

define SQLITE_EXTERN __declspec(dllexport)

#endif

static int callback(void *NotUsed, int argc, char **argv, char **azColName) { if(NotUsed){}//avoid error

int i; for(i=0; i<argc; i++) { cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"
n"; } cout<<"n"; return 0; }

int main(int argc, char *argv[]) {

if(argc && argv[0]){}//avoid compiler error during testing

//return output to web browser http://localhost/scripts/ezi/testsqlite.exe printf("Content-type: text/htmlnn"); printf("<!DOCTYPE html>n");
printf("<head>n");
printf("TestSQLiten");
printf("<style>n");
printf(".tc{ text-align:center; }n");
printf(".notice{ text-size:9pt; color:green; }n");
printf("</style>n");
printf("</head>n");
printf("<body>n"); printf("

C++ TestSQLite Sample

n");

//my local sqlite db file
char myfile[200]="c:\inetpub\scripts\ezi\data\sqlite\familyguy.db"; char buffer[1001];

printf("File : %s

n",myfile);

//read and return content of familyguy db file FILE *fp; if ((fp=fopen(myfile, "rb"))==NULL){ printf("familyguy.db error"); goto bot; } printf("familyguy.db containes ...

n"); while(fgets(buffer,1000,fp)!=NULL) { printf("%sn", buffer); } printf("

n"); fclose(fp);

printf("Step 1. Success : familyguy.db file opened using c++ fopen() and fclose().
n");

int tryit; tryit=true;

printf("Step 2. sqlite 'tryit' set to %d
n", tryit);

if(tryit) { const int STATEMENTS = 8; sqlite3 *db; char *zErrMsg = 0; const char *pSQL[STATEMENTS]; int rc = 0;

printf("...skipping process sqlite3_open() due to crashing!! ''
n"); goto bot;

rc = sqlite3_open(myfile, &db);//crashed here

//check if opened if( rc ) { // Don't have a sqlite3 pointer here, so cannot dereference it. // cout<<"Can't open database: "<<sqlite3_errmsg(db)<<"
n"; cout<<"Can't open as database "<<myfile<<"
n"; } else { cout<<"Open database successfully
nn"; }

// do some work with the database pSQL[0] = "create table myTable (FirstName varchar(30), LastName varchar(30), Age smallint, Hometown varchar(30), Job varchar(30))";

pSQL[1] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Peter', 'Griffin', 41, 'Quahog', 'Brewery')";

pSQL[2] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Lois', 'Griffin', 40, 'Newport', 'Piano Teacher')";

pSQL[3] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Joseph', 'Swanson', 39, 'Quahog', 'Police Officer')";

pSQL[4] = "insert into myTable (FirstName, LastName, Age, Hometown, Job) values ('Glenn', 'Quagmire', 41, 'Quahog', 'Pilot')";

pSQL[5] = "select * from myTable";

pSQL[6] = "delete from myTable";

pSQL[7] = "drop table myTable";

for(int i = 0; i < STATEMENTS; i++) { rc = sqlite3_exec(db, pSQL[i], callback, 0, &zErrMsg); if( rc!=SQLITE_OK ) { cout<<"SQL error: "<<sqlite3_errmsg(db)<<"
n"; sqlite3_free(zErrMsg); break; } } sqlite3_close(db); } bot: printf("</body>n");
printf("</html>n");
fcloseall(); return 0;

}

I am currently running sqlite in php and is working just fine.

When compiling to create my testsqlite.exe 32bit windows 32bit executable I am including sqlite3.c

I am at a loss to understand why this is not working.

thanks for the assist