从sqlite表iphone sdk中删除数据?

发布于 2024-11-09 10:10:43 字数 2879 浏览 0 评论 0原文

任何人都可以帮我制作一种从 sqlite 表中删除数据的方法,这里有一些代码..

谢谢,

TC

-(void)openDB{
//create database 
if (sqlite3_open([[self filPath] UTF8String],&db) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0, @"database failed to open");
}
}


 -(void)insertRecordIntoTableNamed:(NSString *)tableName withField1:(NSString *)field1 field1Value:   (NSString*)field1Value andField2:(NSString *)field2 field2Value:(NSString *)field2Value {

// [self insertRecordIntoTableNamed:@"Contacts" withFiled1:@"email" field1Value:email andField2:@"name" field2Value:name];
NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@' , '%@') VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0,@"error updating table.");
}
}

-(void)createTableNamed:(NSString *) tableName withField1:(NSString *)field1 withField2:(NSString     *)field2 {
char *err;
NSString *sql = [NSString stringWithFormat:@"Creat table if not exist '%@' ('%@' text prmaru key,  '%@' TEXT",tableName,field1,field2];
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert (0,@"tabel failed to create");
}
}       

-(void)getAllRowsFromTableNamed:(NSString *)tableName{
NSString *qsql = [NSString stringWithFormat:@"select * from %@",tableName];
sqlite3_stmt *statment;

if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statment, nil) == SQLITE_OK) {
    while (sqlite3_step(statment) == SQLITE_ROW) {
        char *field1 = (char *)sqlite3_column_text(statment, 0); 
        NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
        char *field2 = (char *)sqlite3_column_text(statment, 1);
        NSString *field2Str = [[NSString alloc] initWithUTF8String: field2]; 
        NSString *str = [[NSString alloc] initWithFormat:@"%@ - %@", field1Str, field2Str];
        NSLog(@"%@", str);
        //      [field1Str release];
        //      [field2Str release];
        //      [str release];
    }       
    sqlite3_finalize(   statment);
}
 }

- (void)viewDidLoad {
[self openDB];
NSLog(@"%@",[self filPath]);
[self createTableNamed:@"Contacts" withField1:@"email" withField2:@"name"];

for (int i = 0; i<= 2;i++) {
    NSString *email = [[NSString alloc]initWithFormat:@"user%[email protected]",i];       
    NSString *name = [[NSString alloc]initWithFormat:@"user %i",i];
    [self insertRecordIntoTableNamed:@"Contacts" withField1:@"email" field1Value:email     andField2:@"name" field2Value:name];
    [email release];
    [name release];
}

[self getAllRowsFromTableNamed:@"Contacts"];
sqlite3_close(db);
[super viewDidLoad];
}

could any one help me make a method that will delete data from the sqlite table here is some code..

thanks,

TC

-(void)openDB{
//create database 
if (sqlite3_open([[self filPath] UTF8String],&db) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0, @"database failed to open");
}
}


 -(void)insertRecordIntoTableNamed:(NSString *)tableName withField1:(NSString *)field1 field1Value:   (NSString*)field1Value andField2:(NSString *)field2 field2Value:(NSString *)field2Value {

// [self insertRecordIntoTableNamed:@"Contacts" withFiled1:@"email" field1Value:email andField2:@"name" field2Value:name];
NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@' , '%@') VALUES ('%@','%@')",tableName,field1,field2, field1Value,field2Value];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
    sqlite3_close(db);
    NSAssert(0,@"error updating table.");
}
}

-(void)createTableNamed:(NSString *) tableName withField1:(NSString *)field1 withField2:(NSString     *)field2 {
char *err;
NSString *sql = [NSString stringWithFormat:@"Creat table if not exist '%@' ('%@' text prmaru key,  '%@' TEXT",tableName,field1,field2];
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
    sqlite3_close(db);
    NSAssert (0,@"tabel failed to create");
}
}       

-(void)getAllRowsFromTableNamed:(NSString *)tableName{
NSString *qsql = [NSString stringWithFormat:@"select * from %@",tableName];
sqlite3_stmt *statment;

if (sqlite3_prepare_v2(db, [qsql UTF8String], -1, &statment, nil) == SQLITE_OK) {
    while (sqlite3_step(statment) == SQLITE_ROW) {
        char *field1 = (char *)sqlite3_column_text(statment, 0); 
        NSString *field1Str = [[NSString alloc] initWithUTF8String: field1];
        char *field2 = (char *)sqlite3_column_text(statment, 1);
        NSString *field2Str = [[NSString alloc] initWithUTF8String: field2]; 
        NSString *str = [[NSString alloc] initWithFormat:@"%@ - %@", field1Str, field2Str];
        NSLog(@"%@", str);
        //      [field1Str release];
        //      [field2Str release];
        //      [str release];
    }       
    sqlite3_finalize(   statment);
}
 }

- (void)viewDidLoad {
[self openDB];
NSLog(@"%@",[self filPath]);
[self createTableNamed:@"Contacts" withField1:@"email" withField2:@"name"];

for (int i = 0; i<= 2;i++) {
    NSString *email = [[NSString alloc]initWithFormat:@"user%[email protected]",i];       
    NSString *name = [[NSString alloc]initWithFormat:@"user %i",i];
    [self insertRecordIntoTableNamed:@"Contacts" withField1:@"email" field1Value:email     andField2:@"name" field2Value:name];
    [email release];
    [name release];
}

[self getAllRowsFromTableNamed:@"Contacts"];
sqlite3_close(db);
[super viewDidLoad];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2024-11-16 10:10:43

您需要执行类似的操作(这是我项目中的代码,它可以工作,但您必须更改表名称等)

//Delete all applicable rows from the datasets table...
NSString *deleteStatementNS = [NSString stringWithFormat:
                               @"DELETE FROM datasets\
                               WHERE customerID = %i",
                               customerID];

NSLog(@"DELETE statement: %@", deleteStatementNS);

const char *deleteStatement = [deleteStatementNS UTF8String];
dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);

sqlite3_finalize(dbps);
dbps = NULL;

if (dbrc != 101) {  //anything except 101 (SQLITE_DONE for delete, *not* SQLITE_OK)
    NSLog(@"couldn't delete the customer from datasets: result code %i", dbrc);
    return;
}
else {
    NSLog(@"deleted the customer from datasets");
}

You need to do something like this (this is code from my project & it works, but you'll have to change the table name, etc.)

//Delete all applicable rows from the datasets table...
NSString *deleteStatementNS = [NSString stringWithFormat:
                               @"DELETE FROM datasets\
                               WHERE customerID = %i",
                               customerID];

NSLog(@"DELETE statement: %@", deleteStatementNS);

const char *deleteStatement = [deleteStatementNS UTF8String];
dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
dbrc = sqlite3_step(dbps);

sqlite3_finalize(dbps);
dbps = NULL;

if (dbrc != 101) {  //anything except 101 (SQLITE_DONE for delete, *not* SQLITE_OK)
    NSLog(@"couldn't delete the customer from datasets: result code %i", dbrc);
    return;
}
else {
    NSLog(@"deleted the customer from datasets");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文