18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
_getDB
Hi Team,
I am unable to read data from database by using the
_getDB option:
My sahi.bat is:
cd /d %~dp0
SET EXT_CLASS_PATH=..\extlib\db\mysql-connector-java-5.0.4-bin.jar;C:\sahi\extlib\classes12.jar;
SET SAHI_CLASS_PATH=..\lib\sahi.jar;..\extlib\rhino\js.jar;..\extlib\apc\commons-codec-1.3.jar;
java -classpath C:\sahi\extlib\classes12.jar;../lib/sahi.jar net.sf.sahi.Proxy
my code is:
var db = _getDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@host:port:sid", "usrname", "pwd");
var $rs = db.select("select * from emptable where emp_name = 'chej'");
_wait(1000);
_alert($rs);
_assertEqual("nes", $rs[0]["emp_name"]);
Please let me know what is the error.
I am unable to read data from database by using the
_getDB option:
My sahi.bat is:
cd /d %~dp0
SET EXT_CLASS_PATH=..\extlib\db\mysql-connector-java-5.0.4-bin.jar;C:\sahi\extlib\classes12.jar;
SET SAHI_CLASS_PATH=..\lib\sahi.jar;..\extlib\rhino\js.jar;..\extlib\apc\commons-codec-1.3.jar;
java -classpath C:\sahi\extlib\classes12.jar;../lib/sahi.jar net.sf.sahi.Proxy
my code is:
var db = _getDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@host:port:sid", "usrname", "pwd");
var $rs = db.select("select * from emptable where emp_name = 'chej'");
_wait(1000);
_alert($rs);
_assertEqual("nes", $rs[0]["emp_name"]);
Please let me know what is the error.
Tagged:
Comments
cd /d %~dp0
SET EXT_CLASS_PATH=C:\sahi\extlib\classes12.jar;
SET SAHI_CLASS_PATH=..\lib\sahi.jar;..\extlib\rhino\js.jar;..\extlib\apc\commons-codec-1.3.jar;
java -classpath %EXT_CLASS_PATH%;%SAHI_CLASS_PATH% net.sf.sahi.Proxy
If you also need mysql driver, use
SET EXT_CLASS_PATH=..\extlib\db\mysql-connector-java-5.0.4-bin.jar;C:\sahi\extlib\classes12.jar;
_alert(db.select("select * from emptable where emp_name = 'chej'"));
TypeError: 'db' is undefined
No trace available
Click for browser script [object Object] at Oct 27, 2009 10:08:36 AM
var $db = _getDB("Driver","Jdbc url","username","password");
_alert($db.select("select * from emptable where emp_name = 'chej'"));
When you write
_alert(db);
Sahi will search for the variable db on the browser (and will not get it)
When you write
_alert($db);
Sahi will evaluate $db on the proxy and pass it to _alert on the browser.
I used the following code:
var $db = _getDB("Driver","Jdbc url","username","password");
_alert($db.select("select * from emptable where emp_name = 'chej'"));
Got yhe following error:
_alert([object Object]);
SyntaxError: Expected ']'
No trace available
Click for browser script [object Object] at Oct 27, 2009 2:22:10 PM
Any help?
You are right, there is a bug in Sahi in the last 2 builds which is causing the alert to fail. Your alert would work if you added an empty string to it like this:
var $db = _getDB("Driver","Jdbc url","username","password");
var $resultAsString = "" + $db.select("select * from emptable where emp_name = 'chej'");
_alert($resultAsString);
But I guess this bug would not bother you since you would be iterating over the array and using those variables elsewhere rather than alerting them.
You can expect a build in a day or two with a fix for this. Email me if your need is urgent and I will email you the needed files.
Regards,
Narayan
Changed the code to :
var $db = _getDB("Driver","Jdbc url","username","password");
var $resultAsString = "" + $db.select("select * from emptable where emp_name = 'chej'");
_alert($resultAsString);
This time i get an empty result!!
Where as
_alert($db.select("select * from emptable where emp_name = 'chej';");
I got "Undefined"
The database is working fine.
But i am unable to retrive and assert the values through Sahi.
Any help?
Create an excel sheet by name testSheet.xls. Create a column by title "Name". Enter few rows of data. Then run the following code segment and let me know how it goes. (Please update the path to the excel sheet appropriately). Assuming that you are working on a Windows machine, the below code should work fine
I gave the following code:
var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:/sahi/scripts/3GDibbler/testSheet.xls;readOnly=false", "", "");
$rs = db.select("select * from [Sheet1$];");
_alert($rs.length);
_assertEqual("dp", $rs[0]["Name"]);
And the error message:
JSERROR SyntaxError: syntax error at Nov 2, 2009 3:22:25 PM
Also,
1. What jars need to be placed in the classpath for the above code to work?
2. Please verify if the syntax mentioned above for _getDB is correct.
Sahi gives JSError mostly when
1. The database is not accessible (or) problem with connecting to database.
2. Any driver related errors (or) Driver is not recognizable
And also try this,
_getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");
For more details on how to access Excel datas using Sahi, Please refer http://sahi.co.in/w/working-with-excel-sheets
Which version of Sahi you are using?
Please try it with the latest release
Let us know if you still have any problems.
I am using the Sahi V3 (2009-10-23)
Used the following code:
_getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");
Still getting the error:
JSERROR
SyntaxError: syntax error at Nov 2, 2009 6:21:12 PM
Any help?
The code that you wrote and the one posted by Venky has an error. Both of you tried to split the second parameter into two!!! The second parameter is a single string including the Driver, DBQ and readonly.
Hi Chejness, Please try it as dp mentioned and let us know whether it helps or not.
I used the code as given by dp:
var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");
$rs = db.select("select * from [Sheet1$]");
_alert($rs.length);
_assertEqual("dp", $rs[0]["Name"]);
Got the same error!!
JSERROR
SyntaxError: syntax error at Nov 3, 2009 9:13:30 AM
What is the workaround for this?
Any help?
Any workaround to access db through scripts?
DB, Excel access seems to be working fine for the rest of us. One new thing that came up was that if the data had newlines in it, a JSERROR was being thrown. You could have a look at this: http://sahi.co.in/forums/viewtopic.php?id=1075 to see if the suggested workaround fixes your problem.
If not, please email your sample excel sheet and script which fails, and we will help you fix your problem.
Regards,
Narayan