使用 stringWithFormat 进行查询设置

发布于 2024-12-08 04:01:00 字数 1702 浏览 1 评论 0原文

不完全确定为什么这不起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舂唻埖巳落 2024-12-15 04:01:00

我怀疑 NSNumber *start = (*cur_page * 50) - 50; 正在计算一个整数值,然后将该 int 值分配给指针 start,从而产生无效指针。 start 应声明为 intNSInteger

I suspect that NSNumber *start = (*cur_page * 50) - 50; is calculating an integer value and then assigning that int value to the pointer start, producing an invalid pointer. start should be declared as an int or NSInteger.

凯凯我们等你回来 2024-12-15 04:01:00

尝试使用 %@ 而不是 %d。 %d 表示整数; NSNumber 是一个 Objective-C 对象。

另外,不要

NSNumber *start = (*cur_page * 50) - 50;

尝试

NSNumber *start = [NSNumber numberWithInt:([cur_page intValue] * 50) - 50];

假设 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

NSNumber *start = (*cur_page * 50) - 50;

try

NSNumber *start = [NSNumber numberWithInt:([cur_page intValue] * 50) - 50];

Assuming cur_page is of type NSNumber*.

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnumber_Class/Reference/Reference.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文