我想从数据库中删除记录,
我在 file.h 中定义了数据库,在 file.m 中定义了插入数据和读取数据,它可以工作,但删除不起作用。
这是文件.h
#import "sqlite3.h"
#define DATA_FILE @"prova12"
#define TABLE_NAME @"password"
#define FIELDS_NAME_SID @"pass"
#define FIELDS_NAME_SNAME @"foto"
#define FIELDS_NAME_SCLASS @"studentClass"
#define FIELDS_NAME_PROVA @"provaClass"
{sqlite3 *db;
}
在文件.m
对于路径:
-(NSString *)dataFilePath {
NSArray * myPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES); NSString * myDocPath = [myPaths objectAtIndex:0];
NSString *filename = [myDocPath stringByAppendingPathComponent:DATA_FILE];
return filename;
供阅读:
-(NSMutableArray*)selectAll
{
NSMutableArray *list = [[NSMutableArray alloc] initWithObjects:nil];
NSString *filename = [self dataFilePath];
NSLog(@"%@",filename);
if (sqlite3_open([filename UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO,@"no");
} else {
NSString *qsql = [NSString stringWithFormat: @"SELECT %@ FROM %@", FIELDS_NAME_SID, TABLE_NAME];
NSLog(@"qui%@",qsql);
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [inserisci.text UTF8String], -1, NULL);
while (sqlite3_step(statement) == SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement, 0);
NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
//studentId.text = field1Str;
//[field1Str release];
[list addObject:field1Str];
NSLog(@"%d",list.count);
}
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
return list;
}
这是为了保存:
-(IBAction) save {
NSString *filename = [self dataFilePath];
if (sqlite3_open([filename UTF8String], &db) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(NO,@"no");
} else {
NSString *sqlStr = [NSString stringWithFormat: @"INSERT OR REPLACE INTO %@ (%@, %@, %@ ,%@) VALUES (?,?,?,?)",
TABLE_NAME, FIELDS_NAME_SID, FIELDS_NAME_SNAME, FIELDS_NAME_SCLASS, FIELDS_NAME_PROVA];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"quiiiiddddd %@", filename);
sqlite3_bind_text(statement, 1, [inserisci.text UTF8String], -1, NULL);
sqlite3_bind_text(statement, 2, [immagine UTF8String], -1, NULL);
sqlite3_bind_text(statement, 3, [inserisci.text UTF8String], -1, NULL);
sqlite3_bind_text(statement, 4, [inserisci.text UTF8String], -1, NULL);
if (sqlite3_step(statement) != SQLITE_DONE) {
NSAssert(0, @"no");
}
}
sqlite3_finalize(statement);
sqlite3_close(db);
}
但是我没有删除数据的想法,有人可以帮我吗?