Phonegap - 获取数据库是否已存在

时间:2022-09-25 23:55:34

I'm using database with Phonegap. I need to know if the database has already been created. I'm trying to avoid the whole creating tables and inserting rows function if the db already exists.

我正在使用Phonegap数据库。我需要知道数据库是否已经创建。我试图避免整个创建表和插入行功能,如果数据库已经存在。

var db = window.openDatabase("Database", "1.0", "FiltersResults", 50000000);

var db = window.openDatabase(“Database”,“1.0”,“FiltersResults”,50000000);

This opens/creates my db, ok, but how do I know if this database already exists?

这打开/创建我的数据库,确定,但我怎么知道这个数据库是否已经存在?

2 个解决方案

#1


0  

The var db = window.openDatabase("myDB", "1.0", "FiltersResults", 50000000); checks if you have a database called myDB if it exists it opens it. If not, it creates one and opens it. So if you are seeing it creating multiple Databases, below can be two reasons I can think of

var db = window.openDatabase(“myDB”,“1.0”,“FiltersResults”,50000000);检查你是否有一个名为myDB的数据库,如果它存在,它会打开它。如果没有,它会创建一个并打开它。因此,如果您看到它创建多个数据库,下面可能是我能想到的两个原因

  • You have more that one window running that url, just Quit the browser completely and open one widow with the URL
  • 你有更多的窗口运行该URL,只需完全退出浏览器并打开一个带URL的寡妇
  • was a knowing UI related bug introduced in Safari 5.1.2, fixed in later versions.
  • 是一个知识的UI相关错误在Safari 5.1.2中引入,在更高版本中修复。

#2


-1  

Check the documentation: http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#Storage

查看文档:http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#Storage

function populateDB(tx) {
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

function successCB() {
    alert("success!");
}

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);

#1


0  

The var db = window.openDatabase("myDB", "1.0", "FiltersResults", 50000000); checks if you have a database called myDB if it exists it opens it. If not, it creates one and opens it. So if you are seeing it creating multiple Databases, below can be two reasons I can think of

var db = window.openDatabase(“myDB”,“1.0”,“FiltersResults”,50000000);检查你是否有一个名为myDB的数据库,如果它存在,它会打开它。如果没有,它会创建一个并打开它。因此,如果您看到它创建多个数据库,下面可能是我能想到的两个原因

  • You have more that one window running that url, just Quit the browser completely and open one widow with the URL
  • 你有更多的窗口运行该URL,只需完全退出浏览器并打开一个带URL的寡妇
  • was a knowing UI related bug introduced in Safari 5.1.2, fixed in later versions.
  • 是一个知识的UI相关错误在Safari 5.1.2中引入,在更高版本中修复。

#2


-1  

Check the documentation: http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#Storage

查看文档:http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#Storage

function populateDB(tx) {
    tx.executeSql('DROP TABLE IF EXISTS DEMO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
    tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
}

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

function successCB() {
    alert("success!");
}

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(populateDB, errorCB, successCB);