【发布时间】:2012-12-26 11:02:09
【问题描述】:
我正在尝试从数据库中获取一个随机字符串,我尝试将数组转换为字符串,但是当我单击按钮时,我只能获得地址,谁能帮助我如何在按钮操作中显示该字符串?
- (IBAction)randBtn:(id)sender {
SendQuoteVC *svc= [[SendQuoteVC alloc]initWithNibName:@"SendQuoteVC" bundle:nil];
DBHandler *db =[[DBHandler alloc]init];
arr =[db randomQuote];
NSMutableString * show = [[NSMutableString alloc] init];
for (NSObject * obj in arr)
{
[show appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", show);
NSString *rs =[NSString stringWithFormat:@" %@",show];
svc.quote=rs;
[self.navigationController pushViewController:svc animated:YES];
}
我的日志消息是
2012-12-26 17:02:46.062 SendQuote[2281:c07] QUERY: SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1
2012-12-26 17:02:46.097 SendQuote[2281:c07] txtQuote is (
"<quoteDC: 0xa9a94c0>"
)
2012-12-26 17:02:46.100 SendQuote[2281:c07] rand is (
"<quoteDC: 0xa9a94c0>"
)
我已将我的用户实体类更改为 quoteDC 类,我的 Db 函数是
-(NSMutableArray *)randomQuote{
NSMutableArray *dataArray =[[NSMutableArray alloc]init];
NSString * sqlStr=[NSString stringWithFormat:@"SELECT * FROM quotes ORDER BY RANDOM() LIMIT 1"];
sqlite3_stmt *ReturnStatement = (sqlite3_stmt *) [self getStatement: sqlStr];
while (sqlite3_step(ReturnStatement)==SQLITE_ROW)
{
@try
{
quoteDC *worddc = [[quoteDC alloc] init];
NSString *userid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 0)];
NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(ReturnStatement, 1)];
worddc.quote_id=[userid integerValue];
worddc.quotes=name;
[dataArray addObject:worddc];
}
@catch (NSException *ept) {
NSLog(@"Exception in %s, Reason: %@", __PRETTY_FUNCTION__, [ept reason]);
}
}
return dataArray;
}
【问题讨论】:
-
您如何存储从数据库中检索到的值?您是否使用任何实体类的对象?
-
是的。我正在使用实体类..
-
如果您粘贴您的数据库方法,那么我们将提供更准确的答案
-
我编辑了我的问题并更改了我的实体类
标签: iphone ios xcode arrays memory-management