花了几个小时但找不到代码有什么问题。
如果不退出,代码只会创建一个表,并添加在文本框中键入的文本。在每次加载时,它都会加载这些并写入列表。
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var mydb = null;
function initDatabase() {
try {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
} else {
var shortName = 'mydb';
var version = '1.0';
var displayName = 'sample db';
var maxSize = 1024*1024*2;
mydb = openDatabase(shortName, version, displayName, maxSize, null);
mydb.transaction(
function (transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS sampletable(term TEXT NOT NULL);', [], nullDataHandler, errorHandler);
}
);
loadDB();
}
} catch(e) {
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
}
function loadDB() {
$(".debugger").html("");
mydb.transaction(
function (t) {
t.executeSql("SELECT * FROM sampletable", [],dataSelectHandler,errorHandler);
}
);
}
function saveDB(_term) {
mydb.transaction(
function (transaction) {
transaction.executeSql("INSERT INTO sampletable(_term) VALUES (\""+_term + "\");");
}
);
}
function dataSelectHandler(transaction, results){
$(".debugger").append("<li>Length: " + results.rows.length + "</li>");
for (var i=0; i<results.rows.length; i++)
{
var row = results.rows.item(i);
addToHistoryUI(row['_term']);
}
}
function errorHandler(transaction, error){
if (error.code==1){
alert("DB Table already exists");
} else {
// Error is a human-readable string.
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
}
return false;
}
function nullDataHandler(){
//alert("SQL Query Succeeded");
}
function addToHistoryUI(_term)
{
var newH = "<li>" + _term + "</li>";
$(".addedQueries").append(newH);
}
$(function () {
initDatabase();
});
$(document).ready(function () {
$(".addBtn").click(function () {
var _term = $("#query").val();
addToHistoryUI(_term);
saveDB(_term);
loadDB();
});
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div class="panel">
<input id="query" type="text" name="query" />
<a class="navigationLink addBtn" href="#">add</a>
</div>
<div class="content">
<ul class="addedQueries">
</ul>
</div>
<ul class="debugger"></ul>
</div>
</body>
</html>
答案 0 :(得分:0)
在saveDB()方法中,您似乎试图在WHERE子句中使用双引号。我是对的吗?
双引号不会在SQL中运行。
我不确定为什么你的SELECT不能正常工作。请发布错误消息。
更新:你的INSERT也失败了,因为你有一个错字。
transaction.executeSql("INSERT INTO sampletable(_term) VALUES (\""+_term + "\");");
你有INTO sampleTable(_term),但是你没有名为_term的列,列名是term(没有下划线)。我做了那个改变,插入现在发生了。
你还有其他一些小事。这似乎做你想要的。
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var mydb = null;
function initDatabase() {
try {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
} else {
var shortName = 'mydb';
var version = '1.0';
var displayName = 'sample db';
var maxSize = 1024*1024*2;
mydb = openDatabase(shortName, version, displayName, maxSize, null);
mydb.transaction(
function (t) {
t.executeSql('CREATE TABLE IF NOT EXISTS sampletable(term TEXT NOT NULL);', [], nullDataHandler, errorHandler);
}
);
loadDB();
}
} catch(e) {
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}
return;
}
}
function loadDB() {
$(".debugger").html("");
$("li").remove();
mydb.readTransaction(
function (t) {
t.executeSql("SELECT term FROM sampletable", [],dataSelectHandler,errorHandler);
}
);
}
function saveDB(_term) {
mydb.transaction(
function (transaction) {
transaction.executeSql("INSERT INTO sampletable(term) VALUES ('"+_term + "');");
}
);
}
function dataSelectHandler(t, results){
$(".debugger").append("<li>Length: " + results.rows.length + "</li>");
for (var i=0; i<results.rows.length; i++)
{
var row = results.rows.item(i);
addToHistoryUI(row['term']);
}
}
function errorHandler(transaction, error){
if (error.code==1){
alert("DB Table already exists");
} else {
// Error is a human-readable string.
alert('Oops. Error was '+error.message+' (Code '+error.code+')');
}
return false;
}
function nullDataHandler(){
//alert("SQL Query Succeeded");
}
function addToHistoryUI(_term)
{
var newH = "<li>" + _term + "</li>";
$(".addedQueries").append(newH);
}
$(function () {
initDatabase();
});
$(document).ready(function () {
$(".addBtn").click(function () {
var _term = $("#query").val();
addToHistoryUI(_term);
saveDB(_term);
loadDB();
});
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div class="panel">
<input id="query" type="text" name="query" />
<a class="navigationLink addBtn" href="#">add</a>
</div>
<div class="content">
<ul class="addedQueries">
</ul>
</div>
<ul class="debugger"></ul>
</div>
</body>
</html>