2010-07-21 36 views
0

我有以下的iphone代碼,這似乎是失敗的:SQLite的似乎不喜歡我的delete語句

sqlite3_stmt *dbps; 
NSString *sql = @"delete from days where day=?1;insert into days(disabled,recipe_id,day) values(?2,?3,?1)"; 
int rc = sqlite3_prepare_v2(db, sql.UTF8String, -1, &dbps, NULL); 
... 

的「RC」返回代碼爲1,這意味着SQLITE_ERROR(SQL錯誤或缺少數據庫,根據sqlite網站)。不知道我做錯了什麼?數據庫'db'確實是打開的,其他查詢似乎工作正常。

非常感謝球員

+0

爲什麼不能發出更新語句而不是刪除/插入?你有幾天有同一day_id? – 2010-07-21 13:06:07

+0

我知道這是kludgey,但我正在做刪除/插入,因爲有時表中沒有任何內容,我不想先檢查然後選擇更新/插入。 – Chris 2010-07-21 13:40:55

回答

1

你確定你打開它之前已經複製的文件目錄下的數據庫? iPhone OS只允許在文檔目錄中寫入權限。下面是拷貝數據庫文件目錄代碼 -

//function to copy database in Documents dir. 

    -(void) checkAndCreateDatabase{ 
     // Check if the SQL database has already been saved to the users phone, if not then copy it over 
     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]; 


    } 


// open the database and fire the delete query... 


    sqlite3 *database; 
    NSString *sqlStatement = @""; 



    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
      NSLog(@"%@",databasePath); 
    [serlf checkAndCreateDatabase]; 



    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    { 

// here you can fire the delete query... 
    } 
+0

是的,我想到了,我確信它被複制到文檔中。感謝您的幫助。 – Chris 2010-07-21 13:41:31

2

從字符串中取出插入語句。因爲sqlite3_prepare_v2將「只編譯zSql中的第一個語句」,所以它不會被編譯。

也許你應該使用觸發器來做你的(可選的)刪除,或者使用insert or replace

1

傻了,我只是在我的文檔文件夾中有一箇舊的副本,它沒有'天'表。所以我按照這裏的說明:Cleaning up the iPhone simulator,然後它複製新的架構,它又開始工作。

感謝您的幫助球員。