2013-05-07 239 views
-3

我想從我的數據庫表中獲取數據,當我在文本字段中輸入星號的名稱時,它會爲我提供星號的隨機名稱的所有引號。如何從ios中的sqlite數據庫表中獲取數據?

-(NSMutableArray *)searchAllQuotesOfStar 
{ 
    NSString *starName = [[NSString alloc] init]; 

    // NSString *name = [NSString stringWithFormat:@"starName"]; 

    NSMutableArray *starQuoteArray = [[NSMutableArray alloc] init]; 

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT quote FROM quotes where star like '%%starName%%'"]; 

    //SELECT count(quote) from quotes where star like '%ac%' 

    sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement:sqlStr]; 

    while (sqlite3_step(ReturnStatement)==SQLITE_ROW) 
    { 
     @try 
     { 
      searchStarQuoteDC *searchQoute = [[searchStarQuoteDC alloc] init]; 

      NSString *quote = [NSString stringWithUTF8String:(char*)sqlite3_column_text(ReturnStatement, 0)]; 

      searchQoute.quote = quote; 

      //    NSLog(@"%@", quote); 

      [starQuoteArray addObject:searchQoute]; 
     } 
     @catch (NSException *ept) { 

      NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]); 
     } 
    } 

    return starQuoteArray; 
} 
+1

當你這樣嘗試時發生了什麼? – Balu 2013-05-07 07:03:44

+0

它給我的數據,但只有一個名字,我想給我的名字,我搜索該名稱的數據輸入文本域 – HappyCoding 2013-05-07 07:06:13

回答

1

將此代碼添加到databaseFlipsideViewController //databaseFlipsideViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"showDetail"]) { 

     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     detailController = segue.destinationViewController; 
     detailController.customerName = [keys objectAtIndex:indexPath.row]; 

     NSLog(@"%@", detailController.customerName); 
    } 
} 

而我detailViewController代碼是這樣的:

@implementation detailViewController 

@synthesize customerLabel,code1Label,code2Label,dateLabel,customerName,code1Name; 

//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 opened"); 
    } 
} 

- (IBAction)back:(id)sender 
{ 
    [self.delegate detailViewControllerDidFinish:self]; 
} 

- (void)viewDidLoad 
{ 
    [self openDB]; 

    NSString*sql = [NSString stringWithFormat:@"SELECT theDate, customer,code1,code2 FROM summary WHERE key=\"%@\"", customerName]; 

    const char *query_stmt = [sql UTF8String]; 

    sqlite3_stmt*statement; 

    if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, nil)==SQLITE_OK) { 

     if (sqlite3_step(statement)==SQLITE_ROW) { 

      NSString *date = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)]; 
      dateLabel.text = date; 

      NSString *customer = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 
      customerLabel.text = customer; 

      NSString *code1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 2)]; 
      code1Label.text = code1; 

      NSString *code2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,3)]; 
      code2Label.text = code2; 

     } 
     sqlite3_finalize(statement); 

     [super viewDidLoad]; 
    } 
    sqlite3_close(db); 
} 
+0

仍然我有問題 – HappyCoding 2013-05-07 07:25:01

+0

我想添加相應的數據的單詞ID,但我沒有得到它在另一種觀點。 – Asheesh 2015-09-16 06:19:53

1

首先獲取數據從sqlite的篩選代碼如下。

-(void)getdataMT 
{ 
    sqlite3 * database; 

    NSString *[email protected]"MathsTricks.sqlite"; // Your database Name. 

    NSArray * documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); 

    NSString * DocDir=[documentpath objectAtIndex:0]; 

    NSString * databasepath=[DocDir stringByAppendingPathComponent:databasename]; 

    if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK) 
    { 
     const char *sqlStatement = "SELECT * FROM Mathematicstricks"; // Your Tablename 

     sqlite3_stmt *compiledStatement; 

     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
      [name removeAllObjects]; 

      while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
      { 
       [name addObject:[NSString stringWithFormat:@"%s",(char *) sqlite3_column_text(compiledStatement, 0)]]; 
      } 
     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
} 

它存儲到泰伯維 這樣

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return name.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text=[NSString stringWithFormat:@"%@",[name objectAtIndex:indexPath.row]]; 

    return cell; 
} 

試試這個。
我希望這真的很有幫助。

+0

這一個沒有解決我的問題 – HappyCoding 2013-05-07 07:20:04

+0

,但你會明白如何從sqlite中獲取數據並將其存儲到tableview中。 – Jitendra 2013-05-07 07:49:39

相關問題