使用 stringWithFormat 进行查询设置
不完全确定为什么这不起作用。
sqlite3 *db;
sqlite3_open([databasePath UTF8String], &db);
NSString *query;
NSNumber *start = (*cur_page * 50) - 50;
query = [NSString stringWithFormat:@"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC %d Limit 50;", start];
以前,代码看起来像:
sqlite3 *db;
sqlite3_open([databasePath UTF8String], &db);
NSString *query;
query = @"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC";
工作得很好。我尝试将 start 设置为:
int start
int *start
NSString *start
当然,我相应地更改了字符串的“%d”。一切都无济于事。我现在一无所知。当我现在点击此页面时,程序崩溃了。没有错误,无法使用 NSLog 写入任何内容。
我也尝试过使用:
[NSString alloc] initWithFormat
有什么想法吗?他们受到赞赏。谢谢。
---------------------------------------- 编辑 ----------------- -----------
.h
int page_count;
int cur_page;
@property int page_count;
@property int cur_page;
.m
@synthesize page_count;
@synthesize cur_page;
int start = (cur_page * 50) - 50;
query = [NSString stringWithFormat:@"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC %d Limit 50;", start];
现在,由于我确实根据之前的建议之一修复了类型转换,它不会使程序崩溃,但不会显示查询中的数据为 start = 0。当我查看时我的代码正是它应该等于的,如果我正确设置了我的 sqlite 语句,我会发现我将它放入错误位置的限制中...... -.- 所以最初的类型转换问题是崩溃和看来这是唯一的问题。
Not entirely sure why this isn't working.
sqlite3 *db;
sqlite3_open([databasePath UTF8String], &db);
NSString *query;
NSNumber *start = (*cur_page * 50) - 50;
query = [NSString stringWithFormat:@"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC %d Limit 50;", start];
Previously, the code looked like:
sqlite3 *db;
sqlite3_open([databasePath UTF8String], &db);
NSString *query;
query = @"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC";
which worked just fine. I've tried setting start as:
int start
int *start
NSString *start
and of course I changed the "%d" accordingly for the string. All to no avail. I'm clueless at the moment. The program crashes when I hit this page now. No errors, can't get anything to write using NSLog.
I've also tried using:
[NSString alloc] initWithFormat
Any ideas? They are appreciated. Thanks.
----------------------------- EDIT ----------------------------
.h
int page_count;
int cur_page;
@property int page_count;
@property int cur_page;
.m
@synthesize page_count;
@synthesize cur_page;
int start = (cur_page * 50) - 50;
query = [NSString stringWithFormat:@"SELECT rest_db.rest_id, rest_db.name, prices.value FROM rest_db JOIN prices ON rest_db.rest_id = prices.table_id WHERE prices.table_name = 'rest_db' ORDER BY rest_db.name ASC %d Limit 50;", start];
And now, since I did fix a typecast thanks to one of the previous suggestions, it doesn't crash the program, but shows no data from the query as start = 0. Which as I look over my code is exactly what it should equal, and if I had my sqlite statement set up correctly, I would see that I am putting it into the limit in the wrong place... -.- So the initial typecast issue was the crash and only issue, it would seem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑 NSNumber *start = (*cur_page * 50) - 50; 正在计算一个整数值,然后将该 int 值分配给指针
start
,从而产生无效指针。start
应声明为int
或NSInteger
。I suspect that
NSNumber *start = (*cur_page * 50) - 50;
is calculating an integer value and then assigning that int value to the pointerstart
, producing an invalid pointer.start
should be declared as anint
orNSInteger
.尝试使用 %@ 而不是 %d。 %d 表示整数; NSNumber 是一个 Objective-C 对象。
另外,不要
尝试
假设 cur_page 是 NSNumber* 类型。
http://developer.apple .com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html
Try using %@ instead of %d. %d is for integers; NSNumber is an objective-c object.
Also, instead of
try
Assuming cur_page is of type NSNumber*.
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html