Javascript 从sqlite数据库读取信息,语法?如何在 html5 webapp 中使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3122057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading info from sqlite database, Syntax? How do I use it in html5 webapp?
提问by Mac-Gon
I have a webapp that I'm building, and have just started with SQLite. I have been able to create my form, open the database I created, create the table, and fields i need, and enter data into the fields.
我有一个正在构建的 web 应用程序,并且刚刚开始使用 SQLite。我已经能够创建我的表单、打开我创建的数据库、创建表和我需要的字段,并将数据输入到字段中。
Now I'm trying to read the data back out with a SELECT statement, to show it on screen and as a list of the columns. I just don't know the syntax for the SELECT statemnt in javascript or HTML5 beyond
现在我正在尝试使用 SELECT 语句读取数据,以将其显示在屏幕上并作为列列表显示。我只是不知道 JavaScript 或 HTML5 之外的 SELECT 语句的语法
'SELECT * FROM MyTable'...I know it can be done, just need some help with the syntax of getting the results onto the screen.
'SELECT * FROM MyTable'...我知道它可以完成,只是需要一些有关将结果显示在屏幕上的语法的帮助。
I start with this.
我从这个开始。
var db = window.openDatabase('TabOrder', '', 'Bar Tab Orders', 2500000);
function insertDrinks(category, drinkname, our_cost, cust_cost){
db.transaction(function(tx){
tx.executeSql('INSERT INTO Drinks (category, drinkname, our_cost, cust_cost) VALUES (?, ?, ?, ?)', [category, drinkname, out_cost, cust_cost]);
});
}
$(document).ready(function() {
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Drinks(id INTEGER PRIMARY KEY Autonumber, category TEXT, drinkname TEXT, our_cost FLOAT(6,2), cust_cost FLOAT(7,2))', []);
});
});
I later have this....
我后来有这个....
View Cat / Drink List
function readDrinks(id, category, drinkname, our_cost, cust_cost){
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM Drinks', [id, category, drinkname, our_cost, cust_cost]);
});
document.write(id, category + " are the categories.");
}
I just tried to piece it together, and have no idea what i'm doing with it beyond the basic SQL.
我只是试着把它拼凑起来,除了基本的 SQL 之外,我不知道我在做什么。
Any help is greatly appreciated...and this is for a client side DB, not one connecting to the web.
非常感谢任何帮助......这是针对客户端数据库的,而不是连接到网络的数据库。
thanks....
谢谢....
回答by Matthew Flaschen
See the specand this Apple tutorial. In short, you need to add data and error callbacks. Also, you should be passing an empty array (or null) because your query has no parameters.
请参阅规范和此Apple 教程。简而言之,您需要添加数据和错误回调。此外,您应该传递一个空数组(或 null),因为您的查询没有参数。
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM Drinks',
[],
function(tx, results)
{
// results is a http://dev.w3.org/html5/webdatabase/#sqlresultset .
// It has insertId, rowsAffected, and rows, which is
// essentially (not exactly) an array of arrays.
},
function(tx, error)
{
}
);
});
It's up to you whether to use named or anonymous functions.
使用命名函数还是匿名函数取决于您。
EDIT: I made a working demo at http://jsfiddle.net/WcV6Y/7/. It's tested in Chrome 5.0.375.70.
编辑:我在http://jsfiddle.net/WcV6Y/7/做了一个工作演示。它在 Chrome 5.0.375.70 中进行了测试。
回答by Kai CriticallyAcclaimed Cooper
try something like this
尝试这样的事情
tx.executeSql('SELECT * FROM foo', [], function (tx, results) {
var len = results.rows.length;
for (var i = 0; i < len; ++i) {
var obj = results.rows.item(i);
alert(obj);
}
});also see this for short tutorial http://html5doctor.com/introducing-web-sql-databases/
也可以看到这个简短的教程http://html5doctor.com/introducing-web-sql-databases/

