iOS上的SQLite(Spatialite)查询在64位模拟器上运行,但没有别的

时间:2013-12-31 01:07:31

标签: ios django sqlite pysqlite spatialite

我有一个由Django管理命令生成的Spatialite数据库,我将其嵌入到iOS应用程序中。数据库打开完美,但我发现在iOS上查询数据库只有在我在64位设备上运行时才有效。模拟或其他任何设备在调用SQLITE_NOTADB时会生成输出sqlite3_prepare_v2,并显示错误消息file is encrypted or is not a database

显然,生成这个数据库的Mac是64位机器,但SQLite数据库应该是位不可知的,所以我不知道这应该是一个什么问题。也许这不适用于Spatialite数据库?是否有任何标志我可以使用任何SQLite函数(可能是sqlite3_opensqlite3_prepare_v2或者pragma命令)来使它以与64-相同的方式读取文件有点拱形iOS吗?或者也许有一种方法可以用更平台兼容的格式从django生成Spatialite数据库?欢迎任何建议。

以下是我的代码片段,如果有人能找到任何明显的问题:

@implementation DataModel

@synthesize db;

- (id) init {
    self = [super init];
    if (self != nil) {
        spatialite_init(1);

        sqlite3 *newDbConnection;
        if (sqlite3_open([[self spatialiteDbPath] UTF8String], &newDbConnection) == SQLITE_OK) {
            NSLog(@"Database opened successfully");
            db = newDbConnection;
        } else {
            NSLog(@"Error opening database");
            db = NULL;
        }

    }

    return self;
}

- (NSArray *) getLockupsForRegion: (MKCoordinateRegion) region {

    NSMutableArray *lockups = [[NSMutableArray alloc] init];
    NSString *query = [NSString stringWithFormat:@"\
            SELECT name, X(location) as lat, Y(location) as lon, \
                   covered, type, capacity \
            FROM lockups_lockup \
            WHERE WITHIN(location, GeomFromText('POLYGON((%f %f, %f %f, %f %f, %f %f))'));",
                       regionCorners[0].latitude, regionCorners[0].longitude,
                       regionCorners[1].latitude, regionCorners[1].longitude,
                       regionCorners[2].latitude, regionCorners[2].longitude,
                       regionCorners[3].latitude, regionCorners[3].longitude];

    sqlite3_stmt *statement;
    if(db &&
       sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, NULL) == SQLITE_OK) {
        while(sqlite3_step(statement) == SQLITE_ROW) {
            NSLog(@"A row");
        }
    } else {
        NSLog(@"Query failed for reason: %s", sqlite3_errmsg(db));
        NSLog(@"Sqlite version: %s", sqlite3_version);
    }

    return lockups;

}

@end

启动应用程序并在非64位iOS平台getLockupsForRegion:上运行会生成日志输出:

SpatiaLite version ..: 4.1.1    Supported Extensions:
    - 'VirtualShape'    [direct Shapefile access]
    - 'VirtualDbf'      [direct DBF access]
    - 'VirtualText'     [direct CSV/TXT access]
    - 'VirtualNetwork'  [Dijkstra shortest path]
    - 'RTree'       [Spatial Index - R*Tree]
    - 'MbrCache'        [Spatial Index - MBR cache]
    - 'VirtualSpatialIndex' [R*Tree metahandler]
    - 'VirtualFDO'      [FDO-OGR interoperability]
    - 'SpatiaLite'      [Spatial SQL - OGC]
PROJ.4 version ......: Rel. 4.8.0, 6 March 2012
GEOS version ........: 3.4.2-CAPI-1.8.2 r3921
2013-12-31 00:29:39.567 App[8320:70b] Database opened successfully
2013-12-31 00:29:48.128 App[8320:70b] Query failed for reason: file is encrypted or is not a database
2013-12-31 00:29:48.710 App[8320:70b] Sqlite version: 3.8.1

arm64下的日志输出:

SpatiaLite version ..: 4.1.1    Supported Extensions:
    - 'VirtualShape'    [direct Shapefile access]
    - 'VirtualDbf'      [direct DBF access]
    - 'VirtualText'     [direct CSV/TXT access]
    - 'VirtualNetwork'  [Dijkstra shortest path]
    - 'RTree'       [Spatial Index - R*Tree]
    - 'MbrCache'        [Spatial Index - MBR cache]
    - 'VirtualSpatialIndex' [R*Tree metahandler]
    - 'VirtualFDO'      [FDO-OGR interoperability]
    - 'SpatiaLite'      [Spatial SQL - OGC]
PROJ.4 version ......: Rel. 4.8.0, 6 March 2012
GEOS version ........: 3.4.2-CAPI-1.8.2 r3921
2013-12-31 01:10:34.491 App[8548:70b] Database opened successfully
2013-12-31 01:10:44.913 App[8548:70b] A row
2013-12-31 01:10:44.913 App[8548:70b] A row
2013-12-31 01:10:44.914 App[8548:70b] A row
2013-12-31 01:10:44.914 App[8548:70b] A row
2013-12-31 01:10:44.915 App[8548:70b] A row
2013-12-31 01:10:44.915 App[8548:70b] A row
2013-12-31 01:10:44.916 App[8548:70b] A row
2013-12-31 01:10:44.916 App[8548:70b] A row

版本:

  • Python:2.7.5
  • Django:1.6
  • PySqlite:2.6.3
  • Sqlite(在生成Spatialite数据库的机器上):3.8.1
  • Spatialite(在机器上生成Spatialite数据库):4.1.1
  • Sqlite(在iOS上):3.8.1
  • Spatialite(在iOS上):4.1.1

对此的任何建议都将非常感谢!

0 个答案:

没有答案