Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在ios中从sqlite数据库表中获取数据?_Iphone_Ios_Objective C_Xcode_Sqlite - Fatal编程技术网

Iphone 如何在ios中从sqlite数据库表中获取数据?

Iphone 如何在ios中从sqlite数据库表中获取数据?,iphone,ios,objective-c,xcode,sqlite,Iphone,Ios,Objective C,Xcode,Sqlite,当我在文本字段中输入星的名称时,我想从数据库表中获取数据,它会给我该星随机名称的所有引号 -(NSMutableArray *)searchAllQuotesOfStar { NSString *starName = [[NSString alloc] init]; // NSString *name = [NSString stringWithFormat:@"starName"]; NSMutableArray *starQuoteArray = [[NSM

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

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

将此代码添加到数据库FlipSideViewController //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);
}

首先从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;
}
试试这个。

我希望这真的很有帮助。

当你尝试这样做时发生了什么?它给了我数据,但只有一个名称,我想给我在文本字段中输入的名称的数据。这一个没有解决我的问题,但你会了解如何从sqlite获取数据并将其存储到tableview。我想添加相应的word id的数据,但我不在另一个视图中获取它。