2010-07-21 50 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,然后它复制新的架构,它又开始工作。

感谢您的帮助球员。