我有2个表资源, ResourceViewHistory 资源包括以下字段: ResourceId , TypeId ,名称,网址, IsFavorite ,的语言代码下,的类别ID 即可。 ResourceViewHistory包含以下字段: ViewId , ResourceId , DateViewed ,经度,纬度 < / p>
我想在Resources表中为ResourceId设置结果集:ResourceViewHistory表中相同ResourceId的Name,Url,IsFavorite,ResourceId和DateViewed,Longitude,Latitude以及CategoryId = 1,TypeId = 1
我的查询是:
SELECT r.ResourceId,Url,Name,IsFavorite,max(v.DateViewed) AS LastVisited,Longitude,Latitude
FROM Resources r
LEFT JOIN ResourceViewHistory v on (r.ResourceId=v.ResourceId)
WHERE CategoryId=1 AND TypeId=1
GROUP BY r.ResourceId
以上查询在模拟器中工作但在iPhone设备中不起作用。我的ObjectiveC代码如下.arrUrlDetails数组总是显示0个对象
+(NSMutableArray*)getUrlDetails{
NSString *strHome = NSHomeDirectory();
NSString *strDestPath = [NSString stringWithFormat:@"%@/Documents/test.sqlite",strHome];
NSMutableArray *arrUrlDetails=[[NSMutableArray alloc]init];
sqlite3 *db;
int n = sqlite3_open([strDestPath UTF8String], &db);
if (n==SQLITE_OK) {
NSString *strQuery=@"SELECT r.ResourceId,Url,Name,IsFavorite,max(v.DateViewed) AS LastVisited,Longitude,Latitude FROM Resources r LEFT JOIN ResourceViewHistory v on (r.ResourceId=v.ResourceId) where CategoryId=1 AND TypeId=1 group by r.ResourceId";
sqlite3_stmt *stmt;
const char *err_msg;
int res=sqlite3_prepare_v2(db, [strQuery UTF8String], -1, &stmt, &err_msg);
if (res == SQLITE_OK) {
while (sqlite3_step(stmt)==SQLITE_ROW) {
Resources *resources=[[Resources alloc]init];
resources.resourceId=sqlite3_column_int(stmt,0);
resources.name=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 2)];
resources.url=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 1)];
resources.isFavorite=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 3)];
resources.dateViewed=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 4)];
resources.latitude=sqlite3_column_double(stmt, 6);
[arrUrlDetails addObject:resources];
}
}
}
return arrUrlDetails;
}