I'm building an iOS app using storyboards.I have integrated sqlite database in my app. I'm unable to insert data into the table,i'm getting this error: Failed to open db connection
I have created twoo more table with the same code that is working fine but this sports is my third table in which i'm getting this error.
here is my code
//SQLlite database code used to get file path
-(NSString *) getSportsFilePath
{
NSString * docsPath= NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES)[0];
return [docsPath stringByAppendingPathComponent:@"sportsdb.db"];
}
-(int) createTable:(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);
}
else
{
char * query ="CREATE TABLE IF NOT EXISTS sportsselection (id INTEGER PRIMARY KEY AUTOINCREMENT, sportslist 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;
}
//SQLlite database code is used to insert data into the table
-(int) insert:(NSString *)filePath withName:(NSString *)sportslist
{
sqlite3* db = NULL;
int rc=0;
rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
if (SQLITE_OK != rc)
{
sqlite3_close(db);
NSLog(@"Failed to open db connection");
}
else
{
NSString * query = [NSString
stringWithFormat:@"INSERT INTO sportsselection (sportslist) VALUES (\"%@\")",sportslist];
char * errMsg;
rc = sqlite3_exec(db, [query UTF8String] ,NULL,NULL,&errMsg);
if(SQLITE_OK != rc)
{
NSLog(@"Failed to insert record rc:%d, msg=%s",rc,errMsg);
}
sqlite3_close(db);
}
return rc;
}
- (void)viewDidLoad {
if( _sports){
for (int j=0;j< _sports.count;j++)
{
int rc= [self insert:[self getSportsFilePath] withName: _sports[j]];
if(rc != SQLITE_OK)
{
NSLog(@"Failed to insert record");
}
else
NSLog(@"Record is added");
}}
}
Aucun commentaire:
Enregistrer un commentaire