-3

当我在文本字段中输入星的名称时,我想从我的数据库表中获取数据,它为我提供了该星的所有引号以获取星的随机名称。

-(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;
}
4

2 回答 2

1

首先像下面的代码一样从 Sqlite 获取数据。

-(void)getdataMT
{
    sqlite3 * database;

    NSString *databasename=@"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);
}

并像这样将其存储到 Tableview 中

- (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;
}

尝试这个。
我希望这真的很有帮助。

于 2013-05-07T07:17:28.673 回答
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);
}
于 2013-05-07T07:06:16.047 回答