0

I'm trying to insert values into my sqlite database in iphone application, but with no success:

insertSQL = [NSString stringWithFormat: @"INSERT INTO eventslist (date, title, description) VALUES ('%@', '%@', '%@')", dateString, title, descr];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(eventsDB, insert_stmt, -1, &statement, NULL);

My descr variable contains piece of text with special characters, new line characters etc. I assume that is a reason. How can I solve that?

Oleg
  • 2,984
  • 8
  • 43
  • 71

3 Answers3

4
+(void)CheckConnection
{
    NSArray *docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docdir = [docpath objectAtIndex:0];
    dbpath=[docdir stringByAppendingPathComponent:databasename];
    BOOL success;
    NSFileManager *fm = [NSFileManager defaultManager];
    success=[fm fileExistsAtPath:dbpath];
    if(success)
    {
        return;
    }
    NSString *dbPathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:databasename];
    [fm copyItemAtPath:dbPathFromApp toPath:dbpath error:nil];
}

+(BOOL)Savedata:(NSDictionary*)dicval
{
    [self CheckConnection];
    BOOL sucsess;
    sqlite3 *database;

    if (sqlite3_open([dbpath UTF8String], &database)==SQLITE_OK) {
        NSString *query=[NSString stringWithFormat:@"insert into emp_info values(NULL,'%@','%@','%@','%@')",[dicval valueForKey:@"name"],[dicval valueForKey:@"surname"],[dicval valueForKey:@"mobile_no"],[dicval valueForKey:@"city"]];
        const char *sqlStmt=[query UTF8String];
        sqlite3_stmt *cmpSqlStmt;
        int rv=sqlite3_prepare_v2(database, sqlStmt, -1, &cmpSqlStmt, NULL);

        sucsess=((rv ==SQLITE_OK)? YES : NO);
        sqlite3_step(cmpSqlStmt);
        sqlite3_finalize(cmpSqlStmt);
    }
    sqlite3_close(database);
    return sucsess;

}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

This link Contained Creating, insert and retrieve data from Database , It might be help.

Creating an SQLite3 database file through Objective-C

First Create Database and Open the it. Then Only try to insert data into Database.

Community
  • 1
  • 1
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
0
if(sqlite3_open([databasePath UTF8String],&db) == SQLITE_OK)
{
    insertSQL = [NSString stringWithFormat: @"INSERT INTO eventslist (date, title, description) VALUES ('%@', '%@', '%@')", dateString, title, descr];

    char *errmsg=nil;

    if(sqlite3_exec(db, [insertSQL UTF8String], NULL, NULL, &errmsg)==SQLITE_OK)
    {
       NSLog(@"Row Inserted");
    }
}
sqlite3_close(db);
Krunal
  • 6,440
  • 21
  • 91
  • 155