這段時(shí)間公司在做一個(gè)新的應(yīng)用,用到了數(shù)據(jù)庫。在網(wǎng)上找了一些資料,最后決定用fmdb來做。主要是用起來比較簡單,適合剛接觸數(shù)據(jù)庫這方面的人。 一、步 首先是創(chuàng)建數(shù)據(jù)庫: (NSString*)filePath:(NSString *)fileName { NSString *path=NSHomeDirectory(); /
這段時(shí)間公司在做一個(gè)新的應(yīng)用,用到了數(shù)據(jù)庫。在網(wǎng)上找了一些資料,最后決定用fmdb來做。主要是用起來比較簡單,適合剛接觸數(shù)據(jù)庫這方面的人。
一、步 首先是創(chuàng)建數(shù)據(jù)庫:
+(NSString*)filePath:(NSString *)fileName
{
NSString *path=NSHomeDirectory();
//拼接路徑Library/Caches
path=[path stringByAppendingPathComponent:@"Library/Caches"];
NSLog(@"path=%@",path);
NSFileManager *fm=[NSFileManager defaultManager];
//檢查指定的緩存目錄是否存在
if ([fm fileExistsAtPath:path]) {
//檢查要保存的文件名是否合法
if (fileName&& [fileName length]!=0) {
//拼接全路徑
path=[path stringByAppendingPathComponent:fileName];
}
}
else{
NSLog(@"緩存目錄不存在");
}
return path;
}
-(id)init
{
DataItem *item = [[DataItem alloc] init];
NSLog(@"090909090==%@",item.destination);
if (self=[super init]) {
//實(shí)例化第三方數(shù)據(jù)庫操作類對象
//如果user.db文件不存在就創(chuàng)建新的
//存在就直接使用
fmdb=[[FMDatabase databaseWithPath:[Database filePath:@"user.db"]] retain];
//嘗試打開數(shù)據(jù)庫
if ([fmdb open]) {
//創(chuàng)建數(shù)據(jù)表
[self createTable];
}
}
return self;
}
二、 創(chuàng)建數(shù)據(jù)庫表 需要建多個(gè)表所以用一個(gè)數(shù)組來保存建表語句,然后遍歷數(shù)組執(zhí)行建表語句
NSArray *reportinfoArray = [NSArray arrayWithObjects:@"CREATE TABLE IF NOT EXISTS report_info (report_id integer Primary Key Autoincrement,report_name Varchar(1024) DEFAULT NULL,user_id integer DEFAULT NULL“,nil];
以report_id為自增長的主建 在建表語句中將其聲明為主建,在表的插入過程中 repord_id回從1開始自動(dòng)增長不需要對其進(jìn)行賦值。
for (NSString *sql in reportinfoArray) {
//執(zhí)行sql語句
//創(chuàng)建表,增,刪,改都用這個(gè)方法
if ([fmdb executeUpdate:sql]) {
NSLog(@"已創(chuàng)建");
}
else{
NSLog(@"創(chuàng)建表失敗:%@",[fmdb lastErrorMessage]);;
}
}
三,插入 可一條一條插入也可批量插入
-(void)insertItem:(DataItem *)item
{
if ([self existsItem:item]) {
return;
}else{
NSString *sql=[NSString stringWithFormat:@"insert into report_info (report_name,user_id,any_invoice,allowance,advance_payment,report_aim,report_starttime,report_endtime,destination) values (?,?,?,?,?,?,?,?,?)"];
//變參方法,每個(gè)?號(hào)代表一個(gè)字段值,所有參數(shù)必須為對象類類型
if (sql) {
[fmdb executeUpdate:sql,item.report_name,item.user_id,item.any_invoice,item.allowance,item.advance_payment,item.report_aim,item.report_starttime,item.report_endtime,item.destination];
//獲取最后一個(gè)插入的數(shù)據(jù)的主鍵
markID = [fmdb lastInsertRowId];
}
else{
NSLog(@"插入失敗 :%@",[fmdb lastErrorMessage]);
}
}
}
批量插入
-(void)insertStayArray:(NSArray *)array
{
//開始批量操作
[fmdb beginTransaction];
for (DataItem *item in array) {
[self insertStayItem:item];
}
//提交所有修改
[fmdb commit];
}
注釋:在插入過程中遇到了一個(gè)問題 再插入nsinteger 類型的數(shù)據(jù)的時(shí)候 程序會(huì)崩潰 后來的解決辦法是[NSString stringWithFormat:@"%d",item.report_id] 將其轉(zhuǎn)化為nsstring 類
型在進(jìn)行插入。
四、刪除相對簡單 只需要根據(jù)其主鍵后按照某些特定條件進(jìn)行刪除
-(void)deletestay_info:(int)stay_id
{
NSString*delete=[NSString stringWithFormat:@"DELETE FROM stay_info WHERE stay_id = %d",stay_id];
BOOL a=[fmdb executeUpdate:delete];
if (a) {
NSLog(@"%d,刪除成功!",stay_id);
}
}
五、查找 也是根據(jù)某些特定的條件進(jìn)行查找
-(DataItem*)getStay_info:(int)stay_id
{
NSString *sql=[NSString stringWithFormat:@"select * from stay_info where stay_id=%d",stay_id];
//執(zhí)行查詢
FMResultSet *rs=[fmdb executeQuery:sql];
//如果有記錄
DataItem *item=[[[DataItem alloc] init] autorelease];
while ([rs next]) {
//此方法是一組方法
//根據(jù)字段類型選擇不同方法
item.stay_id = [rs longForColumn:@"stay_id"];
item.stay_endtime = [rs stringForColumn:@"stay_endtime"];
item.stay_company_pay = [rs stringForColumn:@"stay_company_pay"];
}
return item;
}
六、修改 根據(jù)要修改的內(nèi)容判斷滿足怎樣的條件需要修改
-(void)alerttraffic_info:(DataItem *)item
{
NSLog(@"traffic_destination = %@",item.traffic_destination);
NSLog(@"traffic_id=%d",item.traffic_id);
NSString*Name=[NSString stringWithFormat:@"UPDATE traffic_info SET traffic_kind = '%@' WHERE traffic_id = %d",item.traffic_kind,item.traffic_id];
[fmdb executeUpdate:Name];
NSString*trafficDate=[NSString stringWithFormat:@"UPDATE traffic_info SET traffic_date = '%@' WHERE traffic_id = %d",item.traffic_date,item.traffic_id];
[fmdb executeUpdate:trafficDate];
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com