Write NSDictionary Array to SQLite3 Database

时间:2017-08-05 11:51:36

标签: ios objective-c xcode sqlite

how can I write data from my NSDictionary * dictTemp to my SQLite3 database / update? What is the best solution. It's about updating a field (rating )

NSDictionary Demo Output :

2017-08-05 13:23:54.048340 TestApp [1904:979083] Club ID : 101  Value 3
2017-08-05 13:23:54.048498 TestApp [1904:979083] Club ID : 102  Value 1
2017-08-05 13:23:54.048579 TestApp [1904:979083] Club ID : 103  Value 5

SQLite3 Database :

enter image description here

Thanks for help

my code:

insert JSON to NSDictionary:

NSURL *baseURL = [NSURL URLWithString:getLiveRating];
    NSString *path = [NSString stringWithFormat:@"LiveRatingDemo.json"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id JSON)
     {
         NSDictionary *dictTemp = (NSDictionary *) JSON;
         NSArray *arrRating = [dictTemp objectForKey:@"ratingArray"];


         for (int i=0; i<arrRating.count; i++) {
             GetRating *rat = [[GetRating alloc] init];
             [rat parseResponse:[arrRating objectAtIndex:i]];
             [mratingLive addObject:rat];

         }
         NSLog(@"Success: %@", JSON);

save to database:

-(void)updateDatabase    
{


    sqlite3_stmt    *statement;

    NSString* db_ort = [DOCS_FOLDER stringByAppendingPathComponent:DB_NAME];

    const char *dbpath = [db_ort UTF8String];

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO restaurants (rating) VALUES (\"%@\")", ratingValue];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            ratingValue = @"Rating Insert OK!";

        } else {
            ratingValue = @"Rating error ";
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);
    }

What did I wrong this?

-(void)updateDatabase    
{
    NSLog(@"Datenbank wird Aktualisiert (get Rating JSON Data)");

    sqlite3_stmt    *statement;

    NSString* db_ort = [DOCS_FOLDER stringByAppendingPathComponent:DB_NAME];

    const char *dbpath = [db_ort UTF8String];

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
    {
       // NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO restaurants (rating) VALUES (\"%@\")", ratingValue];

        for (int i=0; i<mratingLive.count; i++) {
            GetRating *rat = [mratingLive objectAtIndex:i];

        NSString *insertSQL = [NSString stringWithFormat:@"update restaurants Set rating = '%@' WHERE restaurantid = '%@'", rat.ratingValue, rat.ratingClubID];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            rat.ratingValue = @"Rating Insert OK!";

        } else {
            rat.ratingValue = @"Rating error ";
        }
        sqlite3_finalize(statement);
        sqlite3_close(contactDB);


        }
    }
}

How can I relate the right strings , NSString... in my code

1 个答案:

答案 0 :(得分:0)

如果要更新SQLite数据库中的字段,请按照以下代码

进行操作
input.cv-dashboard-input {
    font-family: Roboto;
    font-weight: 300;
    color: #004d6f;
    border: 1px solid #adadad;
    border-radius: 0px;
    font-size: 14px;
    transition: all 0.3s;
    margin: 0;
    box-sizing: border-box;
}

input.cv-input {
    height: 42px;
    width: 100%;
    padding: 5px;
    font-family: Roboto;
    font-weight: 300;
}