Cordova:Sqlite不存储我的数据

时间:2014-05-27 04:53:16

标签: javascript sqlite cordova

我是混合移动应用程序开发的新手。我正在构建一个简单的应用程序,我想注册用户并使用相同的凭据登录。我面临的问题是,当我在sqlite数据库中插入记录时,它不存储我的数据。如果我重新启动我的应用程序,它将不允许我使用以前插入的信息。 我使用Cordova进行原生包装,使用Graphite作为我的IDE,使用sqlite作为我的数据库。

这是我的register.js文件

var db;
var dbCreated = false;

var scroll = new iScroll('wrapper', {
 vScrollbar : false,
 hScrollbar : false,
 hScroll : false
});

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    var fname = document.getElementById("fname").value;
    var lname = document.getElementById("lname").value;
    var age = document.getElementById("age").value;
    var username = document.getElementById("username").value;
    var password = document.getElementById("psw").value;

    db = window.openDatabase("RegistrationDB", "1.0", "Registration", 200000);

    if (dbCreated){

    } else{
        db.transaction(populateDB, transaction_error, populateDB_success);
    }

}

function populateDB(tx) {

 var sql = "CREATE TABLE IF NOT EXISTS Registration ( "
   + "firstName VARCHAR(50), " + "lastName VARCHAR(50), "
   + "age INTEGER, " + "username VARCHAR(50), "
   + "password VARCHAR(200))";
 tx.executeSql(sql);
 var fname = document.getElementById("fname").value;
 var lname =  document.getElementById("lname").value;
 var age = document.getElementById("age").value;
 var uname =document.getElementById("username").value;
 var pwrd = document.getElementById("psw").value;
 tx.executeSql("INSERT INTO Registration (firstname,lastname,age,username,password) VALUES ('"+ fname +"','"+ lname +"' , "+ age+", '"+ uname +"','"+ pwrd +"' )");

}

function transaction_error(tx, error) {
 alert("Database Error: " + error);
}

function populateDB_success() {
 dbCreated = true;

 alert("Successfully inserted");
  //window.location="file:///MyApp/MyApp/login.html"
    window.location = "./login.html"
}

    var db;
var dbCreated = false;

var scroll = new iScroll('wrapper', {
 vScrollbar : false,
 hScrollbar : false,
 hScroll : false
});

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady(){
    var fname = document.getElementById("fname").value;
    var lname = document.getElementById("lname").value;
    var age = document.getElementById("age").value;
    var username = document.getElementById("username").value;
    var password = document.getElementById("psw").value;

    db = window.openDatabase("RegistrationDB", "1.0", "Registration", 200000);

    if (dbCreated){

    } else{
        db.transaction(populateDB, transaction_error, populateDB_success);
    }

}

function populateDB(tx) {

 var sql = "CREATE TABLE IF NOT EXISTS Registration ( "
   + "firstName VARCHAR(50), " + "lastName VARCHAR(50), "
   + "age INTEGER, " + "username VARCHAR(50), "
   + "password VARCHAR(200))";
 tx.executeSql(sql);
 var fname = document.getElementById("fname").value;
 var lname =  document.getElementById("lname").value;
 var age = document.getElementById("age").value;
 var uname =document.getElementById("username").value;
 var pwrd = document.getElementById("psw").value;
 tx.executeSql("INSERT INTO Registration (firstname,lastname,age,username,password) VALUES ('"+ fname +"','"+ lname +"' , "+ age+", '"+ uname +"','"+ pwrd +"' )");

}

function transaction_error(tx, error) {
 alert("Database Error: " + error);
}

function populateDB_success() {
 dbCreated = true;

 alert("Successfully inserted");
  //window.location="file:///MyApp/MyApp/login.html"
    window.location = "./login.html"
}

这是我的login.js

var db;
var dbCreated = false;

var scroll = new iScroll('wrapper', {
 vScrollbar : false,
 hScrollbar : false,
 hScroll : false
});
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
 db = window.openDatabase("RegistrationDB", "1.0", "Registration", 200000);
 if (dbCreated)
  {
      // created
  }

 else{
 db.transaction(getregistdata, transaction_error);
 }
}

function getregistdata(tx){

  var sql = "select username, password from Registration";
  tx.executeSql(sql, [], getlogin_success);
}

function transaction_error(tx, error) {
 alert("Database Error: " + error);
}

function getlogin_success(tx, results){
   var len = results.rows.length;
   for (var i=0; i< len; i++) { 
    var employee = results.rows.item(i);
    var username=document. getElementById("username").value;
    var password=document. getElementById("psw").value;
    var uname=employee.username;
    var pasw=employee.password;
    alert(username);
    alert(password);
    if(username == uname && password == pasw){
     alert("Login Success");
     break;
    }
    else{
        var status=1;
     }
   }

   if(status == 1)
    {
       alert("login failed");
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用window.sqlitePlugin.openDatabase而不是window.openDatabase

db = window.sqlitePlugin.openDatabase({name: 'RegistrationDB', location: 'default'});

并确保已安装带有cordova的sqlite插件