sqlite db上的C程序无法打开数据库文件

时间:2013-10-27 10:31:20

标签: c database sqlite

首先,我必须说这不是一个重复的线程,因为我的另一个与此主题相关的类似线程。 我为一些测试写了这个简单的sqlite C程序,但它没有在MySqlite_close函数内关闭sqlite db而没有错误,但是一段时间它达到了它打开的最大文件时打印无法打开数据库文件:

#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;
 }

这是我的示例输出:

.
.
.
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 
.
.
.

2 个答案:

答案 0 :(得分:0)

您忘了拨打sqlite3_finalize,因此数据库永远不会关闭。

请注意,the documentation适用于最新的SQLite版本;你的行为可能会有所不同。

答案 1 :(得分:0)

我终于找到了我有指针问题的解决方案。我的整个更改代码如下注意Mysqlite_exec函数中的db指针的更改和其他函数中的其他调用,我没有最终确定我的语句:

#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;
 }