Connect to MySQL DB using zxJDBC in a Sikuli script

Asked by Radamanthys

#! / Usr / bin / env python
# - * - codificação: utf-8 - * -
import jarray
from guide import *
from sikuli import *
import codecs

class classPathHacker(object):
    """Original Author: SG Langer Jan 2007, conversion from Java to Jython
    Updated version (supports Jython 2.5.2) From http://glasblog.1durch0.de/?p=846

    Purpose: Allow runtime additions of new Class/jars either from
    local files or URL
    """

    import java.lang.reflect.Method
    import java.io.File
    import java.net.URL
    import java.net.URLClassLoader

    def addFile(self, s):
        """Purpose: If adding a file/jar call this first
        with s = path_to_jar"""
        # make a URL out of 's'
        f = self.java.io.File(s)
        u = f.toURL()
        a = self.addURL(u)
        return a

    def addURL(self, u):
        """Purpose: Call this with u= URL for
        the new Class/jar to be loaded"""
        sysloader = self.java.lang.ClassLoader.getSystemClassLoader()
        sysclass = self.java.net.URLClassLoader
        method = sysclass.getDeclaredMethod("addURL", [self.java.net.URL])
        a = method.setAccessible(1)
        jar_a = jarray.array([u], self.java.lang.Object)
        b = method.invoke(sysloader, [u])
        return u

tmp = classPathHacker()
tmp.addFile("C:\Arquivos de Programas\MySQL\MySQL Connector J\mysql-connector-java-5.1.35-bin.jar")

from com.ziclix.python.sql import zxJDBC

db = zxJDBC.connect("jdbc:mysql://localhost/usuarios", "root", "", "com.mysql.jdbc.Driver", CHARSET='utf-8')
C = db.cursor()
#C.execute("insert into status values(?),['passou']")

#C.execute ("select * from usuario")
#C.execute("insert into status values (?)", ['Passou'])
C.execute ("select * from status")
#C.execute(sql)
for a in C.fetchall():

    uni = unicode(a,'utf-8')

    print repr(uni)

soon after,nothing happens.
I'am listed the bank and there were results

sorry my english

Question information

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

With the latest version of SikuliX 1.1.0 this is all you have to do:

load("mysql.jar") # see comment
from com.ziclix.python.sql import zxJDBC
db = zxJDBC.connect("jdbc:mysql://localhost/usuarios", "root", "",
    "com.mysql.jdbc.Driver", CHARSET='utf-8')

comment: "mysql.jar" is either an absolute path to the driver jar or just this, if the driver named "mysql.jar" is located in the SikuliX Extensions folder (http://www.sikulix.com/quickstart.html#qs3)

This runs when either using this as script inside the SikuliX IDE or using sikulix.jar/runsikulix(.cmd) to run a .sikuli from command line.

If you want to use this with your own Jython, then you have to use sikulixapi.jar according to
https://github.com/RaiMan/SikuliX-2014/wiki/Usage-in-Java-programming.

To avoid any utf-8 conversion problems, you should use Jython 2.7.0 (which is bundled with SikuliX 1.1.0)

BTW:
#! / Usr / bin / env python <---- not sure what this should help !?
# - * - codificação: utf-8 - * - <---- not needed when using SikuliX to run your scripts

Can you help with this problem?

Provide an answer of your own, or ask Radamanthys for more information if necessary.

To post a message you must log in.