2013年3月13日 星期三

Sqlite Sample(記得加入檔案要勾選Target)

把data.sqlite時,選Target
[NSBundle mainBundle]

//NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject ]; //用lastObjest
NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *file=[path[0] stringByAppendingPathComponent:@"data.sqlite"];
if ([[NSFileManager defaultManager]fileExistsAtPath:file]==FALSE){
NSString* fromFile = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"sqlite"];
[[NSFileManager defaultManager] copyItemAtPath:fromFile toPath:file error:nil];
}else{
NSLog(@"db exist");
}
if (sqlite3_open([file UTF8String], &database)!=SQLITE_OK) {
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}else{
NSLog(@"DB Opened");
}
NSString* Sql=@"SELECT * FROM Info";
const char *selectSql=[Sql UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, selectSql, -1, &statement, nil)==SQLITE_OK) { NSLog(@"select ok.");
while (sqlite3_step(statement)==SQLITE_ROW) {
int _id=sqlite3_column_int(statement, 0);
char *name=(char *)sqlite3_column_text(statement, 1);
NSLog(@"row>>id %i, name %s",_id,name);
}
}else{
NSLog(@"dont match");
}
sqlite3_finalize(statement);
sqlite3_close(database);

沒有留言:

張貼留言