0

我将图像存储在服务器 url 中并从 JSON 中获取图像然后写入本地数据库。但是 NSLog 显示categories insert is insert into categories (id,cat_name,order_by) values('3','test','3'). 但值未显示在 db1.sqlite 文件中。

代码:

文件管理器:

- (void)viewDidLoad
{
 NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSError *err;

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"db4" ofType:@"sqlite"];
    //NSLog(@"bundlePath %@", bundlePath);


  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSLog(@"docs dir is %@", documentsDirectory);

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];

  BOOL success = [fileMgr copyItemAtPath:bundlePath toPath:appFile error:&err];


    if (!success) {
        NSLog(@"Failed to create writable database file with message '%@'.", [err localizedDescription]);
    }

从服务器 url 获取数据添加到数据库文件:

NSURL *URL = [NSURL URLWithString:@"http://serverurl.net/projects/mobile/jsonstring.php"];

     NSError *error;
    NSString *stringFromFileAtURL = [[NSString alloc]
                                     initWithContentsOfURL:URL
                                     encoding:NSUTF8StringEncoding
                                     error:&error];



    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
    //NSLog(@"filepath %@",path);



    NSArray *userData = [stringFromFileAtURL JSONValue];
    [stringFromFileAtURL release];


    NSLog(@"userdata is %@", userData);

    int  i = 0;
    BOOL notExist = TRUE;
    sqlite3_stmt *statement, *addStmt;


    for (NSArray *skarray in userData) {
        //NSLog(@"test");


        if(i == 0){
            //insert all main category
            for (NSDictionary *tuser in skarray) {

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

                    const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];


                    NSLog(@"categories select is %s", sql);

                    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

                        notExist = TRUE;

                        while (sqlite3_step(statement) == SQLITE_ROW) {

                            notExist = FALSE;

                        }

                    }



                    if(notExist){


                        NSLog(@"categories does not exist");


                     //   const char *sqlInsert = "insert into categories (id,cat_name,order_by) Values(?, ?, ?)";

                        const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (id,cat_name,order_by) values('%@','%@','%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_name"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];


                        NSLog(@"categories insert is %s", sqlInsert);

                        if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) != SQLITE_OK)
                            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));



                        sqlite3_bind_text(addStmt, 0, [[tuser objectForKey:@"id"] UTF8String], -1, SQLITE_TRANSIENT);

                        sqlite3_bind_text(addStmt, 1, [[tuser objectForKey:@"cat_name"] UTF8String], -1, SQLITE_TRANSIENT);
                        sqlite3_bind_text(addStmt, 2, [[tuser objectForKey:@"order_by"] UTF8String], -1, SQLITE_TRANSIENT);


        if(SQLITE_DONE != sqlite3_step(addStmt))

   NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));

                        else
                            sqlite3_reset(addStmt);

      }

                    sqlite3_close(database);


                }



            }
        }

我问了很多问题。但没有得到解决。

4

1 回答 1

0

您的参数索引错误;它们必须从 1 开始,并且您的语句没有任何参数。

你不需要sqlite3_reset

您必须始终调用sqlite3_finalize

于 2013-08-20T13:15:57.250 回答