iOS FMDB和简单的SQL语句运用
144 作者 iOS_愛OS 关注
2015.12.16 09:56 字数 753 阅读 3473评论 1喜欢 17
1.添加fmdb
点击这里下载fmdb
在项目中TARGETS->Build Phases ->Link Binary With Libraries 添加libsqlite3.tdb
导入fmdb库文件 添加依赖库 libsqlite3.tbd
把fmdb拖到自己的项目中 添加头文件 #import "DBManager.h"
TARGETS->Build Phases ->Compile Sources 中找到FMDatabaseQueue.m 和FMDatabasePool.m,改为非arc (-fno-objc-arc)
这下运行fmdb就不会报错了
2.SQL语句
我项目中使用的代码
//创建表格
-(void)initData
{
//1.获得数据库文件的路径
NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName=[doc stringByAppendingString:@"/Test.db"];
//2.获得数据库
// db=[FMDatabase databaseWithPath:fileName];
db = [[FMDatabase alloc]initWithPath:fileName];
//3.打开数据库
if ([db open]) {
//4.创表
NSString *str =[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS student (stuID integer primary key autoincrement, stuName text,stuNumber text,stuAge text,stuScore text)"];
if ([db executeUpdate:str]) {
NSLog(@"创表成功");
[db close];
}else
{
NSLog(@"创表失败");
}
}
}
//添加数据
-(void)insertData:(Model*)stuInt
{
if ([db open]) {
NSString *str = [NSString stringWithFormat:@"insert into student (stuName,stuNumber,stuAge,stuScore)values('%@','%@','%@','%@')",stuInt.stuName,stuInt.stuNumber,stuInt.stuAge,stuInt.stuScore];
if ([db executeUpdate:str]) {
NSLog(@"添加成功");
[db close];
}else{
NSLog(@"添加失败");
}
}
}
//删除数据
-(void)deleteData:(Model*)stuDele
{
if ([db open]) {
NSString *str = [NSString stringWithFormat:@"delete from student where stuID=%d",stuDele.stuID];
if ([db executeUpdate:str]) {
NSLog(@"删除成功");
[db close];
}else{
NSLog(@"删除失败");
}
}
}
//修改数据
-(void)upData:(Model*)stuUp
{
if ([db open]) {
NSString *str1 = [NSString stringWithFormat:@"update student set stuName = '%@' ,stuNumber = '%@' ,stuAge = '%@' ,stuScore = '%@' where stuID = '%d'",stuUp.stuName,stuUp.stuNumber,stuUp.stuAge,stuUp.stuScore,stuUp.stuID];
if ([db executeUpdate:str1]) {
NSLog(@"修改数据成功");
[db close];
}else{
NSLog(@"修改失败");
}
}
}
//查询数据
-(NSMutableArray*)quertData
{
NSMutableArray *arr = [NSMutableArray array];
if ([db open]) {
NSString *quert = [NSString stringWithFormat:@"select *from student"];
FMResultSet *set = [db executeQuery:quert];
while ([set next]) {
Model *mo = [[Model alloc]init];
mo.stuID = [set intForColumn:@"stuID"];
mo.stuName = [set stringForColumn:@"stuName"];
mo.stuNumber = [set stringForColumn:@"stuNumber"];
mo.stuAge = [set stringForColumn:@"stuAge"];
mo.stuScore = [set stringForColumn:@"stuScore"];
[arr addObject:mo];
}
NSLog(@"查询成功");
[db close];
}else{
NSLog(@"查询失败");
}
return arr;
}
浅宇
2楼 · 2017.03.22 17:15
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;这个方法必须自己实现吗?我看了一些自定义layout的 没有写这个方法也正常运行 疑问????
赞 回复
Felix灬泡泡
3楼 · 2017.04.06 16:45
你好,我用你的代码实现了想要的效果,但是我有几个疑问:
一、WaterFlowLayout文件实现了rowMarginInWaterflowLayout:等方法,但是没有看到设置minimumLineSpacing等属性值,请问,这是怎么达到效果的?
二、WaterFlowLayoutDelegate的几个代理方法中,设置WaterFlowLayout参数的意义是什么?
三、设置Header或Footer应该怎么设置?为什么我把该添加的都添加了,它还是不走collectionView: viewForSupplementaryElementOfKind: atIndexPath:方法?
四、-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.table];
}
return self;
}
-(UITableView*)table
{
if (!_table) {
_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
}
return _table;
}
新手初到,还望指点:blush:
赞 回复