Brief Guide to MySQLXPCOM
by Moss Collum <moss@hamparts.com>
Contents
Overview
Exceedingly brief introduction to MySQLXPCOM: first initialize the MySQL component, then create a Connection object, then call the appropriate methods on it. Everything's listed here in more or less the order you'll use it.
I've also put together a simplified interface, SimpleMySQLConnection. It's a relatively quick hack, and hasn't been thoroughly tested, so I make no promises about performance or reliability. But it's been working well for me, and it does take care of some of the overhead of creating a connection.
MySQLComponent
To initialize the MySQLXPCOM component, use:
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var mysql = Components.classes["@mozilla.org/mysql;1"].createInstance();
mysql = mysql.QueryInterface(Components.interfaces.nsIMySQL);
Once this is done, you can create a Connection object
Connection
mysqlRun = mysql.Connection(host, port, database, user, password);
- mysqlRun
- Variable that will hold the new Connection object.
- mysql
- An existing MySQL component.
- host
- The hostname of the MySQL server.
- port
- The port number of the MySQL server.
- user
- The username to log on with.
- password
- The password for the given username.
If this method fails, it will throw an exception.
The remaining items described below are methods of the Connection object.
Execute
Execute a MySQL statement. If it is a query, the results will be available through the same Connection object.
mysqlRun.Execute(sqlcommand, type);
- mysqlRun
- An existing Connection object.
- sqlcommand
- Text of a MySQL command to execute.
- type
- Unclear what exactly this is. Set to 1 if you want to use the NumRows method, 0 otherwise.
FetchFields
Appears to do the same thing as the FetchFieldNames method, but with different syntax. So far, I haven't worked out the details of this one--I just use FetchFieldNames instead.
FetchFieldNames
Fetch the names of the fields in the result set for the current query.
var fieldNamesCnt = {value: 0};
var fieldNames = {value: []};
mysqlRun.FetchFieldNames(fieldNamesCnt, fieldNames);
- mysqlRun
- An open Connection object.
- fieldNamesCnt.value
- After method is called, contains the number of fields in the result set.
- fieldNames.value
- After method is called, contains an array of field names.
If this method fails, it will throw an exception.
NumRows
Return the number of rows in the current result set.
rows = mysqlRun.NumRows();
- mysqlRun
- An open Connection object.
- rows
- Gets set to the number of rows in the response to the most recent query.
FetchRow
Fetch a new row of results from the current query.
var rowDataCnt = {value: 0};
var rowData = {value: []};
mysqlRun.FetchRow(rowDataCnt, rowData);
- mysqlRun
- An open Connection object.
- rowDataCnt.value
- After method is called, contains the number of columns in the row.
- rowData.value
- After method is called, contains an array of field names.
Finish
Closes the connection to the database.
rows = mysqlRun.Finish();
- mysqlRun
- An open Connection object.
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.