server-side JavaScript: Example 2: Sqlite class

Discussion in 'Javascript' started by GVDC, Jun 28, 2008.

  1. GVDC

    GVDC Guest

    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









    --
    GVDC, Jun 28, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl Youngblood
    Replies:
    1
    Views:
    213
    Carl Youngblood
    Apr 9, 2005
  2. Replies:
    4
    Views:
    336
  3. GVDC
    Replies:
    0
    Views:
    148
  4. GVDC
    Replies:
    1
    Views:
    142
    Thomas 'PointedEars' Lahn
    Jun 28, 2008
  5. GVDC
    Replies:
    0
    Views:
    132
Loading...

Share This Page