2
votes

I am trying to use MySQL to store my results with my monkeyrunner script. I am getting this error:

import MySQLdb
ImportError: No module named named MySQLdb

I write my monkeyrunner script using python. I run the scripts on android-sdk/tools folder where monkeyrunner is located. And I already setup python-mySQL connection, it's working fine when I run python scripts inside Python27 folder(not inside Android)

How do I import MySQLdb to my monkeyrunner script?

Thanks.

2

2 Answers

0
votes

Monkeyrunner uses jython, not python. Hence, you are able to connect to mysqldb using python scripts. Not using the jython script.

From monkeyrunner, to access mySQL you should use zxJDBC package for jython. Add JDBC driver in your monkeyrunner class path.

The zxJDBC package provides a nearly 100% Python DB API 2.0 compliant interface for database connectivity in Jython And following code should get you to connect to the mysql database.

from com.ziclix.python.sql import zxJDBC
params = {}
params['serverName'] = 'localhost'
params['databaseName'] = 'ziclix'
params['user'] = None
params['password'] = None
params['port'] = 3306
db = apply(zxJDBC.connectx, ("org.gjt.mm.mysql.MysqlDataSource",), params)

Below link can elaborate more on this.

http://www.jython.org/archive/21/docs/zxjdbc.html

0
votes

Keep it simple. Instead of dealing with jars, versions, and other stuff just use the command line:

#! /opt/android-sdk/tools/monkeyrunner

import subprocess

USER = 'myuser'
PASSWORD = 'mypass'
DB = 'mydb'
p1 = subprocess.Popen([ "mysql", "--user=%s" % USER, "--password=%s" % PASSWORD, DB ], stdin=subprocess.PIPE)

print p1.communicate('INSERT INTO t1 VALUES(1, "other")')