_getDB is used to connect to a database.
_getDB(driver, jdbcurl, username, password)
Returns a sahiDB object
sahiDB has two methods.
select(sql); // select statements
update(sql); // update/delete/insert statements
select returns a sequential array of associative arrays.
The parameters are:
The driver has to be present in the classpath of Sahi. More info: Adding jars to Sahi’s classpath
The resultset that is returned by the select statement is a list of rows. Rows are accessible by index.
Eg. $rs[0], $rs[5]
The columns in a row are accessible by their column names (not column index).
Eg. $rs[0]["NAME"], $rs[0]["AGE"]
For table “User”
| ID | NAME | AGE |
| 1 | Kamlesh | 28 |
| 2 | Arun | 30 |
| 3 | Raja | 35 |
var db = _getDB("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@dbserver:1521:sid",
"username", "password");
var $rs = db.select("select * from User");
_assertEqual("1", $rs[0]["ID"]);
_assertEqual("Kamlesh", $rs[0]["NAME"]);
_assertEqual("28", $rs[0]["AGE"]);
_assertEqual("2", $rs[1]["ID"]);
_assertEqual("Arun", $rs[1]["NAME"]);
_assertEqual("30", $rs[1]["AGE"]);
_assertEqual("3", $rs[2]["ID"]);
_assertEqual("Raja", $rs[2]["NAME"]);
_assertEqual("36", $rs[2]["AGE"]);
Note the use of index for row and name for column ($rs[rowNum]["columnName"]).
Below is an example which loops through the results, and calls a custom function.
var db = _getDB("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@dbserver:1521:sid",
"username", "password");
var $rs = db.select("select * from User");
for (var $i=0; $i < $rs.length; $i++){
var $row = $rs[$i];
var $name = $row["NAME"];
var $age = $row["AGE"];
// pass name and age to your custom function like processNameAge
processNameAge($name, $age);
}