我编写了一个代码,将记录的值插入sqlite数据库并显示它.
现在我想做的是检查我插入的记录是否存在?
如果记录存在则更新它,否则创建一个新记录.
我怎么能在PhoneGap中做到这一点?以下是我尝试过的代码:
<!DOCTYPE HTML><HTML> <head> <Title>Contact Example</Title> <script type="text/JavaScript" charset="utf-8" src="cordova-1.5.0.Js"></script> <script src="http://AJAX.GoogleAPIs.com/AJAX/libs/jquery/1.5.2/jquery.min.Js"></script> <script type="text/JavaScript" charset="utf-8"> //add Listener when device ready document.addEventListener("deviceready", onDeviceReady, false); var db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 200000); //will create database Dummy_DB or open it //function will be called when device ready function insertdata(){ //alert("hi"); var name = $('#name').val(); var passwd = $('#pwd').val(); if(name == "") { alert("Please enter name"); } if(passwd == "") { alert("Please enter Password"); } if(name != "" && passwd != ""){ db.transaction(populateDB, errorCB, successCB); } } function onDeviceReady(){ document.getElementByID("submit").addEventListener("click", insertdata, false); } //create table and insert some record function populateDB(tx) { tx.executesql('CREATE table IF NOT EXISTS Login (ID INTEGER PRIMARY KEY @R_404_6843@INCREMENT, name TEXT NOT NulL, Password TEXT NOT NulL)'); //tx.executesql('INSERT INTO SoccerPlayer(name,Club) VALUES ("Alexandre Pato", "AC Milan")'); // tx.executesql('INSERT INTO SoccerPlayer(name,Club) VALUES ("Van PersIE", "Arsenal")'); //tx.executesql('DELETE FROM SoccerPlayer'); var name = $('#name').val(); var passwd = $('#pwd').val(); tx.executesql('INSERT INTO Login (name,Password) VALUES ("'+name+'","'+ passwd +'")'); $('#name').val(''); $('#pwd').val(''); } //function will be called when an error occurred function errorCB(err) { // alert("Error processing sql: "+err.code); } //function will be called when process succeed function successCB() { alert("success!"); db.transaction(queryDB,errorCB); } //select all from SoccerPlayer function queryDB(tx){ tx.executesql('SELECT * FROM Login',[],querySuccess,errorCB); } function querySuccess(tx,result){ $('#SoccerPlayerList').empty(); $.each(result.rows,function(index){ var row = result.rows.item(index); $('#SoccerPlayerList').append('<li><a href="#"><h3 >'+row['name']+'</h3></a></li>'); }); $('#SoccerPlayerList').ListvIEw(); } </script> </head> <body> <div data-role="page"> <div data-role="header" data-position="fixed" data-theme="b"> <h1>Login</h1> </div> <div>name: <input type="text" ID="name"></div> <div>Password: <input type="password" ID="pwd"></div> <input type="submit" ID="submit"> <div data-role="content"> <ul ID="SoccerPlayerList"> </ul> </div> </div> </body></HTML>
解决方法:
通常,您必须搜索记录:
SELECT 1 FROM Login WHERE name = '...'
当你得到结果时,检查你是否有东西,并相应地进行INSERT或UPDATE:
INSERT INTO Login(name, Password) VALUES('...', '...')-- orUPDATE Login SET Password = '...' WHERE name = '...'
但是,sqlite有一个扩展,允许您在插入记录时替换记录否则会产生冲突.这要求您具有UNIQUE约束,以便可以检测到冲突:
CREATE table Login(..., name TEXT NOT NulL UNIQUE, ...)
然后,您可以使用以下命令:
INSERT OR REPLACE INTO Login(name, Password) VALUES('...', '...')
请注意,您永远不应该尝试将字符串直接放入sql命令以避免格式化问题(如果密码包含“?”和SQL injection攻击怎么办?)Bobby Tables尝试使用您的应用程序时会发生什么?
相反,使用参数;这样的事情:
db.transaction(function(tx) { var name = ...; var passwd = ...; tx.executesql('INSERT OR REPLACE INTO Login(name, Password) VALUES(?, ?)', [name, passwd]);});
总结 以上是内存溢出为你收集整理的android – 检查记录是否存在,如果存在则更新它,否则在PhoneGap中创建新记录?全部内容,希望文章能够帮你解决android – 检查记录是否存在,如果存在则更新它,否则在PhoneGap中创建新记录?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)