0

iPhoneアプリケーションでsqliteデータベースに値を挿入しようとしていますが、成功しません:

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);

私のdescr変数には、特殊文字、改行文字などを含むテキストが含まれています。それが理由だと思います。どうすれば解決できますか?

4

3 に答える 3

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;

}
于 2012-11-19T09:41:39.497 に答える
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);
于 2012-08-06T12:46:59.957 に答える