是否在iPhone中的sqlite3数据库支持DELETE?
答
这是一个示例函数,它从给定的url中删除sqlite数据库中的一个项目。这里我们有表格“文章”和列“GUID”。
例如,如果我们写这样的查询
DELETE FROM ARTICLES WHERE GUID ='http://www.google.com'
这意味着我们将删除行,其中列“GUID”的值等于“http://www.google.com” 全行将被删除
- (void) DeleteFromDataBase: (NSString*) mainUrl
{
sqlite3_stmt* statement;
if(sqlite3_open([self.dataBasePath UTF8String], &articlesDB) == SQLITE_OK)
{
// Create Query String.
NSString* sqlStatement = [NSString stringWithFormat:@"DELETE FROM ARTICLES WHERE GUID ='%@'", mainUrl];
if(sqlite3_prepare_v2(articlesDB, [sqlStatement UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Item with url: %@ was deleted", mainUrl);
}
else
{
NSLog(@"DeleteFromDataBase: Failed from sqlite3_step. Error is: %s", sqlite3_errmsg(articlesDB));
}
}
else
{
NSLog(@"DeleteFromDataBase: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(articlesDB));
}
// Finalize and close database.
sqlite3_finalize(statement);
sqlite3_close(articlesDB);
}
else
{
NSLog(@"DeleteFromDataBase: Error While opening database. Error: %s\n", sqlite3_errmsg(articlesDB));
}
}
以下是您如何执行该操作的方法! – 2011-02-22 13:04:24