2012-04-19 17 views
2

私のコードは、データベースとテーブルを作成し、テーブルに自分のロケーションデータを格納することを想定しています。 「作成されたテーブル」や「場所が挿入されました」のように、私が作ったすべての正しいログを記録していますが、ファイルが見つかると、そこにはないか(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"); 
       } 
       } 

ここで私は新しい場所を取得し、データベースに挿入する場所の代理人です。奇妙なことは、実際には "Location inserted"ログに当たるということです。

-(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); 
} 
+1

「私がファイルを見つけるときに」と言ったら、どういう意味ですか?あなたはシミュレータまたはデバイス上でこれを実行していますか?また、プログラムの呼び出しごとに「作成されたテーブル」メッセージが記録されますか?私は確信していないが、私はシミュレータが実行間のファイルを保持しない可能性があると思う。 –

+1

エラーが印刷されていますか? –

+1

データベースのパスをシミュレータとデバイスに出力します。 –

答えて

0

私はそれを解決しました。私がminimacにリモーティングしているので、dbへのファイルパスは隠されていました。私はちょうど正しいファイルパスをフォルダに貼り付け、正しいファイルを見つけました。

関連する問題