database:sql.js: Using SQLite Databases with Firefox OS Apps

sql.js: Using SQLite Databases with Firefox OS Apps

Developers coming from platforms like Android might seek for support for SQLite-like functionality in Firefox OS as well, but sadly the WebSQL specification was dropped and Firefox OS doesn’t have native SQLite support. However, you can still use SQLite through a JavaScript library, sql.js, which is SQLite compiled to JavaScript!

Warning: This approach might have performance penalties

Getting started with sql.js

Let’s first start with a blank fresh index.html with a basic template. Download sql.js from the link above (impatient ones can justclick here) and attach it as a script to your index.html. Which should look something like this.

index.html

XHTML

<!doctype html>
<html>
...
<body>
<script src="js/sql.js"></script>
</body>
</html>

<!doctype html>

<html>

...

<body>

<script src = "js/sql.js"> </script>

</body>

</html>

Now we have SQLite functionality available to our app! Now to be able to execute our own code, let’s add our very own JavaScript file to our app, I am willing to name it dbHelper.js, you can name it as you like, and again attach to index.html.

index.html

XHTML

<!doctype html>
<html>
...
<body>
<script src="js/sql.js"></script>
<script src="js/dbHelper.js"></script>
</body>
</html>

<!doctype html>

<html>

...

<body>

<script src = "js/sql.js"> </script>

<script src = "js/dbHelper.js"> </script>

</body>

</html>

We are all set to go, let’s dive in!

Creating the Database

Before we get started with populating tables with data to save the world from meteoroids, we need to create a database for those tables to be in.

dbHelper.js

JavaScript

var db = new SQL.Database();

var db = new SQL. Database ( ) ;

And there you have it, you have an SQLite database ready to interact with!

Executing Queries

Now that we have a database, we can execute some queries for populating data, making tables or anything SQLite is capable of.db.run(query)is the way to execute queries on your database if you don’t need any returns from the query.

dbHelper.js

JavaScript

var le_query = "CREATE TABLE Meteoroids (id int, name char, distance int);";
db.run(le_query);

var le_query = "CREATE TABLE Meteoroids (id int, name char, distance int);";

db . run ( le_query) ;

Or, if you love one-liners,

dbHelper.js

JavaScript

db.run("CREATE TABLE Meteoroids (id int, name char, distance int);");

db . run ( "CREATE TABLE Meteoroids (id int, name char, distance int);") ;

However, if you are expecting the query to return something, instead you should usedb.exec(query).

dbHelper.js

JavaScript

var result = db.exec("SELECT * from Meteoroids;");

var result = db .exec ( "SELECT * from Meteoroids;" );

Saving the database

You app might need to (and probably will need to) save that database somewhere for future use and you have a variety of choices to choose from. Before going anywhere else, exporting our database into anUint8Arrayis probably a good thing.

JavaScript

var binaryArray = db.export();

var binaryArray = db . export ( ) ;

Now we can make a Blob out of it to save it as a file on the device storage, or we can use localStorage/IndexedDB (or localForage.js, a fantastic library to handle complications for us).

JavaScript

var sqlFile = new Blob(binaryArray);

var sqlFile = new Blob( binaryArray ) ;

Opening the database from a Uint8Array

Again, after saving the database, opening it up will probably be required sometimes in the future. First, retrieve yourUint8Arrayfrom wherever and no matter how you stored it.

JavaScript

var binaryarray = this_is_sparta(); // somehow you get the Uint8Array

var binaryarray = this_is_sparta( ) ; // somehow you get the Uint8Array

And, getting our Database ready is as simple as,

JavaScript

var db = SQL.Database(binaryarray);

var db = SQL .Database ( binaryarray ) ;

And now that we are done with the very basics of sql.js, we can explore a bit more of what this library has to offer. Reading thedocumentationand example codewould now make some sense to you.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值