FMDB可以使用OC的方法操作数据库。
百度可以搜到FMDB的相关信息:使用方法、框架
/*
1、打开关闭数据库
*/
// 1、打开关闭数据库
NSString *path = [NSHomeDirectory()stringByAppendingPathComponent:@"fmdb.sqlite"];
// 找到数据库的路径
FMDatabase *db = [FMDatabase databaseWithPath:path];
// 打开数据库
BOOL isOpen = [db open];//BOOL类型
/*
系统自带的打开数据库方式sqlite3_open(path, &db)返回一个int
*/
// 数据库关闭
// [db close];
// 系统:sqlite3_close(db)
/*
数据库增删改等操作:
除了查询操作,FMDB数据库操作都执行executeUpdate方法,这个方法返回BOOL类型
*/
[db executeUpdate:@"增删该sql语句"];
// 系统: sqlite3_exec(db, [@"增删改sql语句" UTF8String], NULL, NULL, &error)
NSString *TABLENAME = @"user";
NSString *ID = @"ID";
NSString *NAME = @"NAME";
NSString *AGE = @"AGE";
NSString *ADDRESS = @"ADDRESS";
// 创建表
if ([db open]) {
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
}
// 添加数据:
if ([db open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"张三",@"13",@"济南"];
BOOL res = [db executeUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"李四",@"12",@"济南"];
BOOL res2 = [db executeUpdate:insertSql2];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
if (!res2) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
// 修改数据:
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",
TABLENAME, AGE, @"15" ,AGE, @"13"];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close];
}
// 删除数据:
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = '%@'",
TABLENAME, NAME, @"张三"];
BOOL res = [db executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
// 查询数据库:executeQuery
// [db executeQuery:@"查询sql语句"];
if ([db open]) {
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@",TABLENAME];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString * name = [rs stringForColumn:NAME];
NSString * age = [rs stringForColumn:AGE];
NSString * address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address);
}
[db close];
}
/*
系统方法:
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, [@"" UTF8String], -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [@"" UTF8String], -1, nil);
while (sqlite3_step(stmt) == SQLITE_ROW) {
// stmt 查询到的结果
sqlite3_column_text(stmt, 0);
1、打开关闭数据库
*/
// 1、打开关闭数据库
NSString *path = [NSHomeDirectory()stringByAppendingPathComponent:@"fmdb.sqlite"];
// 找到数据库的路径
FMDatabase *db = [FMDatabase databaseWithPath:path];
// 打开数据库
BOOL isOpen = [db open];//BOOL类型
/*
系统自带的打开数据库方式sqlite3_open(path, &db)返回一个int
*/
// 数据库关闭
// [db close];
// 系统:sqlite3_close(db)
/*
数据库增删改等操作:
除了查询操作,FMDB数据库操作都执行executeUpdate方法,这个方法返回BOOL类型
*/
[db executeUpdate:@"增删该sql语句"];
// 系统: sqlite3_exec(db, [@"增删改sql语句" UTF8String], NULL, NULL, &error)
NSString *TABLENAME = @"user";
NSString *ID = @"ID";
NSString *NAME = @"NAME";
NSString *AGE = @"AGE";
NSString *ADDRESS = @"ADDRESS";
// 创建表
if ([db open]) {
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
}
// 添加数据:
if ([db open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"张三",@"13",@"济南"];
BOOL res = [db executeUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"李四",@"12",@"济南"];
BOOL res2 = [db executeUpdate:insertSql2];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
if (!res2) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
// 修改数据:
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",
TABLENAME, AGE, @"15" ,AGE, @"13"];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close];
}
// 删除数据:
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = '%@'",
TABLENAME, NAME, @"张三"];
BOOL res = [db executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
// 查询数据库:executeQuery
// [db executeQuery:@"查询sql语句"];
if ([db open]) {
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@",TABLENAME];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString * name = [rs stringForColumn:NAME];
NSString * age = [rs stringForColumn:AGE];
NSString * address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address);
}
[db close];
}
/*
系统方法:
sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, [@"" UTF8String], -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [@"" UTF8String], -1, nil);
while (sqlite3_step(stmt) == SQLITE_ROW) {
// stmt 查询到的结果
sqlite3_column_text(stmt, 0);
}