IOS之?dāng)?shù)據(jù)庫(kù)的查找,刪除,添加,更新
來(lái)源:易賢網(wǎng) 閱讀:935 次 日期:2014-11-04 11:40:35
溫馨提示:易賢網(wǎng)小編為您整理了“IOS之?dāng)?shù)據(jù)庫(kù)的查找,刪除,添加,更新”,方便廣大網(wǎng)友查閱!

DB類(lèi)之.h文件

#import <Foundation/Foundation.h>

#import <sqlite3.h>

@interface DB : NSObject

+(sqlite3 *)openDB;//打開(kāi)數(shù)據(jù)庫(kù)

-(void)closeDB;//關(guān)閉數(shù)據(jù)庫(kù)

@end

DB類(lèi)之.m文件

#import "DB.h"

#import <sqlite3.h>

static sqlite3 *db = nil;

@implementation DB

+(sqlite3 *)openDB

{

if(db)

{

return db;

}

//目標(biāo)路徑

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES)objectAtIndex:0];

//原始路徑

NSString *filePath = [docPath stringByAppendingPathComponent:@"db.sqlite"];

NSFileManager *fm = [NSFileManager defaultManager];

if ([fm fileExistsAtPath:filePath] == NO)//如果doc下沒(méi)有數(shù)據(jù)庫(kù),從bundle里面拷貝過(guò)來(lái)

{

NSString *bundle = [[NSBundle mainBundle]pathForResource:@"classDB" ofType:@"sqlite"];

NSError *err = nil;

if ([fm copyItemAtPath:bundle toPath:filePath error:&err] == NO) //如果拷貝失敗

{

NSLog(@" localizedDescription]);

}

}

sqlite3_open([filePath UTF8String], &db);

return db;

}

-(void)closeDB

{

if (db)

{

sqlite3_close(db);

}

}

@end

Person類(lèi).h文件

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,retain)NSString *name,*phone;

@property(nonatomic,assign)int age,ID;

-(id)initWithName:(NSString *)name phone:(NSString *)phone age:(int)age ID:(int)ID;

+(NSMutableArray *)findAll;

+(int)count;

+(Person *)findByID:(int)ID;

+(NSMutableArray *)findByname:(NSString *)name;

+(void)addName:(NSString *)name phone:(NSString *)phone age:(int)age;

+(void)deleteByID:(int)ID;

+(void)updataName:(NSString *)name phone:(NSString *)phone age:(int)age forID:(int)ID;

@end

Person類(lèi).m文件

#import "Person.h"

#import "DB.h"

@implementation Person

@synthesize name,ID,phone,age;

-(id)initWithName:(NSString *)aName phone:(NSString *)aPhone age:(int)aAge ID:(int)aID

{

[super init];

if (self)

{

self.name = aName;

self.phone = aPhone;

self.age = aAge;

self.ID = aID;

}

return self;

}

-(NSString *)description

{

return [NSString stringWithFormat:@"id = %d name = %@ phone = %@ age = %d",self.ID,self.name,self.phone,self.age ];

}

+(NSMutableArray *)findAll

{

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;//創(chuàng)建一個(gè)聲明對(duì)象

int result = sqlite3_prepare_v2(db, "select * from classDB order by ID ", -1, &stmt, nil);

NSMutableArray *persons = nil;

if (result == SQLITE_OK)

{

persons = [[NSMutableArray alloc]init];

while (sqlite3_step(stmt) == SQLITE_ROW)

{

int ID = sqlite3_column_int(stmt, 0);

const unsigned char *name = sqlite3_column_text(stmt, 1);

const unsigned char *phone = sqlite3_column_text(stmt, 2);

int age = sqlite3_column_int(stmt, 3);

Person *p = [[Person alloc]initWithName:[NSString stringWithUTF8String:(const char *)name] phone:[NSString stringWithUTF8String:(const char *)phone] age:age ID:ID];

[persons addObject:p];

[p release];

}

}

else

{

persons = [[NSMutableArray alloc]init];

}

sqlite3_finalize(stmt);

return [persons autorelease];

}

+(int)count

{

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare_v2(db, "select count(ID) from classDB", -1, &stmt, nil);

if (result == SQLITE_OK)

{

int count = 0;

if (sqlite3_step(stmt))

{

count = sqlite3_column_int(stmt, 0);

}

sqlite3_finalize(stmt);

return count;

}

else

{

sqlite3_finalize(stmt);

return 0;

}

}

+(Person *)findByID:(int)ID

{

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

Person *p = nil;

int result = sqlite3_prepare_v2(db, "select * from classDB where ID = ?", -1, &stmt, nil);

if (result == SQLITE_OK)

{

sqlite3_bind_int(stmt, 1, ID);

if (sqlite3_step(stmt))

{

int ID = sqlite3_column_int(stmt, 0);

const unsigned char *name = sqlite3_column_text(stmt, 1);

const unsigned char *phone = sqlite3_column_text(stmt, 2);

int age = sqlite3_column_int(stmt, 3);

p = [[Person alloc]initWithName:[NSString stringWithUTF8String:(const char *)name] phone:[NSString stringWithUTF8String:(const char *)phone] age:age ID:ID];

}

}

sqlite3_finalize(stmt);

return [p autorelease];

}

+(NSMutableArray *)findByname:(NSString *)name

{

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare(db, "select * from classDB where name = ?", -1, &stmt, nil);

NSMutableArray *persons = nil;

if (result == SQLITE_OK)

{

sqlite3_bind_text(stmt, 1, [name UTF8String], -1, nil);

persons = [[NSMutableArray alloc]init];

while (sqlite3_step(stmt) == SQLITE_ROW)

{

int ID = sqlite3_column_int(stmt, 0);

const unsigned char *name = sqlite3_column_text(stmt, 1);

const unsigned char *phone = sqlite3_column_text(stmt, 2);

int age = sqlite3_column_int(stmt, 3);

Person *p = [[Person alloc]initWithName:[NSString stringWithUTF8String:(const char *)name] phone:[NSString stringWithUTF8String:(const char *)phone] age:age ID:ID];

[persons addObject:p];

[p release];

}

}

else

{

persons = [[NSMutableArray alloc]init];

}

sqlite3_finalize(stmt);

return [persons autorelease];

}

//添加元素

+(void)addName:(NSString *)name phone:(NSString *)phone age:(int)age

{

NSString *str = [NSString stringWithFormat:@"insert into classDB(name,phone,age) values(];

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare_v2(db, [str UTF8String],-1 ,&stmt , nil);

if (result == SQLITE_OK)

{

sqlite3_step(stmt);

}

sqlite3_finalize(stmt);

}

//根據(jù)ID刪除信息

+(void)deleteByID:(int)ID

{

NSString *str = [NSString stringWithFormat:@"delete from classDB where ID = %d",ID];

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);

if (result == SQLITE_OK)

{

sqlite3_step(stmt);

}

sqlite3_finalize(stmt);

}

//更新

+(void)updataName:(NSString *)name phone:(NSString *)phone age:(int)age forID:(int)ID

{

NSString *str = [NSString stringWithFormat:@"update classDB set name = = %d where ID = %d",name,phone,age,ID];

sqlite3 *db = [DB openDB];

sqlite3_stmt *stmt = nil;

int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);

if (result == SQLITE_OK)

{

sqlite3_step(stmt);

}

sqlite3_finalize(stmt);

}

@end

更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

更多信息請(qǐng)查看技術(shù)文章
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢(xún)回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢(xún)?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢(xún) | 簡(jiǎn)要咨詢(xún)須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢(xún)關(guān)注公眾號(hào):hfpxwx
咨詢(xún)QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)