gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:57:29 25 4
gpt4 key购买 nike

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

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

我的问题是,当条形码扫描时,它应该显示存储在第二个表中的信息(如果条形码匹配)。代码是正确的,但是当我运行我的条形码时(在第二个表中有一个条目之后)它不显示除预设文本以外的任何数据。我为此绞尽脑汁,想不通,但我认为这可能与我引用数据库的方式有关。

感谢您的宝贵时间!

What I am greeted with when I scan the barcodes

这是sqlite数据库

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

最佳答案

快速浏览后,问题似乎可能在于:

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

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

你的查询字符串看起来像什么:

SELECT * from Tracked WHERE barcode=123123123123

它应该是什么样子:

SELECT * from Tracked WHERE barcode LIKE '123123123'

关于ios - 无法从 SQLite 数据库获取数据以显示在 iOS 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com