Sqlite似乎不喜欢我的删除语句

时间:2010-07-21 13:03:03

标签: iphone objective-c sqlite

我有以下的iphone代码,这似乎是失败的:

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'确实是打开的,其他查询似乎工作正常。

非常感谢你们

3 个答案:

答案 0 :(得分:2)

从字符串中删除insert语句。它不会被编译,因为sqlite3_prepare_v2将“只编译zSql中的第一个语句。”

也许您应该使用触发器进行(可选)删除,或使用insert or replace

答案 1 :(得分:1)

在打开数据库之前,您确定已将数据库复制到Documents目录中吗? iPhone OS仅允许在文档目录中写入权限。以下是将数据库复制到Documents目录的代码 -

//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...
    }

答案 2 :(得分:1)

愚蠢的我,我刚在我的Documents文件夹中有一个旧模式的副本,其中没有'days'表。所以我按照这里的说明进行操作:Cleaning up the iPhone simulator,然后将新模式复制过来,然后重新开始工作。

感谢帮助人员。