如何用Python从CSV文件创建MDB文件?

2024-06-01 02:01:26 发布

您现在位置: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()

但我收到了以下错误:

^{pr2}$

我需要做的就是获取一个.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

Tags: inpyclientdblibpackageslinesite
3条回答

试试PyPyODBC,它可以很简单:

import pypyodbc
pypyodbc.win_create_mdb( "D:\\Your MDB file path.mdb" )

https://code.google.com/p/pypyodbc/wiki/pypyodbc_for_access_mdb_file

运行此脚本的计算机上尚未安装Access。在

我建议的第一件事是尽可能升级到Python2.7。在

第二,你试过^{}吗?在

下面是一个测试脚本,它执行上面的操作:

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()

相关问题 更多 >