HTML5SQL使用SELECT语句从Web数据库中检索记录 [英] HTML5SQL retrieve records from web databases using SELECT statements
问题描述
我正在使用jquery mobile / phonegap创建一个iphone webapp,我正在尝试使用Web数据库。我有大约700条记录要插入,并希望有一个单独的文件与sql语句一起创建表并在应用程序运行时插入记录。我决定使用一个名为 html5sql 的小型javascript模块,因为它为我提供了在一个中执行多个sql语句的优势单笔交易。
I am creating an iphone webapp using jquery mobile/phonegap and I am trying to use web database. I have about 700 records to insert and want to have a separate file with the sql statements to create the table and insert the records the fist time the app runs. I decided to use an small javascript module called html5sql because It gives me the advantage of executing several sql statements in one single transaction.
例如我可以执行
sql = "CREATE TABLE milestones (
ID INT( 10 ) NOT NULL,
Title VARCHAR( 50 ) DEFAULT NULL,
mYear INT( 11 ) NOT NULL);
INSERT INTO `milestones` VALUES ('2', 'Cotton Mather', '1721');";
使用
html5sql.process(sql, function(){ //Success}, function(error, failingQuery){//Failure});
而不是
transaction.executeSql('CREATE TABLE milestones (ID INT( 10 ) NOT NULL, Title VARCHAR(50 ) DEFAULT NULL, mYear INT( 11 ) NOT NULL);', [], nullDataHandler, errorHandler);
transaction.executeSql('INSERT INTO [milestones] ([ID], [Title], [mYear]) VALUES (2, "Cotton Mather", "1721");', [], nullDataHandler, errorHandler);
htlm5sql的问题是它们没有关于如何从数据库中检索信息的任何信息使用process函数和SELECT语句并在div块中显示结果。
The problem of htlm5sql is that they don't have any information on how to retrieve information from the database using the process function and SELECT statements and display the results in a div block.
任何人都可以帮我吗?
推荐答案
当然。使用html5sql选择数据非常简单,可以通过以下简单方法完成:
Sure. Selecting data with html5sql is really quite simple and can be accomplished with something as simple as:
html5sql.process(
["SELECT * FROM milestones;"],
function(transaction, results, rowsArray){
for(var i = 0; i < rowsArray.length; i++){
//each row in the rowsArray represents a row retrieved from the database
var id = rowsArray[i].ID;
var Title = rowsArray[i].Title;
var mYear = rowsArray[i].mYear;
console.log("Retrieved Milestone: "+id+" - "+Title+" "+mYear);
}
},
function(error, statement){
//hande error here
}
);
如果你想要一个更深入的例子,我创建了一个 jsfiddle 一段时间后面的实际工作代码显示了创建和填充数据库然后选择和检索数据的整个过程。
If you want a more in-depth example I created a jsfiddle a while back which has actual working code showing the whole process of creating and populating a database and then selecting and retrieving data.
作为html5sql的作者,我为缺乏文档而道歉。我正在努力改进网站上的文档,并添加更多关于如何做事的实时示例。
As the author of html5sql I apologize for the lack of documentation. I am working to improve the documentation on the site and add more live examples of how to do things.
这篇关于HTML5SQL使用SELECT语句从Web数据库中检索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!