16225

values not inserting in sqlite database using cordova

Question:

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..

<strong>Here is my coding</strong>..

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); });

Tried saving and importing the database from <strong>fileexprorer->data->data->projectpackage->databases->mydb.db</strong>

then <strong>firefox->sqlitemanager->connectdb</strong>

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

Answer1:

can insert the values:

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

Where

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();

Answer2:

<a href="http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html" rel="nofollow">http://docs.phonegap.com/en/1.2.0/phonegap_storage_storage.md.html</a> check the above link for db storage queries. Try with the below code . Hope this will help you.

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script type="text/javascript" charset="utf-8" src="cordova.js"></script> <script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); var nameval,pwdval,fnameval,lnameval,hobbyval,emailval; function onDeviceReady() { var db = window.openDatabase("Logindetails_DB", "1.0", "Logindetails_DB", 100000); db.transaction(populateDB, errorCB, successCB); } function validationCheck(){ nameval=document.getElementById('u_name').value; pwdval=document.getElementById('u_pwd').value; fnameval=document.getElementById('u_firstname').value; lnameval=document.getElementById('u_lastname').value; hobbyval=document.getElementById('u_hobby').value; emailval=document.getElementById('u_email').value; if(document.getElementById('u_name').value==""){ alert('Enter Your Name'); } if(document.getElementById('u_pwd').value==""){ alert('Enter Your Password'); } if(document.getElementById('u_firstname').value==""){ alert('Enter Your FirstName'); } if(document.getElementById('u_lastname').value==""){ alert('Enter Your LastName'); } if(document.getElementById('u_hobby').value==""){ alert('Enter Your HobbyName'); } if(document.getElementById('u_email').value==""){ alert('Enter Your Email'); } if(nameval!='' && pwdval!='' && fnameval!='' && lnameval!='' && hobbyval!='' && emailval!=''){ alert('Name'+nameval+'Pwd'+pwdval); Insertvalue(); } } function Insertvalue(){ var db = window.openDatabase("Logindetails_DB", "1.0", "Logindetails_DB", 100000); db.transaction(getregistdata, transaction_error); } function getregistdata(tx){ tx.executeSql('INSERT INTO login (username, password, firstname,lastname,hobby,email) VALUES ("'+nameval+'", "'+pwdval+'","'+fnameval+'","'+lnameval+'","'+hobbyval+'","'+emailval+'")'); alert('Record Inserted Successfully'); } function transaction_error(tx, error) { alert("Database Error: " + error); } // Populate the database function populateDB(tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS login(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, password TEXT NOT NULL, firstname TEXT NOT NULL,lastname TEXT NOT NULL,hobby TEXT NOT NULL,email TEXT NOT NULL)'); } // Transaction error callback // function errorCB(tx, err) { alert("Error processing SQL: "+err); } // Transaction success callback // function successCB() { alert("success!"); } </script> </head> <body>

Hello PhoneGap

<form name="form1" id="form1" method="post"> User Name: <br><input type="text" name="u_name" id="u_name"><br><br> Password:<br><input type="text" name="u_pwd" id="u_pwd"><br><br> First Name<br> <input type="text" name="u_firstname" id="u_firstname"><br><br> Last Name<br> <input type="text" name="u_lastname" id="u_lastname"><br><br> Hobby<br> <input type="text" name="u_hobby" id="u_hobby"><br><br> Email<br> <input type="text" name="u_email" id="u_email"><br><br> <div style="text-align:center;"> <input type="button" name="submitbtn" id="submitbtn" value="Submit" onclick="validationCheck()"> </div> </form> </body> </html>

Recommend

  • How can I format the text inside to create a bounding box/ tag system like gmail, SO
  • How to Convert a CCSprite image to UIImage?
  • Spring Social Facebook UncategorizedApiException: (#3) Application does not have the capability to m
  • javax.persistence.EntityManager.getCriteriaBuilder()Ljavax/persistence/criteria/CriteriaBuilder
  • Unable to send a &str between threads because it does not live long enough
  • How to transform this native SQL query to an HQL
  • RS-232 communication using an Arduino Duemilanove and the Cutedigi RS-232 interface
  • Aspect not executed in Spring
  • pure javascript dom dynamic insert, update and delete
  • Grails Cannot get property 'id' on null object
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • Issue with SVN Commit for certain File Extension
  • Java: java.util.ConcurrentModificationException
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • Appium MobileElement swipe returns unknown server error
  • How to resolve permission denied maybe missing internet permission?
  • Building Qt project for C++11 standard
  • Spring: No transaction manager has been configured
  • Invalid object name 'dbo.Item'
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • Code in Job's Script Block after Start-Process Does not Execute
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Deserializing XML into class C#
  • Google cloud sdk not working when python points python3
  • Is there a mandatory requirement to switch app.yaml?
  • How to include full .NET prerequisite for Wix Burn installer
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize