开发者

HTML5 Database to verify exist table?

Is there a way to verify whether the table is exist inside the database in HTML5 local database?

I need to create 9 tables, and this method will run when the document ready. If each time the page start, it also calling the same method, will it not be waste of memory? I using it for the mobile device (iPhone).

This is the code:

try{
                if(!window.openDatabase){
                    alert('DB not supported.');
                }else{
                    var shortName = 'abc';
                    var version = '1.0';
                    开发者_JAVA百科var displayName = 'ABC';
                    var maxSize = 3145728;
                    var tableName = ['business', 'politic', 'firstread', 'commentary','features', 'insiderasia', 'management', 'media'];
                    db = openDatabase(shortName, version, displayName, maxSize);

                    $.each(tableName, function(theCount, value){
                        db.transaction(
                            function(transaction){
                                transaction.executeSql('CREATE TABLE IF NOT EXISTS '+  value +' (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, link TEXT NOT NULL, title TEXT NOT NULL, author TEXT NOT NULL, pubdate TEXT NOT NULL, imgLink TEXT NULL, desc TEXT NOT NULL, day TEXT NOT NULL);');
                        });
                    });
                }
            }catch(e){
                if(e == INVALID_STATE_ERR){
                    console.log('invalid database version.');
                }else{
                    console.log('unknown error ' + e + '.');
                }
                return;
            }


For what you need this? If you worry about that you can recreate table that already exist in your database, you need creating your table with this SQL query:

CREATE TABLE IF NOT EXISTS table_name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜