gpt4 book ai didi

android - 检查记录是否存在,如果存在则更新它,否则在 PhoneGap 中创建新记录?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:05:22 28 4
gpt4 key购买 nike

我编写了一段代码,将一条记录的值插入 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 AUTOINCREMENT, 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 class="ui-li-heading">'+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 = '...'

当你得到结果时,检查你是否得到了一些东西,并相应地执行 INSERTUPDATE:

INSERT INTO Login(Name, Password) VALUES('...', '...')
-- or
UPDATE 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 中创建新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12971248/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com