简体   繁体   English

从sqlite数据库读取信息,语法? 如何在html5 webapp中使用它?

[英]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.

相关问题 在 HTML5 JavaScript 中,如何从 Sqlite 数据库中读取 BLOB 字段并将其用作我的 Z25267A5EC93705EB8 应用程序中的内容? - In HTML5 JavaScript, how do I read a BLOB field from an Sqlite database and use it as content in my web app? 如何在HTML5中使用现有的sqlite数据库 - How to use an existing sqlite database in html5 如何将表单信息存储在html5 sql数据库中? - How to store form info in a html5 sql database? 如何使用HTML5 Boilerplate中的main.js和plugins.js? - How do I use main.js and plugins.js from HTML5 Boilerplate? 在HTML5中,如何直接返回SQLite调用的结果,而不是将它们交给函数? - In HTML5, how do I return the results of an SQLite call directly, rather than handing them to a function? 如何重新聚焦创建HTML5通知的窗口? - How do I refocus the window that a HTML5 notification is created from? 如何防止 HTML5 游戏暂停? - How do I prevent HTML5 game from suspending? 如何使用HTML文本区域中的信息设置JavaScript变量? - How do I set a JavaScript variable with info from an HTML textarea? 如何从JavaScript获取HTML5视频的编解码信息? - How obtain codec info of an HTML5 video from JavaScript? 在HTML5 sqlite数据库中存储字节数组 - Storing byte array in HTML5 sqlite database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM