-
-
Save 0xlitf/c83b12f5f77f0d86a6f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create | |
NSFileManager * fileManager = [NSFileManager defaultManager]; | |
if ([fileManager fileExistsAtPath:self.dbPath] == NO) { | |
// create it | |
FMDatabase * db = [FMDatabase databaseWithPath:self.dbPath]; | |
if ([db open]) { | |
NSString * sql = @"CREATE TABLE 'User' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'name' VARCHAR(30), 'password' VARCHAR(30))"; | |
BOOL res = [db executeUpdate:sql]; | |
if (!res) { | |
debugLog(@"error when creating db table"); | |
} else { | |
debugLog(@"succ to creating db table"); | |
} | |
[db close]; | |
} else { | |
debugLog(@"error when open db"); | |
} | |
} | |
//insert | |
static int idx = 1; | |
FMDatabase * db = [FMDatabase databaseWithPath:self.dbPath]; | |
if ([db open]) { | |
NSString * sql = @"insert into user (name, password) values(?, ?) "; | |
NSString * name = [NSString stringWithFormat:@"tangqiao%d", idx++]; | |
BOOL res = [db executeUpdate:sql, name, @"boy"]; | |
if (!res) { | |
debugLog(@"error to insert data"); | |
} else { | |
debugLog(@"succ to insert data"); | |
} | |
[db close]; | |
} | |
//query | |
debugMethod(); | |
FMDatabase * db = [FMDatabase databaseWithPath:self.dbPath]; | |
if ([db open]) { | |
NSString * sql = @"select * from user"; | |
FMResultSet * rs = [db executeQuery:sql]; | |
while ([rs next]) { | |
int userId = [rs intForColumn:@"id"]; | |
NSString * name = [rs stringForColumn:@"name"]; | |
NSString * pass = [rs stringForColumn:@"password"]; | |
debugLog(@"user id = %d, name = %@, pass = %@", userId, name, pass); | |
} | |
[db close]; | |
} | |
//clear | |
FMDatabase * db = [FMDatabase databaseWithPath:self.dbPath]; | |
if ([db open]) { | |
NSString * sql = @"delete from user"; | |
BOOL res = [db executeUpdate:sql]; | |
if (!res) { | |
debugLog(@"error to delete db data"); | |
} else { | |
debugLog(@"succ to deleta db data"); | |
} | |
[db close]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment