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 回答
1

此链接包含从数据库创建、插入和检索数据,它可能会有所帮助。

通过 Objective-C 创建 SQLite3 数据库文件

首先创建数据库并打开它。然后只尝试将数据插入数据库。

于 2012-08-06T12:22:13.650 回答
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 回答