dimanche 8 février 2015

How to use same db path to create multiple tables using SQLite in iOS ?

I am developing an iOS application in objective-c using sqllite3 as local DB to store some data. I am trying to use multiple tables with the same db path, but it does not allow me to do so.


For instance, 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 paths, then it functions normally. ex : db path as DB1 for table T1 , DB2 for table T2 and so on.


Here is my code:


Statement to create table :



-(int) createTable
{
sqlite3* db = NULL;
int rc=0;
rc = sqlite3_open_v2([[self getDbFilePath] cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (SQLITE_OK != rc)
{
sqlite3_close(db);
NSLog(@"Failed to open db connection");
}
else
{
const char * query ="CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, selfuserid TEXT, profilepic TEXT, username TEXT, gender TEXT, profilelink TEXT );"

"CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY AUTOINCREMENT, id TEXT, info TEXT, date TEXT, players TEXT, name TEXT, time TEXT,userid TEXT, area TEXT, day TEXT, Name TEXT, Pic TEXT,total TEXT,rank 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);
}
NSLog(@"bangayiii");
return rc;
}


db path function :



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

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

}


Help is much appreciated, thanks in advance!


Aucun commentaire:

Enregistrer un commentaire