无法创建SQL-DB

时间:2012-10-27 15:13:16

标签: javascript ios sql sqlite cordova

无法创建数据库。我使用Xcode 4.4,最新的PhoneGap版本。

通常在User /.../ iPhone模拟器/.../ Documents / tapp.sql中应该有一个数据库。但没有什么。我来到了警报点(" 2")。有什么想法吗?

我的代码:

        <script type="text/javascript" src="cordova-2.1.0.js"></script>
        <script type="text/javascript" charset="utf-8">
            function onBodyLoad()
            {
                alert("0");
               document.addEventListener("deviceready", onDeviceReady,false);
            }                    

            /* When this function is called, PhoneGap has been initialized and is ready to roll */
            function onDeviceReady()
            {
                alert("1");
                try {
                  alert("2");
                        var shortName = 'tapp';
                        var version = '1.0';
                        var displayName = 'Tumblr App Database';
                        var maxSize = 65536;
                        db = openDatabase(shortName, version, displayName,  maxSize);
                } catch (e) {
                    debug.error(e);
                }
            }
            </script>
        </head>
<body onLoad="onBodyLoad();" >



    <button onclick="createDatabase();">Database</button>
    <button onclick="createTables();">Tables</button>
</body>

2 个答案:

答案 0 :(得分:1)

要确保实际创建文件,请在数据库中写入内容。

答案 1 :(得分:0)

试试这个

 <script type="text/javascript" charset="utf-8">

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
    db.transaction(populateDB, errorCB, successCB);
}

// Populate the database 
//
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")');
}

// Transaction error callback
//
function errorCB(tx, err) {
    alert("Error processing SQL: "+err);
}

// Transaction success callback
//
function successCB() {
    alert("success!");
}

</script>

请参阅此链接:Phonegap- storage