【问题标题】:How columns/objects are created on indexedDB如何在 indexedDB 上创建列/对象
【发布时间】:2014-12-05 10:56:50
【问题描述】:

我对 indexedDB 很陌生,并开始创建一个数据库来传输 WebSQL。

我想知道的是如何在 indexedDB 中创建字段。

例如:

tx.executeSql("CREATE TABLE IF NOT EXISTS TEMPRESULTENTRIES ( "
    + " id INTEGER PRIMARY KEY AUTOINCREMENT, "
    + " instid INTEGER NOT NULL REFERENCES institutions(id) ON DELETE CASCADE, "
    + " qid INTEGER NOT NULL REFERENCES questionnaires(id) ON DELETE CASCADE, "
    + " result TEXT NOT NULL )");

如何在 indexedDB 中创建同一个表。我对 indexedDB 的这一部分有点困惑。 目前我只创建带有 id 的 keypath:

db.createObjectStore("TEMPRESULTENTRIES", {keypath: "id", autoIncrement: true});

我想知道如何添加,或者何时创建其他字段?

从一个小测试用例中,我可以看到,当我填充表/存储时,我可以在存储中创建列/对象,如下所示:

store.put({ObjectName: Value});

在商店中创建对象的方法正确吗?

最好的,艾瑞

【问题讨论】:

    标签: javascript indexeddb


    【解决方案1】:

    IndexedDB 是面向对象的数据库,而不是关系数据库。与 SQL 表最接近的类似物是对象存储,使用 IDBDatabase.createObjectStore 创建。对象存储没有固定模式,因此对IDBObjectStore.put 的调用可以接受具有任何字段的对象,只要该对象具有keyPath 字段或通过设置{autoIncrement: true} 使keyPath 成为可选。默认情况下,您只能使用keyPath 查询文档。要使用其他字段查询文档,必须使用IDBObjectStore.createIndex创建索引

    这是一个使用号码或姓名前缀查找联系人的应用程序的摘录。

    dbReq.onupgradeneeded = function (event) {
        var db = event.target.result,
            objectStore = db.createObjectStore('calls', {keyPath: 'timestamp'}),
            contactStore = db.createObjectStore('contacts', {autoIncrement: true});
    
        objectStore.createIndex('number', 'number', {unique: false});
        contactStore.createIndex('number', 'number');
        contactStore.createIndex('name', 'name', {unique: false});
        objectStore.createIndex('timestamp', 'timestamp', {unique: true});
    };
    

    按前缀查找:

    findPrefix: function(prefix, fn) {
        var transaction = this.db.transaction(['contacts', 'calls']),
            contactStore = transaction.objectStore('contacts'),
            callStore = transaction.objectStore('calls'),
            numberIndex = callStore.index('number'),
            nameIndex = contactStore.index('name'),
            key = IDBKeyRange.bound(prefix, prefix + '\uffff'),
            times = 0,
            result = [],
            consume = function(records) {
                result = result.concat(records);
                if (++times === 2) {
                    /* Remove duplicate numbers: names and numbers may have the same value*/
                    var numbers = {};
                    result = result.filter(function(contact) {
                        if (!numbers[contact.number]) {
                            return (numbers[contact.number] = true);
                        }
                        return false;
                    });
                    fn(result);
                }
            };
    
        this.consumeCursor(numberIndex.openCursor(key, 'nextunique'), consume);
        this.consumeCursor(nameIndex.openCursor(key), consume);
    }
    

    More on IndexedDB

    【讨论】:

    • 所以要拥有其他对象字段,我需要在创建ObjectStore时首先为其创建keypath?如果我错了,请纠正我。
    • 不,对象存储中最多可以有一个keyPath 字段。 keyPath 字段用于查找对象,并且必须是唯一的。您可以将具有任何字段的对象添加到对象存储中,而无需重新调整对象存储。也就是说,objectStore.put({keyPathField: 1, f1: 2, f2: 3})objectStore.put({keyPathField: 2, g1: [1,3], g2: 3}) 都是允许的。如果字段上没有任何索引,则只能使用 keyPath 字段的值查找对象存储:IDBObjectStore.get(2) 将产生 {keyPathField: 2, g1: [1,3], g2: 3}
    • 感谢您的澄清。如果你能回答,我可以在 onupgradeneeded 函数上创建几个对象库吗?当我第一次创建数据库和对象存储时?
    • 我刚刚查了一下,到目前为止我可以创建几个商店。如果有什么可以补充的,请随时发表评论。谢谢
    • 是的,你可以。我添加了我的一个应用程序的摘录。它在onupgradeneeded 上创建两个对象存储,同时查询它们并过滤结果。 findPrefix 方法通过姓名或号码查找联系人。
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2016-02-24
    • 2014-04-23
    • 2011-03-12
    • 2014-07-22
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多