Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript and Database Connectivity

Is it possible for javascript to access a database directly? I feel my question is rhetorical owing to the fact that this is a security issue. But is it possible anyway?

like image 539
Ashwin Krishnamurthy Avatar asked Apr 02 '11 07:04

Ashwin Krishnamurthy


4 Answers

It is possible!
with the new html5 feature, js can connect through WebSql. a live example : http://html5demos.com/database
the syntax is similar to all the other sql wrappers :

var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);
db.transaction(function (tx) {
    tx.executeSql('CREATE TABLE foo (id unique, text)');
});    

it is currently supported by chrome, safari and opera
here's a tutorial : http://html5doctor.com/introducing-web-sql-databases/

like image 173
gion_13 Avatar answered Oct 11 '22 22:10

gion_13


Is it possible for javascript to access a database directly?

No. Setup a server side script which will talk the database and then call this script with AJAX.

like image 38
Darin Dimitrov Avatar answered Oct 11 '22 22:10

Darin Dimitrov


Depends on what DB you want to use.

CouchDB is HTTP addressable, so can be hit from JS. http://couchdb.apache.org/

like image 28
Daniel Willis Avatar answered Oct 11 '22 22:10

Daniel Willis


Not from the browser. Javascript can be used on the server to set up server side functionality, though.

like image 38
picardo Avatar answered Oct 11 '22 21:10

picardo