server-side JavaScript: Example 2: Sqlite class

G

GVDC

Example server-side JavaScript Web script, Sqlite class

//demonstrate usage of SQLite3 database and JavaScript interface
// BEGIN CODE -->
printf("<html>");
printf("<body bgcolor=\"#ffffff\" color=\"#333333\">");

var dbfilepath = "mydatabase.db";
var databaseobj = new Sqlite(dbfilepath); //Sqlite class

//set errno 0
Server.errno(0);

printf("<b>Opening database.</b><br>");

if ( databaseobj.open()==true ) {
printf("Executing CREATE and INSERT statement.<br>");

//SQL statement
var ssqlstmt = ""
+"CREATE TABLE ttesttable (i INTEGER PRIMARY KEY, cname CHAR(255), cweight INTEGER); "
+"INSERT INTO ttesttable (cname,cweight) VALUES ('firstuser',95); "
+"INSERT INTO ttesttable (cname,cweight) VALUES ('anotheruser',263); "
;

//if statements executed ok
if ( databaseobj.exec(ssqlstmt)==true ) {
printf("Two rows inserted.<br>");
printf("Using SELECT query to fetch inserted values.<br>");
//query
if ( databaseobj.query("SELECT * FROM ttesttable")==true ) {
//printing selected values in tabular form ie.
//colname (TYPE) colname (TYPE) colname (TYPE)
//--------------------------------------------------
//value value value
//value value value
printf("Printing selected values in tabular form.<br><br>");
printf("<table bgcolor=\"#DDDDDD\" cellspacing=1 cellpadding=4>");

//print header row
printf("<tr bgcolor=\"#eeeeee\">");
for ( var ccol=0; ccol!=databaseobj.colcount(); ccol++ ) {
printf("<td><b>");
printf(databaseobj.colname(ccol));
printf(" (");
printf(databaseobj.coltype(ccol));
printf(")</b></td>");
}
printf("</tr>");


//print rows
var nrows = 0;
while ( (onerow=databaseobj.rowfetch())!=null ) {
printf("<tr bgcolor=\"#FFFFFF\">");
for ( var indx in onerow ) {
printf("<td>");
printf(onerow[indx]);
printf("</td>");
}
printf("</tr>");

nrows++;
}

printf("</table>");
printf("<br>");

printf("<i>number of rows in select query [");
printf(nrows);
printf("]</i><br>");
}
}
//error
else {
//ie. table already exists etc
printf("Error in exec, error [" ,databaseobj.error(), "]<br>");
}

printf("<b>Closing database.</b><br>");
databaseobj.close();
printf("Database closed.<br>");
}
//error
else {
//ie. invalid path, no permission etc
printf("Error opening database, error [" ,databaseobj.error(), "]<br>");
}



printf("Deleting database file.<br>");
//delete database file
if ( Server.unlink(dbfilepath)==true ) {
printf("Database file deleted.<br>");
}
else {
//ie. invalid path, no permission etc
printf("Error deleting file ",dbfilepath,", system errno [" ,Server.errno(), "]<br>");
}



printf("</body>");
printf("</html>");


// <--END CODE









--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,799
Latest member
Mercury_Dev

Latest Threads

Top