无法使用带有mysql.connector模块的Jython连接到mysql数据库

时间:2017-04-06 13:48:20

标签: java python mysql jython

我正在创建一个脚本,它将从mysql服务器获取数据并使用Jasypt解密文本,Jython是一个用于加密/解密文本的java库。所以我必须使用std::normal_distribution::(constructor)执行此操作。

我可以从Jasypt库导入java类。我也可以将mysql.connector导入Jython脚本。但是脚本无法连接到mysql服务器。

以下是我正在尝试的代码

import sys
SYSPATH = ['/usr/local/lib/python2.7/site-packages', '/data/packages/jasypt-1.9.2.jar']
[sys.path.append(p) for p in SYSPATH]
import mysql.connector

# adding this to avoid "AttributeError: socket.SOL_TCP does not exist" listed at https://github.com/pika/pika/issues/67
import socket
socket.SOL_TCP = socket.IPPROTO_TCP

# This line is giving error "TypeError: unpack_from(): 2nd arg can't be coerced to String"
con = mysql.connector.connect(user='username', password='password', host='192.168.123.123', database='users')

最后一行是我的错误

 File "2017-04-05-get-bank-sms.py", line 11, in createUserIdUuidMap
   con = mysql.connector.connect(user='username', password='password',    host='192.168.123.123', database='users')
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
   return MySQLConnection(*args, **kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
   self.connect(**kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
   self._open_connection()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 207, in _open_connection
   self._do_handshake()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 99, in _do_handshake
   packet = self._socket.recv()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
TypeError: unpack_from(): 2nd arg can't be coerced to String

我正在尝试的是否可能?

1 个答案:

答案 0 :(得分:1)

基于其他相关问题,最常见的方法似乎是使用“MySQL Connector / J”(JDBC驱动程序)而不是“MySQL Connector / Python”。我可以确认这个Jython代码有效......

from com.ziclix.python.sql import zxJDBC

connectionUrl = "jdbc:mysql://localhost:3307"
cnxn = zxJDBC.connect(
        connectionUrl,
        "root",
        "mypassword",
        "com.mysql.jdbc.Driver")
crsr = cnxn.cursor()
crsr.execute("SHOW DATABASES")
rows = crsr.fetchall()
print(rows)

...当我使用以下批处理文件运行它时:

SET CLASSPATH=C:\Users\gord\.m2\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar
C:\jython2.7.0\bin\jython mysql_test.py