SimpleMySQLConnection

by Moss Collum <moss@hamparts.com>

Overview

To use: Include simplemysqlconnection.js in your project, create a new SimpleMySQLConnection object, call executeQuery() to do queries, and call close() when you're finished. If the query is a SELECT statement, executeQuery() returns an array of associative arrays representing the values in each row of the response. Alternately, you can use executeRDFQuery() to get the results in the form of an RDF datasource suitable for use with a XUL template, rather than an array. An array can be converted into a datasource using getDatasourceFromArray().

Usage Example

Suppose you have a database named example, hosted on mysql.example.com, on port 3306, accessible to username jrandom, password foobar, and containing a table called Users, with the following data:

user_id name home_page
1 J. Random Hacker http://www.example.com/
2 Moss Collum http://www.m14m.net/

To set up your connection, you would call:

connection = new SimpleMySQLConnection('mysql.example.com', 3306, 'example', 'jrandom', 'foobar', 'http://www.example.com/autordf');

To get some data as an array, you could call:

results = connection.executeQuery("SELECT user_id, name, home_page FROM Users");
after which, for example, results[0]['name'] will be "J. Random Hacker", and results[1]['home_page'] will be "http://www.m14m.net/".

Alternately, to get the same data as an RDF datasource, you could call:

rdfresults = connection.executeRDFQuery("SELECT user_id, name, home_page FROM Users");

or, if you had already retrieved the results in array form:

rdfresults = connection.getDatasourceFromArray(results);

In either of these cases, rdfresults would contain an RDF datasource equivalent to the following RDF/XML:

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:autordf="http://www.example.com/autordf#">
    <rdf:Description about="urn:root">
        <autordf:rows>
            <rdf:Seq>
                <rdf:li>
                    <rdf:Description
                        autordf:user_id="1"
                        autordf:name="J. Random Hacker"
                        autordf:home_page="http://www.example.com/" />
                </rdf:li>
                <rdf:li>
                    <rdf:Description
                        autordf:user_id="2"
                        autordf:name="Moss Collum"
                        autordf:home_page="http://www.m14m.net/" />
                </rdf:li>
            </rdf:Seq>
        </autordf:events>
    </rdf:Description>
</rdf:RDF>

Copyright

This document Copyright © 2003 Doodlelab, Inc.

This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.