FMDB用法集合
1、//缓存路径
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [file stringByAppendingPathComponent:@"student6677.sqlite"];
2、创建数据库
BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS contacts4(id integer PRIMARY KEY AUTOINCREMENT,name text NOT NULL);"];
3、添加数据
if (result) {
for (int i = 0; i < self.contacts.count; i++) {
[db executeUpdate:[NSString stringWithFormat:@"INSERT INTO contacts4 (name) VALUES (%@)", self.contacts[i]]];
}
}
4、删除数据
[db executeUpdate:@"DELETE FROM CONTACTS WHERE NAME = 'JING' ;"];
5、更新数据
[db executeUpdate:@"UPTATE TABLE CONTACTS4 SET NAME = 'QING' WHIERE NAME = 'JING';"];
6、查找数据
@1、根据条件查找
[db executeQuery:@"SELECT *FROM CONTACTS4 WHERE NAME = 'QING';"];
@2、查找全部数据
[db executeQuery:@"SELECT *FROM CONTACTS4"];
-
FMResultSet
获取不同数据格式的方法- intForColumn:
- longForColumn:
- longLongIntForColumn:
- boolForColumn:
- doubleForColumn:
- stringForColumn:
- dataForColumn:
- dataNoCopyForColumn:
- UTF8StringForColumnIndex:
- objectForColumn:
//遍历结果集合
while ([resultSet next])
{
int idNum = [resultSet intForColumn:@“id”];
NSString *name = [resultSet
objectForColumn:@“name”];
int age = [resultSet intForColumn:@“age”];
}
7、添加表字段
[db executeUpdate:@"ALTER TABLE contacts4 ADD COLUMN age text "];
8、删除表
[db executeUpdate:@"DROP TABLE CONTACTS;"];
或
[db executeUpdate:@"DROP TABLE IF EXISTS CONTACTS4;"];