[英]Reading info from sqlite database, Syntax? How do I use it in html5 webapp?
I have a webapp that I'm building, and have just started with SQLite. 我有一个正在构建的Webapp,并且刚开始使用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. 我已经能够创建表单,打开我创建的数据库,创建表和所需的字段以及在字段中输入数据。
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. 现在,我尝试使用SELECT语句读回数据,以将其显示在屏幕上以及列的列表中。 I just don't know the syntax for the SELECT statemnt in javascript or HTML5 beyond 我只是不知道javascript或HTML5中的SELECT statemnt的语法
'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. 非常感谢您的任何帮助...这是针对客户端DB,而不是连接到Web的用户。
thanks.... 谢谢....
See the spec and this Apple tutorial . 请参阅规格和本Apple教程 。 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. 另外,您应该传递一个空数组(或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/ . 编辑:我在http://jsfiddle.net/WcV6Y/7/上做了一个工作演示。 It's tested in Chrome 5.0.375.70. 已在Chrome 5.0.375.70中测试。
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.