【问题标题】:How to get/delete multiple rows in SQLITE table in iOS?如何在 iOS 中获取/删除 SQLITE 表中的多行?
【发布时间】:2016-01-01 00:49:42
【问题描述】:

我在 SQLITE 中的表格式如下。

id   name      date      height
 1    tt     12-2-15       120
 2    ss     15-3-15       110
 3    tt     14-5-15       120
 ....
 10   tt     19-6-15       130

(1)我喜欢在一条指令中获取名称为 tt 的所有行,然后放入二维数组。如何实现?

(2)如何在一条指令中删除名称为tt的所有行?

我的代码在 iOS 中。我无法轻易找到解决方案。

编辑: 只是为了分享我的实现。

    NSString *databasePath;
    NSString *docsDir;
    NSArray *dirPaths;

    //Database
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = [dirPaths objectAtIndex:0];
    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent: @"RECORDS.db"]];
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath,& RECORDS) == SQLITE_OK)
    {
        retval = [[NSMutableArray alloc] init];

        NSString *query = [NSString stringWithFormat:@"SELECT id, nameid, date, height FROM RECORD WHERE nameid=\"%@\"", currentNameID];
        const char *query_stmt = [query UTF8String];
        if (sqlite3_prepare_v2(RECORDS, query_stmt, -1, &statement, nil) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                char *date = (char *) sqlite3_column_text(statement, 1);
                int height = (int) sqlite3_column_text(statement, 2);
                NSString *name = [[NSString alloc] initWithUTF8String:date];
                HeightInfo *info = [[HeightInfo alloc] initWithInfos:name withheight:(int)height];
                [retval addObject:info];

            }
            sqlite3_finalize(statement);
            sqlite3_close(RECORDS);
        }
    }

谢谢

【问题讨论】:

  • 这里有很多涉及 SQLite 的主题。如果它们都不能帮助您,那么还有什么可以帮助您?
  • 对不起兄弟。我只是无法轻松找到信息。现在我明白了。

标签: ios objective-c xcode sqlite


【解决方案1】:

您需要有关 SQL 或 ios 框架的帮助吗?
SQL 非常简单;

select id, name, date, height from mytablename where name = "tt";

delete from mytablename where name == "tt";         

练习命令行上的命令,以便了解它们是如何工作的。

要处理您的数据,请查看 Ray Wenderlich 的出色示例;

http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app

【讨论】:

  • 谢谢我按照你的建议实现了。我无法轻易找到信息。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
相关资源
最近更新 更多