Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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
如何将SQLite数据库中的查询加载到ios 7(xcode 5.1.1)中的tableview中?_Ios_Sql_Database_Xcode_Sqlite - Fatal编程技术网

如何将SQLite数据库中的查询加载到ios 7(xcode 5.1.1)中的tableview中?

如何将SQLite数据库中的查询加载到ios 7(xcode 5.1.1)中的tableview中?,ios,sql,database,xcode,sqlite,Ios,Sql,Database,Xcode,Sqlite,我有一个名为BookDataBase.SQLite的SQLite数据库,它有两列:Category、Name 它包含书籍的类别和每个类别的一些书籍名称 我有一个表视图控制器连接到类CategoryTableViewController,它是UITableViewController的子类 我希望表视图显示数据库中仅显示类别的查询结果 我已经尝试过这个代码,但它对我不起作用 CategoryTableViewController.m: #import "CategoryTableViewContr

我有一个名为BookDataBase.SQLite的SQLite数据库,它有两列:Category、Name

它包含书籍的类别和每个类别的一些书籍名称

我有一个表视图控制器连接到类CategoryTableViewController,它是UITableViewController的子类

我希望表视图显示数据库中仅显示类别的查询结果

我已经尝试过这个代码,但它对我不起作用

CategoryTableViewController.m:

#import "CategoryTableViewController.h"
#import <sqlite3.h>

@interface CategoryTableViewController ()

@end

sqlite3 *BookDatabase;
NSString *databasepath;
const char *filepath;
NSString *querySQL;
const char *query_stmt;
sqlite3_stmt *statement;
NSMutableDictionary *BookRecord;
NSUserDefaults *BookData;
NSMutableArray *BookArray;


@implementation CategoryTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) viewWillAppear:(BOOL)animated {

    databasepath = [[NSBundle mainBundle]pathForResource:@"BookDatabase" ofType:@"sqlite"];
    filepath = [databasepath UTF8String];

    if (sqlite3_open(filepath, &BookDatabase) == SQLITE_OK) {


        querySQL = @"SELECT Category FROM BookTable Group By Categoy" ;
        query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(BookDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {

                BookRecord =[[NSMutableDictionary alloc] initWithCapacity:sqlite3_column_count(statement)];

                for (int i=0; i<sqlite3_column_count(statement); i++) {

                    NSString *ColName = [[NSString alloc] initWithUTF8String:sqlite3_column_name(statement, i)];
                    NSString *ColVal = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_value(statement, i)];




                    [BookRecord setObject:ColVal forKey:ColName];
                }


                [BookArray addObject:BookRecord];



            }


             sqlite3_finalize(statement);

        }

        sqlite3_close(BookDatabase);





    }





}




- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [BookArray count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CategoryTableViewCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];





    // Configure the cell...

    int row = [indexPath row];



    cell.textLabel.text = [BookArray[row] objectForKey:@"Category"];




    return cell;
}

@end
我已经添加了libsqlite3框架

数据库文件位于应用程序文件夹中:

表视图原型单元的类型设置为basic,单元标识符为CategoryTableViewCell

数据库中表的名称为BookTable

有人能帮我修一下吗

如果需要,请随时重写我的全部代码。<但请记住,它是UITableViewController的子类

谢谢。我终于找到了

原来我没有将数据库文件添加到主捆绑包中


我不知道我应该这么做。

你写的它不起作用-它到底是怎么不起作用的?它是从数据库读取数据还是有错误消息?@AlexAtNet tableview是空的,没有任何内容。你能在[BookRecord setObject:ColVal forKey:ColName]上设置断点吗;并检查数据是否真的添加到了tableview?那么,您做了什么来调试它呢?你知道数据库是否被成功读取了吗?你知道数据库文件是否存在吗?@AlexAtNet我设置了一个断点,但什么也没有出现。