2011-05-17 57 views
0
-(void)checkAndCreateDatabase{ 
    NSLog(@"hello"); 
    databaseName = @"EMAP234.sql"; 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
    NSLog(@"database path %@",databasePath); 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    [fileManager release]; 
} 

-(void)insertIntoDatabase:(NSString *)insertCommand{ 
    // Setup the database object 
    sqlite3 *database; 

    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = [insertCommand UTF8String]; 
     sqlite3_stmt *compiledStatement; 


     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      // Loop through the results and add them to the feeds array 
      NSLog(@"inserted"); 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) {} 
     } 

     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database); 

} 

該應用程序第一次工作正常,但當我重新啓動它時,它可以讀取較早的值,但不能插入新的值... pls幫助當應用程序終止並重新啓動時,無法在iPhone的sqlite數據庫中插入

這裏是我如何打電話這個....

DatabaseOperations *obj=[[DatabaseOperations alloc]init]; 
[obj checkAndCreateDatabase]; 
NSLog(@"patient ID %d",[obj readMaxPatientIDFromDatabase]); 

patientID= [NSString stringWithFormat:@"HCI%d", ([obj readMaxPatientIDFromDatabase]+1)]; 
NSLog(@"patient ID %@",patientID); 
[self generatePatientInsertCommand]; 
[obj insertIntoDatabase:patientInsertCommand]; 
[self generateCarrierInsertCommand]; 
[obj insertIntoDatabase:carrierInsertCommand]; 
[self generateSyncTableInsertCommand]; 
[obj insertIntoDatabase:syncTableInsertCommand]; 
NSString *[email protected]"your PatientID is"; 
[obj release]; 
msg=[msg stringByAppendingString:patientID]; 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REGISTERED" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; 
[alert show]; 
[alert release]; 

回答

0

你好ANKIT我用你的代碼,並呼籲

[self checkAndCreateDatabase]; 
[self insertIntoDatabase:[NSString stringWithFormat:@"INSERT INTO User (uId,userName,userBirthday) VALUES (\"%d\", \"%@\", \"%@\")",3,@"Admin",@"2010-12-01"]]; 

通過

-(void)checkAndCreateDatabase{ 
    NSLog(@"hello"); 
    databaseName = @"MasterDB.sqlite"; 
.... 
} 

的代碼我改變使用我選擇checkAndCreateDatabase功能不同的數據庫文件中的唯一工作得很好,每插入一個新的UID。請檢查您的閱讀功能和如果可能,然後粘貼代碼閱讀功能以及上述代碼和您稱這兩個閱讀和插入功能的方式。

+0

條目不是在數據庫中,我通過sqlite管理器進行檢查。我在nsobject類中有這兩個函數,並從rootViewController中調用它們。 – Ankit 2011-05-18 08:20:21

+0

您提到「該應用第一次正常工作,但是當我重新啓動它時,它可以讀取較早的值,但不能插入新的值」。你可以粘貼代碼,你從rootviewController調用這些方法的方式嗎? – 2011-05-18 08:46:56

+0

我編輯並給出了主要問題中的詳細信息...... – Ankit 2011-05-18 10:15:25

相關問題