iOS FMDB和簡單的SQL語句運用
144? 作者 iOS_愛OS 關(guān)注
2015.12.16 09:56 字?jǐn)?shù) 753 閱讀 3473評論 1喜歡 17
1.添加fmdb
點擊這里下載fmdb
在項目中TARGETS->Build Phases ->Link Binary With Libraries 添加libsqlite3.tdb
導(dǎo)入fmdb庫文件 添加依賴庫 libsqlite3.tbd
把fmdb拖到自己的項目中 添加頭文件 #import "DBManager.h"
TARGETS->Build Phases ->Compile Sources 中找到FMDatabaseQueue.m 和FMDatabasePool.m,改為非arc (-fno-objc-arc)
這下運行fmdb就不會報錯了
2.SQL語句
我項目中使用的代碼
//創(chuàng)建表格
-(void)initData
{
//1.獲得數(shù)據(jù)庫文件的路徑
NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName=[doc stringByAppendingString:@"/Test.db"];
//2.獲得數(shù)據(jù)庫
//? ? ? ? db=[FMDatabase databaseWithPath:fileName];
db = [[FMDatabase alloc]initWithPath:fileName];
//3.打開數(shù)據(jù)庫
if ([db open]) {
//4.創(chuàng)表
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(@"創(chuàng)表成功");
[db close];
}else
{
NSLog(@"創(chuàng)表失敗");
}
}
}
//添加數(shù)據(jù)
-(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(@"添加失敗");
}
}
}
//刪除數(shù)據(jù)
-(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(@"刪除失敗");
}
}
}
//修改數(shù)據(jù)
-(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(@"修改數(shù)據(jù)成功");
[db close];
}else{
NSLog(@"修改失敗");
}
}
}
//查詢數(shù)據(jù)
-(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;這個方法必須自己實現(xiàn)嗎?我看了一些自定義layout的 沒有寫這個方法也正常運行 疑問棕洋??秘症??
贊? 回復(fù)
Felix灬泡泡
3樓 · 2017.04.06 16:45
你好,我用你的代碼實現(xiàn)了想要的效果服球,但是我有幾個疑問:
一献联、WaterFlowLayout文件實現(xiàn)了rowMarginInWaterflowLayout:等方法竖配,但是沒有看到設(shè)置minimumLineSpacing等屬性值,請問里逆,這是怎么達到效果的进胯?
二、WaterFlowLayoutDelegate的幾個代理方法中原押,設(shè)置WaterFlowLayout參數(shù)的意義是什么胁镐?
三、設(shè)置Header或Footer應(yīng)該怎么設(shè)置?為什么我把該添加的都添加了希停,它還是不走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:
贊? 回復(fù)