xcode 如何从ios中的sqlite数据库表中获取数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16413089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:20:58  来源:igfitidea点击:

how to fetch data from sqlite database table in ios?

iphoneiosobjective-cxcodesqlite

提问by HappyCoding

i want to fetch data from my database table when i enter the name of the star in textfield the it give me all the quotes of that star for random names of star.

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

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

回答by NMALKO

Add this code to databaseFlipsideViewController//databaseFlipsideViewController.m

将此代码添加到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);
    }
}

And my detailViewController code is this:

我的 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);
}

回答by Jitendra

First Get Data From Sqlite Like this Below Code.

首先像下面的代码一样从 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);
}

And Stored it into Tableview like this

并像这样将其存储到 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;
}

Try this.
I hope this is really helpful.

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