38


SQL (sqlite)
Sqld.js is a sqlite server build in jsdb. The parameters of the service are described in sqld.conf

sqld.conf
({base: "test.db3", port: 2020, pswd: "poi"})

base : name of the sqlite database
port : port to listen
pswd : password

Usage : jsdb sqld.js

Sample with telnet (server responses in blue):

telnet localhost 2020
PWD poi
PWD OK
SQL create table person (id integer, name varchar(20))
SQL OK
SQL insert into person (id,name) values (1,’ABC’)
SQL OK
SQL insert into person (id,name) values (2,’DEF’)
SQL OK
SQL insert into person (id,name) values (3,’GHI’)
SQL OK
SQL select count(*) from person
DTA count(*)=’3’
SQL OK
SQL select * from demo where id>1 order by name desc
DTA id='3',name='GHI'
DTA id='2',name='DEF'
SQL OK
CLS X
CLS OK

PWD <password> => PWD OK or PWD ERROR if the password is wrong
SQL <sql statement> => SQL OK or SQL ERROR if SQL statement is wrong
CLS X => close the connexion

Sample with the client library…

sql_demo.js
load('SQLC.js');

var sql=new SQLC('localhost',2020,'poi');
var res;
var ind;
if (sql.error=='') {
    writeln('Connected.');
    sql.exec('create table person(id interger,name varchar(10))');
    if (sql.error=='') {
        // Add records to the new table (error if table exists)
        sql.exec('insert into person (id,name) values (1,\'ABC\')');
        sql.exec('insert into person (id,name) values (2,\'DEF\')');
        sql.exec('insert into person (id,name) values (3,\'GHI\')');
    }
    else
        sql.error=''; // Reset create status
    // Count
    res=sql.exec('select count(*) from person');
    if (sql.error=='') {
        writeln('Done.');
        writeln('->',res[0].get('count(*)'));
    }
    else {
        writeln('Error.');
    }
    // Select and display
    res=sql.exec('select * from person');
    if (sql.error=='') {
        writeln('Done.');
        for(ind=0;ind<res.length;ind++) {
            writeln(res[ind].get('id'),'->',res[ind].get('name'));
        }
    }
    else
        writeln('Error.');
}
else
    writeln('Service unavailable.');

Usage : jsdb sql_demo.js
Fichiers : jsdb_sql.zip