Kết nối cơ sở dữ liệu với SQLite
A - Tạo kết nối đến file database sqlite
B1 : Add File .sqlite vào project
B2: Copy file B1 vào thư mục build của iOS Simulator
- Mở file default .m, hàm viewDidLoad
PHP Code:
NSString *databaseName = @"thegioididong.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success=[fileManager fileExistsAtPath:databasePath];
if (!success) {
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
A - SELECT: Lấy dữ liệu từ datbase
PHP Code:
sqlite3 *contactDB;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK){
sqlite3_stmt *statement;
NSString *sql = @"SELECT * FROM dienthoai";
const char *query_stmt = [sql UTF8String];
if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK){
while(sqlite3_step(statement)==SQLITE_ROW){
NSString *ten = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
[danhsach addObject:ten];
}
sqlite3_finalize(statement);
}
}else{
NSLog(@"Có lỗi xảy ra");
}
PHP Code:
sqlite3 *contactDB;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK){
sqlite3_stmt *statement;
NSString *sql = [NSString stringWithFormat:@"INSERT INTO dienthoai VALUES(null, '%@','%@')",hoten, sodt];
const char *query_stmt = [sql UTF8String];
if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK){
sqlite3_step(statement);
sqlite3_finalize(statement);
}
}else{
NSLog(@"iiiii");
}
A - Tạo kết nối đến file database sqlite
B1 : Add File .sqlite vào project
B2: Copy file B1 vào thư mục build của iOS Simulator
- Mở file default .m, hàm viewDidLoad
PHP Code:
NSString *databaseName = @"thegioididong.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success=[fileManager fileExistsAtPath:databasePath];
if (!success) {
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
A - SELECT: Lấy dữ liệu từ datbase
PHP Code:
sqlite3 *contactDB;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK){
sqlite3_stmt *statement;
NSString *sql = @"SELECT * FROM dienthoai";
const char *query_stmt = [sql UTF8String];
if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK){
while(sqlite3_step(statement)==SQLITE_ROW){
NSString *ten = [[NSString alloc] initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
[danhsach addObject:ten];
}
sqlite3_finalize(statement);
}
}else{
NSLog(@"Có lỗi xảy ra");
}
PHP Code:
sqlite3 *contactDB;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &contactDB)==SQLITE_OK){
sqlite3_stmt *statement;
NSString *sql = [NSString stringWithFormat:@"INSERT INTO dienthoai VALUES(null, '%@','%@')",hoten, sodt];
const char *query_stmt = [sql UTF8String];
if(sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL)==SQLITE_OK){
sqlite3_step(statement);
sqlite3_finalize(statement);
}
}else{
NSLog(@"iiiii");
}
No comments:
Post a Comment