0

请任何人帮助我。我无法将数据插入到 sqlite。

输出是“新闻添加失败”。

注意: - 数据库已经创建。- 新闻是一个对象我只是认为问题出在这个函数上。你能告诉我我的代码有什么问题吗?谢谢。

- (void)addNews:(NSString *)content_caption content_date:(NSString *)content_date content_desk:(NSString *)content_desk content_id:(NSString *)content_id content_img:(NSString *)content_img content_type:(NSString *)content_type content_web_url:(NSString *)content_web_url{


char *error;
if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {
    NSString *inserStmt = [NSString stringWithFormat:@"INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\',\'%@\')",content_caption, content_date, content_desk, content_id, content_img, content_type ,content_web_url];

    const char *insert_stmt = [inserStmt UTF8String];

    if (sqlite3_exec(database, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
        NSLog(@"News added");

        News *news = [[News alloc]init];

        [news setContent_caption:content_caption];
        [news setContent_date:content_date];
        [news setContent_desk:content_desk];
        [news setContent_id:content_id];
        [news setContent_img:content_img];
        [news setContent_type:content_type];
        [news setContent_web_url:content_web_url];

        [arrayOfNews addObject:news];
    }else{
        NSLog(@"News added failed");
    }
    sqlite3_close(database);
}

}

4

2 回答 2

1

首先,您需要设置数据库的路径。这样做

// 设置路径

  NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *docsDir = [dirPaths objectAtIndex:0];
   databasepath =[docsDir stringByAppendingPathComponent: @"yourDatabase"];
 NSFileManager *fn=[NSFileManager defaultManager];
 NSError *error;
 BOOL success=[fn fileExistsAtPath:databasepath];
   if(!success) {
           NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"quizdb"];
    success = [fn copyItemAtPath:defaultDBPath toPath:databasepath error:&error];
}

//接下来你要插入如下

 sqlite3_stmt *statement;
 const char *dbpath=[databasepath UTF8String];
 if(sqlite3_open(dbpath,&contactDB)==SQLITE_OK)
 {
    const char *sqlStatement = "insert into dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) VALUES (?,?,?,?,?,?,?)";
    if (sqlite3_prepare_v2(contactDB,sqlStatement,-1,&statement,NULL) == SQLITE_OK)
    {
        sqlite3_bind_text(statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);      
        sqlite3_bind_text(statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3 ,[content_desk UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);           
    }
    if (sqlite3_step(statement)!= SQLITE_DONE)
    {
        NSLog(@"Save Error:%s",sqlite3_errmsg(contactDB));
    }
    else {

        NSLog(@"success");

        sqlite3_reset(statement);
               }
            sqlite3_finalize(statement);
    }
     sqlite3_close(contactDB);

希望对你有帮助。。

于 2013-07-09T11:30:46.920 回答
0

试试这个

    #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)

    const char *insertSql;
    insertSql = nil;
    insertSql = "INSERT INTO dBase(CONTENT_CAPTION,CONTENT_DATE, CONTENT_DESK, CONTENT_ID, CONTENT_IMG, CONTENT_TYPE, CONTENT_WEB_URL) values (?,?,?,?,?,?,?)";

    if (sqlite3_open([databasePath UTF8String], &database)==SQLITE_OK) {

        sqlite3_bind_text(insert_statement, 1, [content_caption UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 2, [content_date UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 3, [content_desk UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 4, [content_id UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 5, [content_img UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 6, [content_type UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 7, [content_web_url UTF8String], -1, SQLITE_TRANSIENT);

    }
于 2013-07-09T11:11:26.800 回答