简体   繁体   中英

Cannot get data from SQLite DB to display in iOS app

so I've got a barcode scanning app that is supposed to read the barcode, match it up with an entry in a database, and display all other information about the entry.

I'm using a single file DB called trackeddb.sqlite, which was created using terminal and sqlite3 commands. It houses two tables, one for static information about products (each part number is unique and has its own entry) for filling out a 'first scan' entry, and a second table which will house the same product information, but also barcodes. The second table allows the user to store multiple products of the same part number and specs, but using a barcode to create unique entries.

My issue is, when the barcode scans, it's supposed to display information stored in the second table (if the barcode matches). The code is sound, but when I run my barcode through (after an entry is in the second table) it displays no data other than preset text. I've racked my brain over this and I can't figure it out, but I think it may have to do with how I'm referencing my database.

Thanks for your time!

What I am greeted with when I scan the barcodes

This is the sqlite DB

PasteBin link to sqlite db and schema

The database is called from this method

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

Here are the main portions of my code, the barcode scanner works perfectly so I won't bother posting that bit. This is just for the database.

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

After a quick look it seems the problem might lie in:

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

In your table, barcode is declared as TEXT. So you need to use LIKE instead of =.

What your query string looks like:

SELECT * from Tracked WHERE barcode=123123123123

What it should look like:

SELECT * from Tracked WHERE barcode LIKE '123123123'

If you're sure that you get information from sqlite, check if your tableview is correctly.

I suggest use Core Data, is easy to do and its better than sqlite for iOS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM