我正在研究SQLite数据库。我正在将数据插入到数据库中,这很好。但我想删除数据库中的数据不起作用,我想我做错了以下是我的代码,请任何人帮助我:
-(IBAction)deleteData {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"paddleEight.sqlite"];
NSLog(@"The Database Path is---> %@", databasePath);
sqlite3_stmt *compiledStatement;
sqlite3 *db;
NSString *sqlStatement = @"DELETE from GalleryTabel;";
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
if (sqlite3_prepare(db, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record Deleted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}
sqlite3_finalize(compiledStatement);
}
}
答案 0 :(得分:5)
尝试记录NSLog(@"Error=%s",sqlite3_errmsg(&db)
,看看是否有任何错误。检查它是否进入sqlite3_open statement
。
我从未使用分号来结束删除语句,我在sqlite中的知识非常有限。但我总是使用NSString *sqlStatement = @"DELETE from GalleryTabel";
注意分号被删除。
尝试使用 exec
代替prepare,step,finalize
方法。
例如:
NSString*deleteSQL=[NSString stringWithFormat:@"delete from GalleryTabel"];
const char*deleteStmt=[deleteScoreSQL UTF8String];
char*errMsg=nil;
if(sqlite3_exec(db, deleteScoreStmt, NULL, NULL, &errMsg)==SQLITE_OK)
{
NSLog(@"Deleted table");
}
希望这有帮助。