Connectiong to MSSQL

Asked by Bruno Vieira

Hello all.

During one of my tests, I need to perform a couple of queries in a MSSQL database.
How would you recommend to do this? Which module to import? How to import it?

Thanks a lot.
Bruno

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Bruno Vieira
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

You may use any solution, that runs with Jython (just look there).

e.g. some JDBC stuff, that connects you to your MSSQL DB via the respective JDBC native driver.

There seems to be many appropriate solution - at least Google says ;-)

The modules you may have to import in your Sikuli script have to reside somewhere in the sys.path.
This is easily achieved by putting the modules in the Sikuli Lib path (in the Sikuli program folder in Windows, create it if it is not there). It is already in sys.path at first place.

Would be nice if you give a short feedback about your solution.

Revision history for this message
Bruno Vieira (giuliapo) said :
#2

Hi RaiMan

I have been researching a lot to fix this. I find some webpages referring to a bug in Jython from 2008, not sure if still applies.
I also found another user with the same question here:
https://answers.launchpad.net/sikuli/+question/121652

My code is:
from com.ziclix.python.sql import zxJDBC
d, u, p, v = "jdbc:microsoft:sqlserver://myserver", "login", "password", "com.microsoft.jdbc.sqlserver.SQLServerDriver"
db = zxJDBC.connect(d, u, p, v)

The error is:
zxJDBC.DatabaseError: driver [com.microsoft.jdbc.sqlserver.SQLServerDriver] not found

I downloaded the JDBC 3.0 from Microsoft:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=%20a737000d-68d0-4531-b65d-da0f2a735707&displaylang=en

I have it inside the c:\sikuli\Lib folder.
I added the driver location to the classpath, path, all kinds of paths :)

Do you have any suggestions? Maybe to put the driver inside the sikuli.jar file in the specific "com\microsoft\jdbc.sqlserver"?

Thanks a lot for your help.
Bruno

Revision history for this message
RaiMan (raimund-hocke) said :
#3

I think you may have the driver in whatever directory you want.

In the actual classpath implemented by Sikuli are only .jar files. So there is actually no directory, where you could put your driver in.

You have to adjust the classpath itself.
Without having tested, I guess you have to specify in the Sikuli-IDE.bat your driver directory with the additional parameter -cp absolute-path-to-your-driver in the line, where java.exe is called.

You will find out, wether it adds to or replaces the classpath ;-)
But generally I think, this is the direction to search for a solution.

If this does not help, try the following (in your script before touching any JDBC stuff):

import java
cp = java.lang.System.getProperty("java.class.path")
java.lang.System.setProperty("java.class.path", "absolute-path-to-your-driver-dir"+":"+cp)

internally the classpath is a string of path's separated by colon.

Revision history for this message
Bruno Vieira (giuliapo) said :
#4

Those were good suggestions, but I could not make it work.
When I do:
import com.microsoft.sqlserver.jdbc.SQLServerDriver
it does not complain, meaning it finds the driver.

But when I do:
zxJDBC.connect(url,user,passwd, "com.microsoft.sqlserver.jdbc.SQLServerDriver")
it says it cannot find the driver.

Meanwhile I solved my need to do query creating a report and using sikuli to open IE and checking the results of the report on the webpage.

But if someone finds the solution to use the driver, it would be nice to know it as well.

Thanks for your help.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

1.--- ok. you have a solution ;-)

2.--- import com.microsoft.sqlserver.jdbc.SQLServerDriver
that this works, only means, that it is found on the PYTHONPATH (sys.path), this means nothing for java

3.--- zxJDBC.connect(url,user,passwd, "com.microsoft.sqlserver.jdbc.SQLServerDriver")
the driver is searched in the classpath of java

I will try the next days with SQLite.