无法从SQLite DB获取数据以在iOS应用程序中显示

时间:2014-03-13 14:50:25

标签: ios sql sqlite

所以我有一个条形码扫描应用程序,它应该读取条形码,与数据库中的条目匹配,并显示有关条目的所有其他信息。

我正在使用名为trackeddb.sqlite的单个文件数据库,它是使用terminal和sqlite3命令创建的。它包含两个表,一个用于产品的静态信息(每个部件号是唯一的,并且有自己的条目)用于填写“第一次扫描”条目,第二个表用于存放相同的产品信息,还有条形码。第二个表允许用户存储具有相同部件号和规格的多个产品,但使用条形码创建唯一条目。

我的问题是,当条形码扫描时,它应该显示存储在第二个表中的信息(如果条形码匹配)。代码是合理的,但是当我运行条形码时(在第二个表中输入一个条目后),它不会显示预设文本以外的数据。我已经把我的大脑绞尽脑汁而我无法理解,但我认为这可能与我引用我的数据库的方式有关。

谢谢你的时间!

What I am greeted with when I scan the barcodes

这是sqlite DB

PasteBin link to sqlite db and schema

从此方法

调用数据库
- (id)init {
    if ((self = [super init])) {
        NSString* sqLiteDb = [[NSBundle mainBundle] pathForResource:@"trackeddb" ofType:@"sqlite"];
        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database");
        }
    }
    return self;
}

以下是我的代码的主要部分,条形码扫描仪工作正常,所以我不会打扰那个位。这仅适用于数据库。

TrackerDatabase.m

//TrackerDatabase.m    
#import "TrackerDatabase.h"
#import "TrackedItems.h"
#import "Barcode.h"

@interface TrackerDatabase()

@end

@implementation TrackerDatabase

static TrackerDatabase *_database;

+ (TrackerDatabase*)database {
    if (_database == nil) {
        _database = [[TrackerDatabase alloc] init];
    }
    return _database;
}

- (id)init {
    if ((self = [super init])) {
        NSString* sqLiteDb = [[NSBundle mainBundle] pathForResource:@"trackeddb" ofType:@"sqlite"];
        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database");
        }
    }
    return self;
}

- (void)dealloc {
    sqlite3_close(_database);
}

- (NSArray *)trackedItems:(NSString *)barcode {
    NSMutableArray *retval = [[NSMutableArray alloc] init];
    NSString *query = @"SELECT * from Tracked WHERE barcode=";
    query = [query stringByAppendingString:barcode];
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            char *barcodeChars      = (char *) sqlite3_column_text(statement, 0);
            char *partNumChars      = (char *) sqlite3_column_text(statement, 1);
            char *descChars         = (char *) sqlite3_column_text(statement, 2);
            char *colorChars        = (char *) sqlite3_column_text(statement, 3);
            char *sizeChars         = (char *) sqlite3_column_text(statement, 4);
            char *leadChars         = (char *) sqlite3_column_text(statement, 5);
            char *manufacturerChars = (char *) sqlite3_column_text(statement, 6);
            NSString *barcode      = [[NSString alloc] initWithUTF8String:barcodeChars];
            NSString *partNum      = [[NSString alloc] initWithUTF8String:partNumChars];
            NSString *desc         = [[NSString alloc] initWithUTF8String:descChars];
            NSString *color        = [[NSString alloc] initWithUTF8String:colorChars];
            NSString *size         = [[NSString alloc] initWithUTF8String:sizeChars];
            NSString *lead         = [[NSString alloc] initWithUTF8String:leadChars];
            NSString *manufacturer = [[NSString alloc] initWithUTF8String:manufacturerChars];

            TrackedItems *items = [[TrackedItems alloc] initWithBarcode:barcode partNum:partNum desc:desc
                color:color size:size lead:lead manufacturer:manufacturer];
            [retval addObject:items];
        }
        sqlite3_finalize(statement);
    }

    return retval;
}

@end

TrackerDatabase.h

//TrackerDatabase.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface TrackerDatabase : NSObject {
    sqlite3 *_database;
}

+ (TrackerDatabase*)database;
- (NSArray *)trackedItems:(NSString*)barcode;

@end

TrackedItems.m

#import "TrackedItems.h"

@implementation TrackedItems

@synthesize barcode      = _barcode;
@synthesize partNum      = _partNum;
@synthesize desc         = _desc;
@synthesize color        = _color;
@synthesize size         = _size;
@synthesize lead         = _lead;
@synthesize manufacturer = _manufacturer;

- (id)initWithBarcode:(NSString *)barcode partNum:(NSString *)partNum desc:(NSString *)desc
        color:(NSString *)color size:(NSString *)size lead:(NSString *)lead
        manufacturer:(NSString *)manufacturer {

    if ((self = [super init])) {
        self.barcode      = barcode;
        self.partNum      = partNum;
        self.desc         = desc;
        self.color        = color;
        self.size         = size;
        self.lead         = lead;
        self.manufacturer = manufacturer;
    }
    return self;
}

- (void) dealloc {
    self.barcode      = nil;
    self.partNum      = nil;
    self.desc         = nil;
    self.color        = nil;
    self.size         = nil;
    self.lead         = nil;
    self.manufacturer = nil;
}

@end

TrackedItems.h

#import <Foundation/Foundation.h>

@interface TrackedItems : NSObject {
    NSString *_barcode;
    NSString *_partNum;
    NSString *_desc;
    NSString *_color;
    NSString *_size;
    NSString *_lead;
    NSString *_manufacturer;
}

@property (nonatomic, copy) NSString *barcode;
@property (nonatomic, copy) NSString *partNum;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *color;
@property (nonatomic, copy) NSString *size;
@property (nonatomic, copy) NSString *lead;
@property (nonatomic, copy) NSString *manufacturer;

- (id)initWithBarcode:(NSString *)barcode partNum:(NSString *)partNum desc:(NSString *)desc
        color:(NSString *)color size:(NSString *)size lead:(NSString *)lead
        manufacturer:(NSString *)manufacturer;

@end

2 个答案:

答案 0 :(得分:0)

快速查看似乎可能的问题在于:

NSString *query = @"SELECT * from Tracked WHERE barcode=";
query = [query stringByAppendingString:barcode];

在您的表格中,条形码被声明为TEXT。所以你需要使用LIKE而不是=。

您的查询字符串是什么样的:

SELECT * from Tracked WHERE barcode=123123123123

应该是什么样子:

SELECT * from Tracked WHERE barcode LIKE '123123123'

答案 1 :(得分:-3)

如果您确定从sqlite获取信息,请检查您的tableview是否正确。

我建议使用Core Data,很容易做到,而且比iOS的sqlite更好