/** * 代码创建数据库 */ - (void)createDatabaseIfNeeded { // 数据库路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *doc = [paths objectAtIndex:0]; NSString *path = [doc stringByAppendingPathComponent:@"test.sqlite"]; NSFileManager * fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:path] == NO) { // create it FMDatabase * db = [FMDatabase databaseWithPath:path]; if ([db open]) { // 建立User表 NSMutableString *sql = [NSMutableString stringWithFormat:@"%@",@"CREATE TABLE User"]; [sql appendString:@"("]; [sql appendString:@"username varchar,"]; [sql appendString:@"password varchar"]; [sql appendString:@")"]; BOOL res = [db executeUpdate:sql]; if (!res) { debugLog(@"error when creating table User"); } else { debugLog(@"succ to creating table User"); } [db close]; } else { debugLog(@"error when open db"); } } } |