Skip to main content
 首页 » 编程设计

python之使用 Python 脚本导入数据库

2025年05月04日15shihaiming

我正在尝试使用我创建的 Python 脚本从数据库中提取一些表...

import jaydebeapi as jdbc 
import pandas.io.sql as psql 
import pandas 
import getpass 
import yaml 
p = yaml.load(file("/Users/glassjawed/.TD")) 
# Contains password 
c = jdbc.connect('com.teradata.jdbc.TeraDriver' 
,['jdbc:teradata://******.***.***.com','glassjawed',p] 
,['~/terajdbc4.jar','~/tdgssconfig.jar']) 
 
# read in the table we want 
df = psql.read_sql('SELECT TOP 10 user_id FROM store_users',c) 
print(pandas.DataFrame.head(df)) 
print(type(df)) 

但我遇到了以下错误。

  File "EPNconnect.py", line 11, in <module> 
    ,['~/terajdbc4.jar','~/tdgssconfig.jar']) 
  File "/usr/lib/python2.7/dist-packages/jaydebeapi/__init__.py", line 359, in connect 
    jconn = _jdbc_connect(jclassname, jars, libs, *driver_args) 
  File "/usr/lib/python2.7/dist-packages/jaydebeapi/__init__.py", line 182, in _jdbc_connect_jpype 
    jpype.JClass(jclassname) 
  File "/usr/lib/python2.7/dist-packages/JPype1-0.6.1-py2.7-linux-x86_64.egg/jpype/_jclass.py", line 55, in JClass 
    raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name) 
jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class com.teradata.jdbc.TeraDriver not found 

我安装了最新的 jar 文件,但仍然出现此错误。我不知道如何解决这个问题。帮忙?

请您参考如下方法:

在使用 JayDeBeApi 时,我在文件名中使用 ~ 时遇到了一些悲伤的经历。尝试使用绝对路径,例如:

    c = jdbc.connect('com.teradata.jdbc.TeraDriver', ['jdbc:teradata://******.***.***.com','glassjawed',p] 
,['/Users/myUserID/terajdbc4.jar', '/Users/myUserID/tdgssconfig.jar']) 

除此之外,我非常喜欢在使用 Python 时使用 teradata 包(和 ODBC)而不是 JayDeBeApi(和 JDBC)。