Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > server-side JavaScript: Example 2: Sqlite class

Reply
Thread Tools

server-side JavaScript: Example 2: Sqlite class

 
 
GVDC
Guest
Posts: n/a
 
      06-28-2008
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









--







 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template class and class design on concrete example xl2csv writer Karim Python 0 11-23-2011 06:04 PM
Ruby sqlite/gem error: Could not load sqlite adapter jhs408@gmail.com Ruby 4 04-18-2009 12:53 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Installing sqlite-ruby when sqlite is in non-standard location Carl Youngblood Ruby 1 04-09-2005 03:32 AM
'example.com' == 'example.com.' => false... is this intended? Sam Roberts Ruby 15 02-07-2005 04:36 PM



Advertisments