How to use JDBC Sqlite in Sikuli Script (Jython)

Asked by Andrew Munro

--- final solution:

see comment #22

-------------------------------------

I tried to import sqlite3 into sikuli only to find I was getting import errors. After digging around a bit, it seems sqlite3 does not work in jython (correct me if im wrong?).

Anyway, does anyone know of anyway to connect to a database in sikuli?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1
Revision history for this message
Andrew Munro (andrew-munro) said :
#2

Thanks, almost got it working. Are there any JDBC drivers built in to the sikuli jython instance or do I have to add my own.

If not, would I just download a driver.jar and then modify my batch script and add something like:

java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dpython.path="Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar/" -jar "Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-ide.jar" -jar "driver.jar"

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

Jython does not come with any DB drivers. Have to be added via class path parameter ( -cp on java command).

see: http://www.jython.org/jythonbook/en/1.0/DatabasesAndJython.html

Revision history for this message
Andrew Munro (andrew-munro) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
Andrew Munro (andrew-munro) said :
#5

Hey, so after reading through the documentation, I tried two methods to import my java class.

The first being :

%JAVA_EXE% -Xms64M -Xmx512M -classpath "sqlite-jdbc.jar" ....

I also tried adding:

 set CLASSPATH=sqlite-jdbc.jar;%CLASSPATH%

but I still get "zxJDBC.DatabaseError: driver [org.sqlite.JDBC] not found

I feel like I'm missing something obvious.

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

ok, now you are ready for question
https://answers.launchpad.net/sikuli/+question/202408

ok, just a joke ;-)
simply forgot about that

Revision history for this message
Andrew Munro (andrew-munro) said :
#7

... how did my searching not find this -_-

too early in the morning. Sorry :)

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

sometimes it is the trick, that you know what to search for ;-)
all the best.

Revision history for this message
Andrew Munro (andrew-munro) said :
#9

Ok, tried experimenting for the last hour and still can't get this to work..(either windows command files suck or I do xD)

My normal bat script consists of:

@echo off

PATH=%PATH%;Sikuli-X-1.0rc3\Sikuli-IDE\libs
set JAVA_EXE="java"
set CLASSPATH=sqlite-jdbc.jar;%CLASSPATH%

if defined PROGRAMFILES(X86) set JAVA_EXE="%PROGRAMFILES(X86)%\Java\jre6\bin\java.exe"
%JAVA_EXE% -Xms64M -Xmx512M -classpath "sqlite-jdbc.jar" -Dfile.encoding=UTF-8 -Dpython.path="Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar/" -jar "Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-ide.jar" -r "run_suite.sikuli"

My driver is in the same directory as the .bat file as is my jython.jar that I downloaded.

I have tried the following:

set cp=sqlite-jdbc.jar
set cp=%cp%:jython.jar
java -cp %cp% org.python.util.jython %1.sikuli/%1.py

I do not understand the last part %1.sikuli/%1.py.

I could not get it to find sikuli :s

Revision history for this message
Andrew Munro (andrew-munro) said :
#10

Exception in thread "main" java.lang.NoClassDefFoundError: org/python/util/jython
Caused by: java.lang.ClassNotFoundException: org.python.util.jython
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.python.util.jython. Program will exit.

FYI that is the error I have

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

--- I do not understand the last part %1.sikuli/%1.py.
Since you are using the Jython interpreter directly, you have to give it the .py file in the .sikuli folder.

--- just checked for Windows
the delimiter in class path between entries is ; (semicolon) and not : (colon)

--- easiest way to test (being in the folder that contains the sqlite-jdbc.jar)
java -cp "sqlite-jdbc.jar;c:\Program Files\Sikuli X\sikuli-script.jar" org.python.util.jython

which opens an interactive Jython session.

first enter
from sikuli import *
to have access to Sikuli features

then import your driver and check functionality.

accoording to the mentioned question this should work.

If you send me the download link for the driver, I will check tomorrow on my Win 7

Revision history for this message
Andrew Munro (andrew-munro) said :
#12

Ok confirmed that I could get it working in the jython shell, however I want it to execute and run my script with a single .bat

I tried:

%JAVA_EXE% -Xms64M -Xmx512M -cp "sqlite-jdbc.jar;Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar/" org.python.util.jython

Finds my driver and connects fine!

But when I try to swap them round and put the jython in the cp and run the sikuli-ide with my startup script, it can't find the driver:

%JAVA_EXE% -Xms64M -Xmx512M -cp "sqlite-jdbc.jar;jython.jar/" -Dfile.encoding=UTF-8 -Dpython.path="Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" -jar "Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-ide.jar" -r "run_suite.sikuli"

Im going to do a little reading into java parameters, but im puzzled as to how I execute sikuli automatically with jython.

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

no need to run a ready script using sikuli-ide.bat

try this:

java -cp "sqlite-jdbc.jar;c:\Program Files\Sikuli X\sikuli-script.jar" -jar "c:\Program Files\Sikuli X\sikuli-script.jar" run_suite.sikuli

BTW: avoid any special characters in Sikuli script file names.
so better: runSuite.sikuli

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

or even this (since sikuli-script.jar is already in cp):

java -cp "sqlite-jdbc.jar;c:\Program Files\Sikuli X\sikuli-script.jar" org.sikuli.script.SikuliScript run_suite.sikuli

There might be an error message when script ends (something about cannot run ...): just ignore it ;-)

Revision history for this message
Andrew Munro (andrew-munro) said :
#15

Nope, still can't find the driver. Im confused now, I thought I had to add jython to my cp or something?

Using:

%JAVA_EXE% -cp "sqlite-jdbc.jar;Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" -jar "Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" run_suite.sikuli

I:\QA\sikuli>"Run_Suite(Vista,7).bat"
[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
[error] Can't run this Sikuli script: run_suite.sikuli
Traceback (most recent call last):
  File "I:\QA\sikuli\run_suite.sikuli\run_suite.py", line 26, in <module>
    conn = zxJDBC.connect("jdbc:sqlite:"+filename+"\\tests.db", None, None, "org.sqlite.JDBC")
zxJDBC.DatabaseError: driver [org.sqlite.JDBC] not found

Code:

filename = dubit.config_get(testcfgfile, 'paths', 'htmlindex')
from com.ziclix.python.sql import zxJDBC
conn = zxJDBC.connect("jdbc:sqlite:"+filename+"\\tests.db", None, None, "org.sqlite.JDBC")

I am remebering back to when my friend suggested using a database to store my results data. "It will be 3 lines of python!" he said... "it will be easy"... he said xD

Revision history for this message
Andrew Munro (andrew-munro) said :
#16

Ok, I tried this:

%JAVA_EXE% -Xms64M -Xmx512M -cp "sqlite-jdbc.jar;Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" -jar "jython.jar" "run_suite.py"

Couldn't find my driver and also gave me a load of cpython import errors (e.g No module named OS)

Revision history for this message
Andrew Munro (andrew-munro) said :
#17

Ok, I tried this:

%JAVA_EXE% -Xms64M -Xmx512M -cp "sqlite-jdbc.jar;Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" -jar "jython.jar" "run_suite.py"

Couldn't find my driver and also gave me a load of cpython import errors (e.g No module named OS)

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

what about suggestion in comment #14 ?

and this might work

java -cp "sqlite-jdbc.jar;c:\Program Files\Sikuli X\sikuli-script.jar" org.python.util.jython run_suite.sikuli\run_suite.py

but the the script needs a
from sikuli import *
at beginning

and you have to take care for your images (e.g. setBundlePath correctly or add image path)

--- Im confused now, I thought I had to add jython to my cp or something?
no need for that, jython 2.5.2 is contained in sikuli-script.jar

--- I am remebering back to when my friend suggested using a database to store my results data. "It will be 3 lines of python!" he said... "it will be easy"... he said xD
Did your friend know, that you wanted to use Sikuli together with JDBC ?
The 3 lines are not the problem - problem is the one line, that runs your stuff using Java ;-)

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

Again:
If you send me the download link for the driver, I will check tomorrow on my Win 7

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

ok, tomorrow now is today ;-)

Revision history for this message
Andrew Munro (andrew-munro) said :
#21

http://code.google.com/p/sqlite-jdbc/

Driver works fine because it worked in the jython shell :3

Revision history for this message
Andrew Munro (andrew-munro) said :
#22

Hey it works!!!!

%JAVA_EXE% -Xms64M -Xmx512M -cp "sqlite-jdbc.jar;Sikuli-X-1.0rc3\Sikuli-IDE\sikuli-script.jar" org.sikuli.script.SikuliScript run_suite.sikuli

I made a stupid error and didn't use your solution properly. I still tried the .jar rather than org.sikuli.script.SikuliScript !

Thanks for all your help RaiMan, you're a lifesaver :)

Revision history for this message
Andrew Munro (andrew-munro) said :
#23

Thanks RaiMan, that solved my question.

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

always welcome ;-)