C program on sqlite db gives unable to open database file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C program on sqlite db gives unable to open database file
Hi
first of all I have to say that I this is not a duplicate thread due to another simililar thread of mine related to this topic.
I wrote this simple sqlite C program for some tests but it does not close sqlite db with no error but after a while when it reached its maximum opened files it prints unable to open database file:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#define SQLITE_MAIN_BASE "/var/"
#define SQLITE_DEVLOG_BASE "./"
#define Create_All_Database_Error 1
MySqlite_close(sqlite3 *db)
{
int i=0,
rc=0;
rc=sqlite3_close(db);
while(rc != SQLITE_OK)
{
printf("yet closing\n");
if (rc == SQLITE_BUSY)
{
printf("it is busy\n");
i++;
if ( i > 10 )
{
return rc;
}
}
sleep(1);
rc=sqlite3_close(db);
}
printf("2closeeeeeee\n\n");
return 0;
}
int MySqlite_Exec(const char *dbname,sqlite3 *db,const char *query,sqlite3_stmt **retStmt,const char *queryTail2,const char *logpath,int logfd,int mode)
{
char logmessage[1500];
char dbfilepath[150];
int rc=0;
sprintf(dbfilepath,"%s%s",SQLITE_DEVLOG_BASE,dbname);
fprintf(stdout,"%s\n",query);
while(sqlite3_open(dbfilepath, &db))
{
sprintf(logmessage,"1Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
sleep(10);
MySqlite_close(db);
return 0;
}
printf("10\n");
if( sqlite3_prepare_v2(db, query, -1, retStmt,NULL) != SQLITE_OK )
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
while(sqlite3_prepare_v2(db, query, -1, retStmt,NULL) == SQLITE_BUSY)
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
sleep(1);
}
}
printf("12\n");
if (mode==0)
{
printf("222\n");
if (sqlite3_step(*retStmt) != SQLITE_DONE)
{
sprintf(logmessage,"3Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
}
if (sqlite3_reset(*retStmt) != SQLITE_OK)
{
sprintf(logmessage,"5Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
}
if ( *retStmt != NULL )
{
printf("retStmt is not NULL\n");
while(sqlite3_finalize(*retStmt)!=SQLITE_OK)
{
sprintf(logmessage,"20Error on : %u %s ",sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
printf("finilized NOT ok\n");
sleep(1);
}
printf("finilized ok\n");
*retStmt=NULL;
}
else
{
printf("retStmt is NULL\n");
}
MySqlite_close(db);
}
return 0;
}
int Create_SqltDB(void)
{
char hostip[20],
LOG_FILE[100],
query[1000],
dbfilepath[100],
logmessage[1500];
int result=0;
const char *queryTail;
sqlite3 *db;
sqlite3_stmt *retStmt=NULL;
sprintf(LOG_FILE,"/var/log/Emain.log");
sprintf(query,"create table if not exists lastuptime(row integer primary key not NULL,microupdatetime double default 0 not NULL,time double default 0 not NULL);");
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2,0))
{
return Create_All_Database_Error;
}
return 0;
}
int main()
{
sqlite3 *db;
Create_SqltDB();
char query[1000],
logmessage[1500],
LOG_FILE[100];
int i=0,
result=0;
sqlite3_stmt *retStmt=NULL;
const char *queryTail;
/*
for (i=0; i<1100;i++)
{
sprintf(query,"insert into lastuptime(microupdatetime,time) values (%i,%i);",i,i);
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2,0))
{
return Create_All_Database_Error;
}
}
*/
while(1)
{
sprintf(query,"select * from lastuptime where row%%1000=0;");
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2,1))
{
return Create_All_Database_Error;
}
do
{
result = sqlite3_step (retStmt) ;
if (result == SQLITE_ROW) /* can read data */
{
printf(" %d \t|\t %f \t|\t '%f' \n",\
sqlite3_column_int(retStmt,0),\
sqlite3_column_double(retStmt,1),\
sqlite3_column_double(retStmt,2)) ;
}
else
{
printf("no data\n");
}
} while (result == SQLITE_ROW) ;
printf("s1\n");
MySqlite_close(db);
printf("s2\n");
}
return 0;
}
here is my sample output:
Quote:
.
.
.
select * from lastuptime where row%1000=0;
10
12
1000 | 899.000000 | '899.000000'
no data
s1
2closeeeeeee
s2
select * from lastuptime where row%1000=0;
10
12
1000 | 899.000000 | '899.000000'
no data
s1
2closeeeeeee
s2
select * from lastuptime where row%1000=0;
1Error on "select * from lastuptime where row%1000=0;" : 14 unable to open database file
2closeeeeeee
1000 | 899.000000 | '899.000000'
no data
s1
2closeeeeeee
s2
select * from lastuptime where row%1000=0;
1Error on "select * from lastuptime where row%1000=0;" : 14 unable to open database file
.
.
.
why MySqlite_close() does not close db ?
thanks for any help.
I changed my code as follow to finalize my retStmt but did not helped:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include "regex.c"
#define SQLITE_MAIN_BASE "/var/"
#define SQLITE_DEVLOG_BASE "./"
#define Create_All_Database_Error 1
MySqlite_close(sqlite3 *db)
{
int i=0,
rc=0;
rc=sqlite3_close(db);
while(rc != SQLITE_OK)
{
printf("yet closing\n");
if (rc == SQLITE_BUSY)
{
printf("it is busy\n");
i++;
if ( i > 10 )
{
return rc;
}
}
sleep(1);
rc=sqlite3_close(db);
}
printf("2closeeeeeee\n\n");
return 0;
}
int Mysqlite_finalize(sqlite3 *db,sqlite3_stmt **retStmt)
{
char logmessage[1500];
if (sqlite3_reset(*retStmt) != SQLITE_OK)
{
sprintf(logmessage,"5Error is : %u %s ",sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
}
if ( *retStmt != NULL )
{
printf("retStmt is not NULL\n");
while(sqlite3_finalize(*retStmt)!=SQLITE_OK)
{
sprintf(logmessage,"20Error on : %u %s ",sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
printf("finilized NOT ok\n");
sleep(1);
}
printf("finilized ok\n");
*retStmt=NULL;
}
else
{
printf("retStmt is NULL\n");
}
return 0;
}
int MySqlite_Exec(const char *dbname,sqlite3 *db,const char *query,sqlite3_stmt **retStmt,const char *queryTail2,const char *logpath,int logfd)
{
char logmessage[1500];
char dbfilepath[150];
int rc=0;
sprintf(dbfilepath,"%s%s",SQLITE_DEVLOG_BASE,dbname);
fprintf(stdout,"%s\n",query);
while(sqlite3_open(dbfilepath, &db))
{
sprintf(logmessage,"1Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
sleep(10);
MySqlite_close(db);
return 0;
}
printf("10\n");
if( sqlite3_prepare_v2(db, query, -1, retStmt,NULL) != SQLITE_OK )
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
while(sqlite3_prepare_v2(db, query, -1, retStmt,NULL) == SQLITE_BUSY)
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
sleep(1);
}
}
printf("12\n");
if ( MyRegexMatch(query,"^select",REG_ICASE) !=0 )
{
printf("222\n");
if (sqlite3_step(*retStmt) != SQLITE_DONE)
{
sprintf(logmessage,"3Error on \"%s\" : %u %s ",query,sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
}
Mysqlite_finalize(db,retStmt);
MySqlite_close(db);
}
return 0;
}
int Create_SqltDB(void)
{
char hostip[20],
LOG_FILE[100],
query[1000],
dbfilepath[100],
logmessage[1500];
int result=0;
const char *queryTail;
sqlite3 *db;
sqlite3_stmt *retStmt=NULL;
sprintf(LOG_FILE,"/var/log/Emain.log");
sprintf(query,"create table if not exists lastuptime(row integer primary key not NULL,microupdatetime double default 0 not NULL,time double default 0 not NULL);");
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
return 0;
}
int main()
{
sqlite3 *db;
Create_SqltDB();
char query[1000],
logmessage[1500],
LOG_FILE[100];
int i=0,
result=0;
sqlite3_stmt *retStmt=NULL;
const char *queryTail;
for (i=0; i<100;i++)
{
sprintf(query,"insert into lastuptime(microupdatetime,time) values (%i,%i);",i,i);
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
}
while(1)
{
sprintf(query,"select * from lastuptime where row%%100=0;");
if (MySqlite_Exec("lastuptime",db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
do
{
result = sqlite3_step (retStmt) ;
if (result == SQLITE_ROW) /* can read data */
{
printf(" %d \t|\t %f \t|\t '%f' \n",\
sqlite3_column_int(retStmt,0),\
sqlite3_column_double(retStmt,1),\
sqlite3_column_double(retStmt,2)) ;
}
else
{
printf("no data\n");
}
} while (result == SQLITE_ROW) ;
printf("s1\n");
Mysqlite_finalize(db,&retStmt);
MySqlite_close(db);
printf("s2\n");
}
return 0;
}
do not confuse with MyRegexMatch(query,"^select",REG_ICASE) in Mysqlite_exec I wrote it myself and it works fine and it is just for finding select queries . my out put is as follow and the result of lsof -p pidof MYPROCESSNAME is the same I mean it does not close db again :
Quote:
s2
select * from lastuptime where row%100=0;
10
12
100 | 99.000000 | '99.000000'
200 | 99.000000 | '99.000000'
300 | 99.000000 | '99.000000'
no data
s1
retStmt is not NULL
finilized ok
2closeeeeeee
s2
select * from lastuptime where row%100=0;
1Error on "select * from lastuptime where row%100=0;" : 14 unable to open database file
2closeeeeeee
no data
s1
retStmt is NULL
2closeeeeeee
s2
select * from lastuptime where row%100=0;
1Error on "select * from lastuptime where row%100=0;" : 14 unable to open database
I do not know of any condition in SQLite whereby it "takes any time at all" to close a file. (Because that is all that SQLite is doing: "closing a file." There is no "external server" involved.)
Nor, for that matter, to open one.
What is the error-code that is being returned from these calls? What is the number, and what does it mean.
the error and the error coe are in my post.
I finally found the solution I had pointer problem . my whole changed code is as follow attention the change of db pointer in Mysqlite_exec function and other calls in other functions:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include "regex.c"
#define SQLITE_MAIN_BASE "/var/"
#define SQLITE_DEVLOG_BASE "./"
#define Create_All_Database_Error 1
MySqlite_close(sqlite3 *db)
{
int i=0,
rc=0;
rc=sqlite3_close(db);
while(rc != SQLITE_OK)
{
printf("yet closing\n");
if (rc == SQLITE_BUSY)
{
printf("it is busy\n");
i++;
if ( i > 10 )
{
return rc;
}
}
sleep(1);
rc=sqlite3_close(db);
}
printf("2closeeeeeee\n\n");
return 0;
}
int Mysqlite_finalize(sqlite3 *db,sqlite3_stmt **retStmt)
{
char logmessage[1500];
/* if (sqlite3_reset(*retStmt) != SQLITE_OK)
{
sprintf(logmessage,"5Error is : %u %s ",sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
}
*/
if ( *retStmt != NULL )
{
printf("retStmt is not NULL\n");
while(sqlite3_finalize(*retStmt)!=SQLITE_OK)
{
sprintf(logmessage,"20Error on : %u %s ",sqlite3_errcode(db), sqlite3_errmsg(db));
printf("%s\n",logmessage);
printf("finilized NOT ok\n");
sleep(1);
}
printf("finilized ok\n");
*retStmt=NULL;
}
else
{
printf("retStmt is NULL\n");
}
return 0;
}
int MySqlite_Exec(const char *dbname,sqlite3 **db,const char *query,sqlite3_stmt **retStmt,const char *queryTail2,const char *logpath,int logfd)
{
char logmessage[1500];
char dbfilepath[150];
int rc=0;
sprintf(dbfilepath,"%s%s",SQLITE_DEVLOG_BASE,dbname);
fprintf(stdout,"%s\n",query);
while(sqlite3_open(dbfilepath, db))
{
sprintf(logmessage,"1Error on \"%s\" : %u %s ",query,sqlite3_errcode(*db), sqlite3_errmsg(*db));
printf("%s\n",logmessage);
sleep(10);
MySqlite_close(*db);
return 0;
}
printf("10\n");
if( sqlite3_prepare_v2(*db, query, -1, retStmt,NULL) != SQLITE_OK )
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(*db), sqlite3_errmsg(*db));
printf("%s\n",logmessage);
while(sqlite3_prepare_v2(*db, query, -1, retStmt,NULL) == SQLITE_BUSY)
{
sprintf(logmessage,"2Error on \"%s\" : %u %s ",query,sqlite3_errcode(*db), sqlite3_errmsg(*db));
printf("%s\n",logmessage);
sleep(1);
}
}
printf("12\n");
if ( MyRegexMatch(query,"^select",REG_ICASE) !=0 )
{
printf("222\n");
if (sqlite3_step(*retStmt) != SQLITE_DONE)
{
sprintf(logmessage,"3Error on \"%s\" : %u %s ",query,sqlite3_errcode(*db), sqlite3_errmsg(*db));
printf("%s\n",logmessage);
}
Mysqlite_finalize(*db,retStmt);
MySqlite_close(*db);
}
return 0;
}
int Create_SqltDB(void)
{
char hostip[20],
LOG_FILE[100],
query[1000],
dbfilepath[100],
logmessage[1500];
int result=0;
const char *queryTail;
sqlite3 *db;
sqlite3_stmt *retStmt=NULL;
sprintf(LOG_FILE,"/var/log/Emain.log");
sprintf(query,"create table if not exists lastuptime(row integer primary key not NULL,microupdatetime double default 0 not NULL,time double default 0 not NULL);");
if (MySqlite_Exec("lastuptime",&db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
return 0;
}
int main()
{
sqlite3 *db;
Create_SqltDB();
char query[1000],
logmessage[1500],
LOG_FILE[100];
int i=0,
result=0;
sqlite3_stmt *retStmt=NULL;
const char *queryTail;
for (i=0; i<100;i++)
{
sprintf(query,"insert into lastuptime(microupdatetime,time) values (%i,%i);",i,i);
if (MySqlite_Exec("lastuptime",&db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
}
while(1)
{
sprintf(query,"select * from lastuptime where row%%100=0;");
if (MySqlite_Exec("lastuptime",&db,query,&retStmt,queryTail,LOG_FILE,2))
{
return Create_All_Database_Error;
}
while(sqlite3_step(retStmt) == SQLITE_ROW) /* can read data */
{
printf(" %d \t|\t %f \t|\t '%f' \n",\
sqlite3_column_int(retStmt,0),\
sqlite3_column_double(retStmt,1),\
sqlite3_column_double(retStmt,2)) ;
}
printf("s1\n");
Mysqlite_finalize(db,&retStmt);
MySqlite_close(db);
printf("s2\n");
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.