Iphone Sqlite不';我似乎不喜欢我的删除语句
我有以下的iphone代码,似乎失败了:Iphone Sqlite不';我似乎不喜欢我的删除语句,iphone,objective-c,sqlite,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_错误(根据S
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_错误(根据SQLITE站点,SQL错误或缺少数据库)。不知道我做错了什么?数据库“db”确实是开放的,其他查询似乎工作正常
非常感谢各位您确定在打开数据库之前已将其复制到文档目录中吗?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...
}
您确定在打开数据库之前已将其复制到文档目录中吗?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...
}
从字符串中删除insert语句。由于
sqlite3\u prepare\u v2
将“仅编译zSql
中的第一条语句”,因此它不会被编译
也许您应该使用触发器来执行(可选)删除操作,或者使用
insert或replace
从字符串中删除insert语句。由于sqlite3\u prepare\u v2
将“仅编译zSql
中的第一条语句”,因此它不会被编译
也许您应该使用触发器执行(可选)删除,或者使用插入或替换愚蠢的我,我的文档文件夹中有一个旧的架构副本,其中没有“天”表。所以我按照这里的说明:,然后它复制了新的模式,然后它又开始工作了
谢谢大家的帮助。我真傻,我的文档文件夹里有一份旧的模式副本,里面没有“days”表。所以我按照这里的说明:,然后它复制了新的模式,然后它又开始工作了
谢谢大家的帮助。为什么你不能发布更新语句而不是删除/插入?您是否有多天使用同一天的id?我知道这很麻烦,但我正在执行删除/插入操作,因为有时表中没有该天的内容,我不想先进行检查,然后在更新/插入之间进行选择。为什么您不能发出update语句而不是delete/insert语句?您是否有多天使用同一天的id?我知道这很麻烦,但我正在执行删除/插入操作,因为有时表中没有该天的内容,我不想先检查然后在更新/插入之间进行选择。是的,我想到了这一点,我已确保它已复制到文档中。谢谢你的帮助。是的,我想到了这一点,我已经确保它被复制到文档中。谢谢你的帮助。