值没有使用科尔多瓦在SQLite数据库中插入 [英] values not inserting in sqlite database using cordova

查看:163
本文介绍了值没有使用科尔多瓦在SQLite数据库中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个形式从文本框插入数据到数据库中,当我在浏览器中运行它,我可以看到在创建表并插入值。

I have created a form for inserting data from textbox into the database and when I run it in the browser I can see the table is created and values are inserted.

我尝试使用越来越创建科尔多瓦和桌子,没有得到插入值,实现相同的。

I tried implementing the same using cordova and table getting created and values are not getting inserted..

这里是我的编码 ..

document.addEventListener("deviceready",  function()
{
var db=window.sqlitePlugin.openDatabase({name : "mydb"});
var createStatement = "CREATE TABLE IF NOT EXISTS login(id INTEGER PRIMARY KEY AUTOINCREMENT,username      TEXT,password TEXT,firstname TEXT, lastname TEXT,hobby  TEXT,email TEXT)";
var insertStatement = " INSERT INTO login(username,password,firstname,lastname,hobby,email) VALUES (?,?,?,?,?,? ) ";
function initDatabase()  // Function Call When Page is ready.

{

try {

    if (!window.openDatabase)  // Check browser is supported SQLite or not.

    {

       // alert('Databases are not supported in this browser.');

    }

    else {

        createTable();  // If supported then call Function for create table in SQLite

    }

 }

 catch (e) {

    if (e == 2) {

        // Version number mismatch. 

        console.log("Invalid database version.");

    } else {

        console.log("Unknown error " + e + ".");

    }

    return;

   }

   }


   function createTable() // Function Call When Page is ready.

   {


   var res=db.transaction(function (tx) { tx.executeSql(createStatement, [], onError); });

  alert("Successfully created the table");

   }
  function onError(tx, error) // Function for Hendeling Error...

   {

 // alert(error.message);

  }
  });
  document.addEventListener("deviceready",  function()
  {
  var db=window.sqlitePlugin.openDatabase({name : "mydb"});         

  function insertRecord()
  {
    var usernameold = $('input:text[id=username]').val();

    var passwordold = $('input:password[id=pass]').val();

     var firstnameold = $('input:text[id=firstname]').val();

    var lastnameold = $('input:text[id=lastname]').val();

    var hobbyold = $('input:text[id=hobby]').val();

    var emailold = $('input:text[id=email]').val();

    db.transaction(function (tx) { tx.executeSql(insertStatement,     [usernameold,passwordold,firstnameold, lastnameold,hobbyold,emailold]/*,loadAndReset, onError*/) });

    alert("Successfully created the table");
    }
   });

   $(document).ready(function()
   {
    initDatabase();
   $("#submit").click(insertRecord); 
   });

尝试在保存和导入数据库的 fileexprorer->数据 - >数据 - > projectpackage->数据库 - > mydb.db

firefox-> sqlitemanager-> connectdb

我可以看到是越来越创建表和值不会插入
PLZ帮助...

I can see the table is getting created and the values are not inserting Plz help...

推荐答案

可以插入值:

    db.transaction(function (tx) 
  { 
    tx.executeSql('INSERT INTO login (username,pass,firstname,lastname,hobby,email)VALUES("'+un+'","'+ps+'","'+fs+'","'+ls+'","'+hb+'","'+ed+'")');

  });
  });
  }

其中,

   var un= $("#username").val();

   var ps= $("#pass").val();

   var fs = $('input:text[id=firstname]').val();

   var ls = $('input:text[id=lastname]').val();

   var hb = $('input:text[id=hobby]').val();

   var ed = $('input:text[id=email]').val();

这篇关于值没有使用科尔多瓦在SQLite数据库中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆