我正在建立一個數據庫,我想從數據庫中保存的對象檢索特定的數據。從sqLite數據庫檢索數據ios iphone應用
這是我的代碼有:
//databaseMainViewController.m
//Create and database and save data
@implementation databaseMainViewController
@synthesize customer, code1,code2;
-(void) createTable: (NSString*)tableName
withField1: (NSString*)field1
withField2: (NSString*)field2
withField3: (NSString*)field3
withField4: (NSString*)field4; {
char*err;
NSString*sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@'"
"TEXT PRIMARY KEY, '%@'TEXT, '%@'TEXT, '%@'TEXT);", tableName, field1, field2, field3, field4];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"DCould not create table");
}
else {
NSLog(@"table created");
}
}
//file path to database
-(NSString*)filePath {
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}
//open database
-(void)openDB {
if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Databese failed to open");
}
else {
NSLog(@"database opemed");
}
}
- (IBAction)save:(id)sender {
NSString*customers = customer.text;
NSString*codeOne = code1.text;
NSString*codeTwo = code2.text;
NSDate*theDate = [NSDate date];
NSString*sql= [NSString stringWithFormat:@"INSERT INTO summary ('theDate','customer','code1','code2')VALUES ('%@','%@','%@','%@')", theDate, customers,codeOne, codeTwo];
char*err;
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Could not update table");
}
else {
NSLog(@"table updated");
}
customer.text = @"";
code1.text = @"";
code2.text = @"";
}
- (void)viewDidLoad
{
[self openDB];
[self createTable:@"summary" withField1:@"theDate" withField2:@"customer" withField3:@"code1" withField4:@"code2"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
然後,我有一些代碼來獲取數據,並把它們放在一個主列表
#import "databaseFlipsideViewController.h"
#import "detailViewController.h"
@interface databaseFlipsideViewController()
@end
@implementation databaseFlipsideViewController
@synthesize entries;
//file path to database
-(NSString*)filePath {
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}
//open database
-(void)openDB {
if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Databese failed to open");
}
else {
NSLog(@"database opemed");
}
}
- (void)viewDidLoad
{
entries = [[NSMutableArray alloc]init];
[self openDB];
NSString*sql = [NSString stringWithFormat:@"SELECT * FROM summary"];
sqlite3_stmt*statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
char*field1 = (char*)sqlite3_column_text(statement, 0);
NSString*field1Str = [[NSString alloc]initWithUTF8String:field1];
char*field2 = (char*)sqlite3_column_text(statement, 1);
NSString*field2Str = [[NSString alloc]initWithUTF8String:field2];
char*field3 = (char*)sqlite3_column_text(statement, 2);
NSString*field3Str = [[NSString alloc]initWithUTF8String:field3];
char*field4 = (char*)sqlite3_column_text(statement, 3);
NSString*field4Str = [[NSString alloc]initWithUTF8String:field4];
NSString*str = [[NSString alloc]initWithFormat:@"%@", field2Str ];
[entries addObject:str];
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
//Return the number of sections
return 1;
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
NSString*myTitle = [[NSString alloc]initWithFormat:@"History"];
return myTitle;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return [entries count];
NSLog(@"%d",[entries count]);
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"test");
static NSString*cellIdentifier = @"Cell";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self.entries objectAtIndex:indexPath.row];
return cell;
}
並在此之後我有一個問題,因爲當我點擊列表中的其中一個名稱,應用程序會轉到另一個視圖,並顯示名稱的詳細信息。但每個名字的細節都是一樣的。
這是detailViewController代碼:
#import "detailViewController.h"
@interface detailViewController()
@end
@implementation detailViewController
@synthesize customerLabel,code1Label,code2Label,dateLabel;
//file path to database
-(NSString*)filePath {
NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}
//open database
-(void)openDB {
if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Databese failed to open");
}
else {
NSLog(@"database opemed");
}
}
- (IBAction)back:(id)sender
{
[self.delegate detailViewControllerDidFinish:self];
}
- (void)viewDidLoad
{
[self openDB];
NSString*sql = [NSString stringWithFormat:@"SELECT * FROM summary"];
sqlite3_stmt*statement;
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
char*field1 = (char*)sqlite3_column_text(statement, 0);
NSString*field1Str = [[NSString alloc]initWithUTF8String:field1];
char*field2 = (char*)sqlite3_column_text(statement, 1);
NSString*field2Str = [[NSString alloc]initWithUTF8String:field2];
char*field3 = (char*)sqlite3_column_text(statement, 2);
NSString*field3Str = [[NSString alloc]initWithUTF8String:field3];
char*field4 = (char*)sqlite3_column_text(statement, 3);
NSString*field4Str = [[NSString alloc]initWithUTF8String:field4];
customerLabel.text = field2Str;
code1Label.text = field3Str;
code2Label.text =field4Str;
dateLabel.text = field1Str;
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
那麼,什麼是在細節畫面顯示從所選對象的詳細信息的最佳途徑
您在代碼中缺少很多概念..「首先,最好從數據庫中檢索線程中的數據」將數據庫連接分離爲獨立的類。最後一件事是你想如何獲得一個視圖的詳細信息和查詢「SELECT * FROM summary」,就像你需要在什麼條件下獲得確切的記錄詳細信息一樣。 –
您正在打開數據庫,但代碼要關閉在哪裏數據庫? – Rajneesh071