2010-09-02 148 views
2

我有幾個csv文件,我需要轉移到mdb格式。我想答案在這個崗位爲起點:如何從python的csv文件創建一個mdb文件?

How do you create a mdb database file in Python?

來源:

from comtypes.client import CreateObject 

access = CreateObject('Access.Application') 

from comtypes.gen import Access 

DBEngine = access.DBEngine 
db = DBEngine.CreateDatabase('test.mdb', Access.DB_LANG_GENERAL) 


db.BeginTrans() 

db.Execute("CREATE TABLE test (ID Text, numapples Integer)") 
db.Execute("INSERT INTO test VALUES ('ABC', 3)") 

db.CommitTrans() 
db.Close() 

但我收到以下錯誤:

Traceback (most recent call last): 
    File "C:\Documents and Settings\rkelly1\Desktop\New Folder (6)\testwrite.py", line 3, in ? 
access = CreateObject('Access.Application') 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 242, in CreateObject 
return _manage(obj, clsid, interface=interface) 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 188, in _manage 
obj = GetBestInterface(obj) 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface 
interface = getattr(mod, itf_name) 
AttributeError: 'module' object has no attribute '_Application' 

所有我需要做的是取一個.csv文件,並以mdb格式創建一個數據庫。我有SQL但不與創建Access數據庫文件的一些經驗....

編輯:

我並不是主張,這是正確的解決方案....如果你有一個更好的,請讓我知道

編輯:劇本後,一個新的第一次運行時安裝comtypes我得到這些錯誤:安裝在此腳本運行的計算機上

# Generating comtypes.gen._4AFFC9A0_5F99_101B_AF4E_00AA003F0F07_0_9_0 
# Generating comtypes.gen._2DF8D04C_5BFA_101B_BDE5_00AA0044DE52_0_2_4 
# Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0 
# Generating comtypes.gen.stdole 
Traceback (most recent call last): 
    File "C:\Documents and Settings\rkelly1\Desktop\New Folder (6)\testwrite.py", line 3, in ? 
    access = CreateObject('Access.Application') 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 242, in CreateObject 
    return _manage(obj, clsid, interface=interface) 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 188, in _manage 
    obj = GetBestInterface(obj) 
    File "C:\Python24\Lib\site-packages\comtypes\client\__init__.py", line 110, in GetBestInterface 
    mod = GetModule(tlib) 
    File "C:\Python24\Lib\site-packages\comtypes\client\_generate.py", line 112, in GetModule 
    mod = _CreateWrapper(tlib, pathname) 
    File "C:\Python24\Lib\site-packages\comtypes\client\_generate.py", line 176, in _CreateWrapper 
    generate_module(tlib, ofi, pathname) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\tlbparser.py", line 716, in generate_module 
    gen.generate_code(items.values(), filename=pathname) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 238, in generate_code 
    self.generate_all(items) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 186, in generate_all 
    self.generate(item) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 182, in generate 
    mth(item) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 689, in ComInterface 
    self.generate(itf.get_head()) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 182, in generate 
    mth(item) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 710, in ComInterfaceHead 
    self.generate(base.get_head()) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 182, in generate 
    mth(item) 
    File "C:\Python24\Lib\site-packages\comtypes\tools\codegenerator.py", line 616, in External 
    comtypes.client.GetModule(ext.tlib) 
    File "C:\Python24\Lib\site-packages\comtypes\client\_generate.py", line 112, in GetModule 
    mod = _CreateWrapper(tlib, pathname) 
    File "C:\Python24\Lib\site-packages\comtypes\client\_generate.py", line 188, in _CreateWrapper 
    mod = _my_import(fullname) 
    File "C:\Python24\Lib\site-packages\comtypes\client\_generate.py", line 26, in _my_import 
    return __import__(fullname, globals(), locals(), ['DUMMY']) 
    File "C:\Python24\lib\site-packages\comtypes\gen\_2DF8D04C_5BFA_101B_BDE5_00AA0044DE52_0_2_4.py", line 82 
    (['retval', 'out'], POINTER(POINTER(IDispatch)), 'ppidisp')), 
COMMETHOD([dispid(1610743809), 'propget'], HRESULT, 'Creator', 
                  ^
SyntaxError: invalid syntax 
+1

沒有腳本的來源是很難猜測那裏發生了什麼。 – 2010-09-02 13:22:16

+0

@Fabian的來源是來自相關的答案,但我明白你的意思。現在正在編輯... – Richard 2010-09-02 13:24:30

回答

4

我建議的第一件事是如果可能的話,升級到python 2.7。

二,你有沒有試過win32com

這是一個測試腳本,做你所擁有的上述:

import win32com.client 
import os 

def main(): 
    db_path = r'C:\temp.mdb' 
    if os.path.exists(db_path): 
     os.remove(db_path) 

    db_eng = win32com.client.gencache.EnsureDispatch("DAO.DBEngine.36") 
    db = db_eng.CreateDatabase(db_path, win32com.client.constants.dbLangGeneral) 

    db.Execute("CREATE TABLE test (ID Text, numapples Integer)") 
    db.Execute("INSERT INTO test VALUES ('ABC', 3)") 

    db.Close() 


if __name__ == '__main__': 
    main() 
+0

謝謝。 +1這可以按預期工作:D – Richard 2010-09-02 14:08:19

+0

我發現遷移到python 2.7是一個可怕的任務,我發現大部分腳本都是用python 2.4編寫的:S – Richard 2010-09-02 14:19:45

+1

這種方法的一個優點是它依賴於每個副本上安裝的組件的Windows(自Win2000以來) - 它使用DAO和Jet數據庫引擎,而不是嘗試自動化Access,這可能沒有安裝。 – 2010-09-02 18:57:44

2

你沒有訪問。

+0

嗯......但我確實。我有權訪問2007 – Richard 2010-09-02 13:29:06

+1

@Richard:嗯......調查... – katrielalex 2010-09-02 13:40:05

+2

@Richard:我無法再現這一切...你做的是正確的事情。我會嘗試@ tgray的答案。 – katrielalex 2010-09-02 14:02:41

相關問題