iOSデータベースの追加削除の作成

1839 ワード

FMDBデータベースフレームワークを使用しています
1.パスによるデータベースの作成
    self.db = [FMDatabase databaseWithPath:sqlFilePath];

2.データベースを開く
    if ([self.db open]) {
        NSLog(@"    ");
        BOOL success = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER DEFAULT 1)"];
        
        if (success) {
            NSLog(@"     ");
        } else {
            NSLog(@"     ");
        }
        
    } else {
        NSLog(@"    ");
    }
#pragma mark     

static NSInteger age = 10;
    for (int i = 0; i < 20; i++) {
        age++;
        BOOL success = [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES (?, ?);", @"jack", @(age)];
        [self.db executeUpdate:@"INSERT INTO t_student (name, age) VALUES(?,?);",@"jack",@(age)];
        if (success) {
            NSLog(@"    ");
        } else {
            NSLog(@"    ");
        }
    }

pragma markデータ削除
BOOL success = [self.db executeUpdate:@"DELETE FROM t_student WHERE age > 20 AND age < 25;"];

pragma markデータの変更
BOOL success = [self.db executeUpdate:@"UPDATE t_student SET name = 'liwx' WHERE age > 12 AND age < 15;"];

pragma markクエリーデータ
FMResultSet *result = [self.db executeQuery:@"SELECT id, name, age FROM t_student WHERE age > 25;"];
    while ([result next]) {
        int ID = [result intForColumnIndex:0];
        NSString *name = [result stringForColumnIndex:1];
        int age = [result intForColumn:@"age"];
        
        NSLog(@"ID: %zd, name: %@, age: %zd", ID, name, age);
    }