LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2013, 05:16 AM   #1
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Rep: Reputation: 18
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.
 
Old 10-29-2013, 02:42 AM   #2
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
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
what else should I do? what is wrong yet?
 
Old 10-29-2013, 08:26 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 10-31-2013, 02:00 AM   #4
golden_boy615
Member
 
Registered: Dec 2008
Distribution: Ubuntu Fedora
Posts: 445

Original Poster
Rep: Reputation: 18
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;
 }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C sqlite program gives unable to open database file golden_boy615 Programming 6 10-18-2013 12:58 PM
[SOLVED] How to link the packet in C file then store in sqlite database? Newbie89 Linux - Networking 2 05-06-2013 02:28 PM
LXer: Sqlite-Commander - A ncurses based tool to display the records and tables of a sqlite database LXer Syndicated Linux News 0 01-02-2011 08:11 AM
[SOLVED] SQLite: Trying to open a database file in sqlitebrowser returns an error Kenny_Strawn Linux - Server 2 10-31-2010 05:24 PM
[SOLVED] sqlite jdbc Exception: unable to open database file pengrath Programming 1 07-10-2009 03:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration