无法使用pyodbc连接QGIS插件和访问2007数据库

时间:2014-06-24 08:58:17

标签: python-2.7 ms-access-2007 pyodbc qgis

我正在运行Win 7 32位并使用python 27,我尝试使用pyodbc在我的QGIS插件中连接ms-access数据库,但是当我启动它时出现以下错误:

 Traceback (most recent call last):
      File "C:/PROGRA~1/QGISDU~1/apps/qgis/./python\qgis\utils.py", line 204, in startPlugin
        plugins[packageName] = package.classFactory(iface)
      File "C:\Users\Mr.Pakde/.qgis2/python/plugins\TestDbase\__init__.py", line 26, in classFactory
        from testdbase import TestDbase
      File "C:/PROGRA~1/QGISDU~1/apps/qgis/./python\qgis\utils.py", line 453, in _import
        mod = _builtin_import(name, globals, locals, fromlist, level)
      File "C:\Users\Mr.Pakde/.qgis2/python/plugins\TestDbase\testdbase.py", line 29, in 
        from testdbasedialog import TestDbaseDialog
      File "C:/PROGRA~1/QGISDU~1/apps/qgis/./python\qgis\utils.py", line 453, in _import
        mod = _builtin_import(name, globals, locals, fromlist, level)
      File "C:\Users\Mr.Pakde/.qgis2/python/plugins\TestDbase\testdbasedialog.py", line 28, in 
        r'DBQ=C:\__tmp\test1.accdb')

错误:('HY024',“[HY024] [Microsoft] [ODBC Microsoft Access驱动程序]'(未知)'不是有效路径。请确保路径名拼写正确并且您已连接到文件所在的服务器。(-1023)(SQLDriverConnect); [HY024] [Microsoft] [ODBC Microsoft Access驱动程序]'(未知)'不是有效路径。请确保路径名拼写正确,并且您连接到文件所在的服务器。(-1023)“)

我把它放在我的代码中:

from PyQt4 import QtCore, QtGui
from ui_testdbase import Ui_TestDbase
import pyodbc
db = pyodbc.connect(
        r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
        r'DBQ=C:\__tmp\test1.accdb')

class TestDbaseDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_TestDbase()
        self.ui.setupUi(self)

        Rec_1 = self.ui.btnOke
        QtCore.QObject.connect(Rec_1, QtCore.SIGNAL('clicked()'),self.Compskala)


    def Compskala(self):
        skal1 = self.ui.lineSkal1.text()
        skal2 = self.ui.lineSkal2.text()
        bandskal = ''
        if skal1 == skal2:
            bandskal = 'Kedua Peta dapat dipadukan'

我真的是个新手,有人可以给我一个帮助吗?

1 个答案:

答案 0 :(得分:3)

在文件名中用反斜杠替换正斜杠似乎有效:

<强>失败

>>> db = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};" +
                        "DBQ=C:\__tmp\test.accdb")
  

Traceback(最近一次调用最后一次):文件“”,第1行,in    pyodbc.Error:('HY000','[HY000] [Microsoft] [ODBC Microsoft   Access Driver]不是有效的文件名。 (-1044)(SQLDriverConnect)')

<强>则成功

>>> db = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};" +
                        "DBQ=C:/__tmp/test.accdb")
>>> connection.getinfo(pyodbc.SQL_SERVER_NAME)
'ACCESS'