How to Access an SQLite Database from JavaScript

Is it possible to access an SQLite database from JavaScript?

Actually the answer is yes. Here is an example how you can do this: http://html5doctor.com/introducing-web-sql-databases/

The bad thing is that it's with very limited support by the browsers.

More information here HTML5 IndexedDB, Web SQL Database and browser wars

PS: As @Christoph said Web SQL is no longer in active maintenance and the Web Applications Working Group does not intend to maintain it further so look here https://developer.mozilla.org/en-US/docs/IndexedDB.

SQL.js

EDIT

As @clentfort said, you can access SQLite database with client-side JavaScript by using SQL.js.

SQLite database in Javascript locally

This script will help you:

<script type="text/javascript">
function createDatabase(){
try{
if(window.openDatabase){
var shortName = 'db_xyz';
var version = '1.0';
var displayName = 'Display Information';
var maxSize = 65536; // in bytes
db = openDatabase(shortName, version, displayName, maxSize);
}
}catch(e){
alert(e);
}
}
function executeQuery($query,callback){
try{
if(window.openDatabase){
db.transaction(
function(tx){
tx.executeSql($query,[],function(tx,result){
if(typeof(callback) == "function"){
callback(result);
}else{
if(callback != undefined){
eval(callback+"(result)");
}
}
},function(tx,error){});
});
return rslt;
}
}catch(e){}
}
function createTable(){
var sql = 'drop table image';
executeQuery(sql);
var sqlC = 'CREATE TABLE image (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, image BLOB )';
executeQuery(sqlC);
}
function insertValue(){
var img = document.getElementById('image');
var sql = 'insert into image (name,image) VALUES ("sujeet","'+img+'")';
executeQuery(sql,function(results){alert(results)});
}
<input type="button" name='create' onClick="createDatabase()" value='Create Database'>
<input type="button" name='create' onClick="createTable()" value='create table'>
<input type="button" name='insert' onClick="insertValue()" value='Insert value'>
<input type="button" name='select' onClick="showTable()" value='show table'>
<input type="file" id="image" >
<div result></div>
</script>

To download the code go visit url:

http://blog.developeronhire.com/create-sqlite-table-insert-into-sqlite-table/

How to get sqlite database Values in javascript

finally i found the solution

var fName =  db.exec("SELECT First_Name FROM profile_id")

this code works

What's the best way to read Sqlite3 directly in Browser using Javascript?

I can not tell the best, but one: Write a JavaScript SQLite reader library yourself. This will be a tedious task, but I am sure it can be done. Some cool folks have done pdf.js, which is a JavaScript renderer for PDF files, which are also binary BLOB's like SQLite files are.

You will most probably start with the FileReader API to walk thru the SQLite file, then create some in-memory representation of the content, which your chart tool can use.

Disclaimer: You probably want to solve your initial problem with another solution, as proposed by others, but this answers your question.

Accessing sqlite databases on remote machine with static adress (Electron / Nodejs / fs)

as suggested by GregHNZ var dbPath = "//192.168.5.15/sqlite/db.sqlite" worked, so just used double slash.

Thanks GregHNZ

how to access Sqlite database with javascript?

Well, if you are working on client side JavaScript, I think you will
be out of luck... browsers tend to sandbox the JavaScript environment
so you don't have access to the machine in any kind of general
capacity like accessing a database.


If you are talking about an SQLite DB on the server end accessed from
the client end, you could set up an AJAX solution that invokes some
server side code to access it.


If you are talking about Rhino or some other server side JavaScript,
you should look into the host language's API access into SQLite (such
as the JDBC for Rhino).


credit: SO Question:
JavaScript sqlite

How to access the database using ajax and php ?

  • http://www.tizag.com/ajaxTutorial/ajax-mysql-database.php
  • http://www.tutorialspoint.com/ajax/ajax_database.htm

Read SQLite database with Node.js

Try connecting to db like this.

let db = new sqlite3.Database('./ocs_athlete.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the my database.');
});

Give path of .db file. This will work.

There are three opening modes:

sqlite3.OPEN_READONLY: open the database for read-only.

sqlite3.OPEN_READWRITE : open the database for reading and writting.

sqlite3.OPEN_CREATE: open the database, if the database does not exist, create a new database.

To open the chinook sample database for read and write, you can do it as follows:

let db = new sqlite3.Database('./ocs_athlete.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the ocs_athlete database.');
});

JavaScript sqlite

Well, if you are working on client side JavaScript, I think you will be out of luck... browsers tend to sandbox the JavaScript environment so you don't have access to the machine in any kind of general capacity like accessing a database.

If you are talking about an SQLite DB on the server end accessed from the client end, you could set up an AJAX solution that invokes some server side code to access it.

If you are talking about Rhino or some other server side JavaScript, you should look into the host language's API access into SQLite (such as the JDBC for Rhino).

Perhaps clarify your question a bit more...?



Related Topics



Leave a reply



Submit