2014-04-29 111 views
0

我試着用phonegap創建一個DB和表。我遵循phonegap文檔,但我做了一些錯誤,無法識別。任何人都可以意味着這對我有幫助。 我的代碼在這裏。Phonegap存儲不能正常工作

<!DOCTYPE html> 
<html> 
<head> 
<title>Daily Word</title> 
<link rel="stylesheet" href="../css/jquery.mobile-1.4.2.min.css"> 
<link rel="stylesheet" href="../css/style.css"> 
<script src="../js/jquery-1.9.1.js"></script> 
<script src="../js/cordova.js"></script> 
<script src="../js/jquery.mobile-1.4.2.min.js"></script> 
<script> 
    document.addEventListener("deviceready", onDeviceReady, false); 

var db; 
var user_type; 

function onDeviceReady() 
{ 
    alert("Device Ready"); 
    db = Window.openDatabase("DailyWord","1.0","DailyWord DB",2*1024*1024); 
    db.transaction(createDB,errorDB,successDB); 
    alert("DB Query fired..."); 
} 
//This will cereate the database 
function createDB(tx) 
{ 
    tx.executeSql('DROP TABLE IF EXISTS Login'); 
    tx.executeSql('CREATE TABLE IF NOT EXISTS Login (type)'); 
    tx.executeSql('CREATE TABLE IF NOT EXISTS DW_Words(id unique,status)'); 
    alert("DB Created");   
} 
//This will report the error in the db connection 
function errorDB(err) 
{ 
    alert("Error in creating DB"+err.code); 
} 
//This will report the success state of db connection 
function successDB() 
{ 
    alert("DB Created");  
} 
function insertUser(tx) 
{ 
    var sql='INSERT INTO Login(type) VALUES (?)'; 
    tx.executeSql(sql,[user_type],SuccessQueryDB,errorDB); 
} 
function SuccessQueryDB(tx) 
{ 
    alert("Query"); 
    tx.executeSql('SELECT * FROM Login',[],redirectUser,errorDB); 
} 
function redirectUser(tx,results) 
{ 
    //var rows=results.rows.length; 
    return "Success"; 
} 
function checkConnection() { 
    var networkState = navigator.connection.type; 
    var states = {}; 
    states[Connection.UNKNOWN] = 'Unknown connection'; 
    states[Connection.ETHERNET] = 'Ethernet connection'; 
    states[Connection.WIFI]  = 'WiFi connection'; 
    states[Connection.CELL_2G] = 'Cell 2G connection'; 
    states[Connection.CELL_3G] = 'Cell 3G connection'; 
    states[Connection.CELL_4G] = 'Cell 4G connection'; 
    states[Connection.CELL]  = 'Cell generic connection'; 
    states[Connection.NONE]  = 'No Connection'; 
    return states[networkState]; 
} 
</script> 
</head> 
<body> 
<div data-role="page" id="LoginPage"> 
    <div data-role="header" id="header"><h4>Daily Word</h4></div> 
    <div data-role="content"> 
    <form> 
    <fieldset> 
    <input type="text" name="Username" class="Username" id="text-basic" value="arockia" onfocus="if (this.value == 'Username') this.value = '';" onblur="if (this.value == '') this.value = 'Username';"> 
    <input type="password" name="Password" class="Password" id="text-basic" value="arulbhuvi" onfocus="if (this.value == 'Password') this.value = '';" onblur="if (this.value == '') this.value = 'Password';"> 
    <input type="button" value="Login" name="btnLogin" id="btnSignIn"> 
    <input type="reset" value="Reset" name="btnClear"> 
    <a href="register.html" data-role="button" id="SignUp">Sign Up</a> 
    </fieldset> 
    </form> 
    </div> 
    <div data-role="footer" data-position="fixed" id="footer" ><h4><a href="http://eazytutor.in/Mobile/">EazyTutor</a></h4></div> 
<script> 
$("#LoginPage").on('pageinit' , function (event) { 
    $("#btnSignIn").on('click', function(){ 
     var uname=$('.Username').val(); 
     var pwd=$('.Password').val(); 
       if(checkConnection() == "No Connection") 
       { 
        alert("Please check internet connection"); 
       } 
       else 
       {  
        $.get("http://eazytutor.in/DailyWord/login.php?Username="+uname+"&Password="+pwd,function(response){ 
        if(response == "Admin") 
         { 
          user_type=response; 
          db.transaction(insertUser,errorDB); 
          window.location.href ="Admin/admin_home.html"; 
         } 
        else if(response == "User") 
         { 
          user_type=response; 
          db.transaction(insertUser,errorDB); 
          window.location.href="User/index.html"; 
         } 
        else 
         { 
          alert(response); 
         } 
        }); 
       } 
    }); 
}); 
</script> 
</div> 
</body> 
</html> 

回答

1

您將要放置deviceready事件在一個被稱爲後的DOM完全加載功能。這應有助於:

<body onload="onLoad()"> 

function onLoad() { 
    document.addEventListener("deviceready", onDeviceReady, false); 
} 

這是PhoneGap的/科爾多瓦最佳做法,因爲它減輕了被稱爲前PhoneGap的是準備接受它們的方法的可能性。

ALSO:

這條線:

<script src="../js/cordova.js"></script> 

應該是:

<script src="cordova.js"></script> 

這是因爲當你運行構建命令的PhoneGap /科爾多瓦將增加這個文件,它會永遠被添加到項目根目錄。

+0

嗨道森。感謝您的輸入。我看到對象函數窗口沒有方法openDatabase ... – Arockia

+0

可能是這個'db = Window.openDatabase','Window'應該是'window'(全部小寫) –

+0

謝謝Dawson。它通過將Window更改爲窗口來工作。非常感謝... – Arockia