2

所以我的代码是假设创建一个数据库和表,然后将我的位置数据存储在表中。它击中了我制作的所有正确日志,例如“创建的表”和“插入的位置”,但是当我去查找文件时,它要么 (a) 不存在,要么 (b) 存在但其中没有位置数据。

我开始使用 CoreData,但它很快变成了一场噩梦,所以我改为直接使用 sqlite。

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

        locationManager =[[CLLocationManager alloc] init];

        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = 10;
        [locationManager startUpdatingLocation]; 



// Create a string containing the full path to the bold2.db inside the documents folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"bold2.sql"];

    // Check to see if the database file already exists
    bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];

    // Open the database and store the handle as a data member
    if (sqlite3_open([databasePath UTF8String], &databasehandle) == SQLITE_OK)
    {
        // Create the database if it doesn't yet exists in the file system
        if (!databaseAlreadyExists)
        {
            // Create the LOCATION table
            const char *sqlStatement = "CREATE TABLE IF NOT EXISTS LOCATION (ID INTEGER PRIMARY KEY AUTOINCREMENT, latitude DOUBLE, longitude DOUBLE, timeStamp DATE)";
            char *error;
            if (sqlite3_exec(databasehandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK)
          {
                NSLog(@"Created Table");
                 }
              }

这是我获取新位置并将其插入数据库的位置委托。奇怪的是它实际上命中了“插入的位置”日志。

    -(void) locationManager: (CLLocationManager *) manager
    didUpdateToLocation: (CLLocation *) newLocation
           fromLocation: (CLLocation *) oldLocation
{

    NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO location (latitude, longitude, timeStamp) VALUES (%g, %g, %g)",newLocation.coordinate.latitude, newLocation.coordinate.longitude, newLocation.timestamp];    
    char *error;   
    if ( sqlite3_exec(databasehandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK)
    {
        NSLog(@"Location inserted. %g", newLocation.coordinate.latitude);
    }    

    else NSLog(@"Error: %s", error);
}
4

1 回答 1

0

我解决了。由于我正在远程处理到 minimac,因此隐藏了 db 的文件路径。我刚刚将正确的文件路径粘贴到 go to folder 并找到了正确的文件。

于 2012-04-23T21:21:44.953 回答