golddragon007
golddragon007

Reputation: 1332

How does sqlite work with javascript in Firefox add-on?

How can I use sqlite? What is the command when I want to use this:

SELECT * FROM table

? (I mean like PHP I need to use mysql_query("SELECT * FROM table");)

What is the command when I get a table with records and I want to dispart it? (in PHP: mysql_fetch_assoc() or mysql_fetch_array())

I used SELECT * FROM table in the table var and I dispart it. (The table has these columns: id, name, pass) How can I get the column values?

This is the connection in the Javascript to the database:

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

let file = FileUtils.getFile("ProfD", ["my_db_file_name.sqlite"]);
let mDBConn = Services.storage.openDatabase(file);

How can I create table (if it doesn't exist), how can I SELECT, UPDATE, DELETE, ect.

How can I do this with javascript and sqlite: example (its wrote in php, mysql):

mysql_connect("localhost","user","pass"); // pass if need
mysql_select_db("my_database");

mysql_query("CREATE TABLE `my_database`.`table` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`nev` VARCHAR( 50 ) NOT NULL ,`pass` VARCHAR( 20 ) NOT NULL ,INDEX ( `nev` )) ENGINE = InnoDB HARACTER SET utf8 COLLATE utf8_unicode_ci;");

mysql_query("INSERT INTO table (name,pass) VALUES ('Peter','sdf')");

$query=mysql_query("SELECT * FROM table");
while($row=mysql_fetch_array($query)){
    echo $row['name'].' '.$row['pass']; // it write all record, like 'name pass' ('Peter sdf')
}

$row2=mysql_fetch_assoc(mysql_query("SELECT * FROM table WHERE id=1"));
echo $row2['name'].' '.$row2['pass']; // it write only 'Peter sdf'

mysql_query("DELETE FROM table WHERE id=1");

mysql_query("DROP TABLE 'table'");

mysql_close();

How can I do this in javascript with sqlite?

Upvotes: 0

Views: 3029

Answers (2)

Sirozha
Sirozha

Reputation: 126

There is an 3rd party lib to use sqlite with firefox addon API:
https://github.com/julianceballos/sqlite-jetpack

In fact the library is just a simple wrapper (look at the source code).

Upvotes: 1

Matthew Gertner
Matthew Gertner

Reputation: 4537

You should use the Mozilla Storage API for working with SQLite databases. See https://developer.mozilla.org/En/Storage.

Upvotes: 1

Related Questions