0

I am developing an iOS application in objective-c and i am using sqllite3 as local DB to hold some data.

There are multiple tables that I need to use but it not happening. If I try to create multiple tables in same DB {db path as DB1} then the second table is not created.

But if I try to create different tables in different db path, then it functions normally. ex : db path as DB1 for table T1 , DB2 for table T2 and so on ....

I am not able to understand Why is this happening!

Here is the code :

Statement to create table :

if(![[NSFileManager defaultManager] fileExistsAtPath:[self getEventDbFilePath]]) //if the file does not exist
    {
        [self createTable:[self getEventDbFilePath]];
    }

db path function :

-(NSString *) getUserInfoDbFilePath
{
    NSString * docsPath= NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"docpath=%@",docsPath);

    return [docsPath stringByAppendingPathComponent:@"userinfo.db"];

}

create table

-(int) createUserInfoTable:(NSString*) filePath
   {
       sqlite3* db = NULL;
       int rc=0;

       rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
       if (SQLITE_OK != rc)
       {
           sqlite3_close(db);
           NSLog(@"Failed to open db connection");
       }
       else
       {
           char * query ="CREATE TABLE IF NOT EXISTS userinfo (id INTEGER PRIMARY KEY AUTOINCREMENT, selfuserid  TEXT, profilepic TEXT, username TEXT, gender TEXT, profilelink TEXT )";
           char * errMsg;
           rc = sqlite3_exec(db, query,NULL,NULL,&errMsg);

           if(SQLITE_OK != rc)
           {
               NSLog(@"Failed to create table rc:%d, msg=%s",rc,errMsg);
           }

           sqlite3_close(db);
       }

       return rc;

   }

If I create a new table with similar function as above and different table name, with same db path, the second table will not be created.

Now for second table, if I provide a new path, the table gets created in thew db path.

Any suggestions would be great.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
madhavan
  • 115
  • 1
  • 8

0 Answers0