Connection error to Access database

痞子三分冷 提交于 2019-12-02 04:07:40

问题


I wrote the program which by means of pyodbc is connected to Access to that it was very glad. Help me please.

import pyodbc
#import kinterbasdb
import firebirdsql
import os, sys
import json,sqlite3,sql
sys.version_info
s1='''
create table CLIENTS
    (
       ID    INTEGER NOT NULL,
       COMPANY   VARCHAR(50),
       "LAST NAME"   VARCHAR(50),
       "FIRST NAME"   VARCHAR(50),
       "E-MAIL ADDRESS"   VARCHAR(50),
       "JOB TITLE"   VARCHAR(50),
       "BUSINESS PHONE"   VARCHAR(25),
       "HOME PHONE"   VARCHAR(25),
       "MOBILE PHONE"   VARCHAR(25),
       "FAX NUMBER"   VARCHAR(25),
       ADDRESS   BLOB SUB_TYPE 1,
       CITY   VARCHAR(50),
       "STATE/PROVINCE"   VARCHAR(50),
       "ZIP/POSTAL CODE"   VARCHAR(15),
       "COUNTRY/REGION"   VARCHAR(50),
       "WEB-SITE"   VARCHAR(25),
       NOTES   BLOB SUB_TYPE 1,
       INCLUDING   BLOB,
       CONSTRAINT PK_CLIENTS_ID PRIMARY KEY(ID)
    );
'''
s2='''create ascending index IDX_CLIENTS_CITY on CLIENTS (CITY);'''
#os.remove('D:/ThirdTask/test1.fdb')
conn=firebirdsql.create_database(host='localhost', database='/test1.fdb', user='sysdba', password='masterkey')
#con=firebirdsql.connect(host='localhost',database='D:/ThirdTask/test1.fdb', user='sysdba', password='masterkey')
cur=conn.cursor()
cur.execute(s1)
#cur.execute(s2)
conn.commit()
conAcc = pyodbc.connect('DRIVER={Microsoft Access Driver (*.accdb)};DBQ=Northwind.accdb')
conn.close()

As a result I received an error that me very much baffled. Help me please. I don't know in what a cause of error

Traceback (most recent call last):
  File "D:\ThirdTask\connecttwo.py", line 39, in <module>
    conAcc = pyodbc.connect('DRIVER={Microsoft Access Driver (*.accdb)};DBQ=Northwind.accdb')
Error: ('IM002', '[IM002] [Microsoft][\xc4\xe8\xf1\xef\xe5\xf2\xf7\xe5\xf0 \xe4\xf0\xe0\xe9\xe2\xe5\xf0\xee\xe2 ODBC] \xc8\xf1\xf2\xee\xf7\xed\xe8\xea \xe4\xe0\xed\xed\xfb\xf5 \xed\xe5 \xed\xe0\xe9\xe4\xe5\xed \xe8 \xed\xe5 \xf3\xea\xe0\xe7\xe0\xed \xe4\xf0\xe0\xe9\xe2\xe5\xf0, \xe8\xf1\xef\xee\xeb\xfc\xe7\xf3\xe5\xec\xfb\xe9 \xef\xee \xf3\xec\xee\xeb\xf7\xe0\xed\xe8\xfe (0) (SQLDriverConnect)')

回答1:


The driver name you specified...

DRIVER={Microsoft Access Driver (*.accdb)}

...is incorrect. There is no ODBC driver with that name. 32-bit applications that want to open an older .mdb database file can use

Driver={Microsoft Access Driver (*.mdb)}

To open an .mdb file from a 64-bit application, or to open an .accdb file from any application, you need to use

Driver={Microsoft Access Driver (*.mdb, *.accdb)}




回答2:


It looks like you do not have connected to the DB. Maybe you should begin with a smaller program just to connect. Samples in other questions : Using Microsoft Access SQL operators in Python ODBC




回答3:


This might help.

import pyodbc

# Connect to your access database file

DBfile = 'Filename.mdb'  # Let your file name <Filename> and access extension .mdb
conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};DBQ='+DBfile)  #    user/password can be used
cur = conn.cursor()

# Create new table in database

cur.execute ('CREATE TABLE CLIENTS (ID INTEGER, COMPANY STRING)')
conn.commit()

cur.close()
conn.close()


来源:https://stackoverflow.com/questions/12800101/connection-error-to-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!