【问题标题】:Sqlite doesn't seem to like my delete statementSqlite 似乎不喜欢我的删除语句
【发布时间】:2011-03-18 23:43:06
【问题描述】:

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

非常感谢大家

【问题讨论】:

  • 为什么不能发出更新语句而不是删除/插入?您是否有多天使用相同的 day_id?
  • 我知道这很麻烦,但我正在执行删除/插入,因为有时那天表中没有任何内容,我不想先检查然后在更新/之间进行选择插入。

标签: iphone objective-c sqlite


【解决方案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】:

从字符串中删除插入语句。无论如何都不会编译它,因为sqlite3_prepare_v2 将“只编译zSql 中的第一条语句。”

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

【讨论】:

    【解决方案3】:

    我真傻,我的 Documents 文件夹中只有一个架构的旧副本,其中没有“days”表。所以我按照这里的说明进行操作:Cleaning up the iPhone simulator,然后它复制了新模式,然后它又开始工作了。

    感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多