MS Access db(mdb):查看表属性 [英] MS Access db (mdb): viewing table attributes

查看:98
本文介绍了MS Access db(mdb):查看表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个受工作组保护的Access 2003数据库,我试图通过python查看
。目前,我正在尝试使用

win32包,我能够查看所有表名,但我不知道
知道如何查看表的属性。


我的代码:


导入win32com.client
来自pprint的
import pprint


#sample代码列出所选数据库中的所有表格

daoEngine = win32com.client.Dispatch(''DAO.DBEngine.36'' )

dtbs = daoEngine.OpenDatabase(''database_file.mdb'')

表格中的dtbs.TableDefs:

if table.Name [:4]<> "&MSys的QUOT;和table.Name [:4]<> ~TMP:

pprint(table.Name.encode(''utf-8''))

#the以上作品,但以下不是:

表格中的col.Fields:

pprint(col.Name,Col.Type,Col.Size)


我是获得len(Fields)= 0,我知道数据库表是

所有列(和数据)。是否还有一些资源

定义了TableDefs的所有属性?我无法为此找到一个

的python参考。


谢谢你,

Gau

I have an Access 2003 database, protected by a workgroup, that I am
trying to view through python. Currently, I''m attempting dao with the
win32 package, and I''m able to view all of the table names, but I don''t
know how to view the attributes of the tables.

My code:

import win32com.client
from pprint import pprint

#sample code to list all tables in the selected database
daoEngine = win32com.client.Dispatch(''DAO.DBEngine.36'')
dtbs = daoEngine.OpenDatabase(''database_file.mdb'')
for table in dtbs.TableDefs:
if table.Name[:4] <> "MSys" and table.Name[:4] <> "~TMP":
pprint(table.Name.encode(''utf-8''))
#the above works, but below does not:
for col in table.Fields:
pprint(col.Name, Col.Type, Col.Size)

I am getting that len(Fields) = 0, and I know the database tables are
all full of columns (and data). Is there also some resource that
defines all the properties of TableDefs? I wasn''t able to find a
python reference for this.

Thank You,
Gau

推荐答案

Em Sex,2006-03-10?* s 09:53 -0800, ga ***** @ gmail.com escreveu:
Em Sex, 2006-03-10 ?*s 09:53 -0800, ga*****@gmail.com escreveu:
我有一个受工作组保护的Access 2003数据库,我是
试图通过python查看。目前,我正在尝试使用
win32包,我能够查看所有表名,但我不知道如何查看表的属性。
I have an Access 2003 database, protected by a workgroup, that I am
trying to view through python. Currently, I''m attempting dao with the
win32 package, and I''m able to view all of the table names, but I don''t
know how to view the attributes of the tables.




我不知道你是否不能使用ODBC,但这就是我现在正在使用的

访问Access数据库。一个(可能)有用的片段:


import dbi,odbc#这个导入的顺序很重要!

connection = odbc.odbc(''Driver = {Microsoft Access Driver(* .mdb)};''+

''Dbq = C:\ database.mdb; Uid = Admin; Pwd =;'')

cursor = connection.cursor()

cursor.execute(''SELECT column1,column2,COUNT(*)FROM table''+

''WHERE column1< ; column2''+

''GROUP BY column1,column2'')

print cursor.fetchall()


AFAIK ,主要的优点是,如果需要,以后更容易更改到另一个

数据库,因为odbc模块使用与许多其他人相同的界面。




至少在我的应用程序中,它具有与Access本身相同的性能

(我的大多数查询只返回一些总和和组,没有一个

返回大块数据,因此大部分处理都保留在

Jet方面)。


希望有所帮助,

Felipe。


-

" Quem excele em empregar a for?§一个MILITAR subjulga OS前?? rcitos DOS

片尾POVOS SEM travar巴塔利亚,托马Cidades的fortificadas DOS片尾

POVOS SEM作为atacarédestr?3i的OS ESTADOS DOS片尾POVOS SEM lutas

prolongadas。 Deve lutar sob o C ?? u com o prop?3sito primordial da

''preserva?§?£o''。 Desse modo suas armas n?£se se embotar?£o,e os ganhos

poder?£o ser preservados。 Essa ?? a estrat ?? gia para planejar ofensivas。


- Sun Tzu,emA arte da guerra



I don''t know if you can''t use ODBC, but that''s what I''m using now to
access an Access database. A (maybe) useful snippet:

import dbi, odbc # The order of this import is important!
connection = odbc.odbc(''Driver={Microsoft Access Driver (*.mdb)};'' +
''Dbq=C:\database.mdb;Uid=Admin;Pwd=;'')
cursor = connection.cursor()
cursor.execute(''SELECT column1, column2, COUNT(*) FROM table'' +
''WHERE column1 < column2'' +
''GROUP BY column1, column2'')
print cursor.fetchall()

AFAIK, the main advantage is that it is easier to change to another
database later if needed, as the odbc module uses the same interface as
many others.

At least in my application, it has the same performance as Access itself
(most of my queries return just some sums and groups, none of them
return big chunks of data, so most part of the processing is kept on the
Jet side).

Hope that helps,
Felipe.

--
"Quem excele em empregar a for?§a militar subjulga os ex??rcitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destr?3i os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o C??u com o prop?3sito primordial da
''preserva?§?£o''. Desse modo suas armas n?£o se embotar?£o, e os ganhos
poder?£o ser preservados. Essa ?? a estrat??gia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"


这是我现在用来试用你的样本的完整代码:


import dbi,odbc

import win32com.client

来自pprint import pprint


#sample代码列出所选数据库中的所有表格

daoEngine = win32com.client.Dispatch (''DAO.DBEngine.36'')

dtbs = daoEngine.OpenDatabase(''database.mdb'')

表格中的dtbs.TableDefs:

if table.Name [:4]<> "&MSys的QUOT;和table.Name [:4]<> ~TMP:

pprint(table.Name.encode(''utf-8''))


#sample代码将执行一个带参数的存储查询

dbconn = odbc.odbc(''DSN = db; UID = user; PWD = pass'')

dbcursor = dbconn.cursor()

dbcursor.execute(''execute" result specs" 1'')

dbcursor.execute(''SELECT column1,column2,COUNT(*)FROM Weight'' +

''WHERE column1< column2''+

''GROUP BY column1,column2'')

#pprint(dbcursor。 fetchall())

dbcursor.close()

dbconn.close()


我收到错误:dbcursor.execute (''SELECT column1,column2,COUNT(*)

FROM Weight''+

dbi.program-error:[Microsoft] [ODBC Microsoft Access Driver]太少

参数。期待2.在EXEC中

Here is the full code I''m using now to try out your sample:

import dbi, odbc
import win32com.client
from pprint import pprint

#sample code to list all tables in the selected database
daoEngine = win32com.client.Dispatch(''DAO.DBEngine.36'')
dtbs = daoEngine.OpenDatabase(''database.mdb'')
for table in dtbs.TableDefs:
if table.Name[:4] <> "MSys" and table.Name[:4] <> "~TMP":
pprint(table.Name.encode(''utf-8''))

#sample code that will execute a stored query with parameters
dbconn = odbc.odbc(''DSN=db; UID=user; PWD=pass'')
dbcursor = dbconn.cursor()
dbcursor.execute(''execute "result specs" 1'')
dbcursor.execute(''SELECT column1, column2, COUNT(*) FROM Weight '' +
''WHERE column1 < column2 '' +
''GROUP BY column1, column2'')
#pprint(dbcursor.fetchall())
dbcursor.close()
dbconn.close()

I get the error: dbcursor.execute(''SELECT column1, column2, COUNT(*)
FROM Weight '' +
dbi.program-error: [Microsoft][ODBC Microsoft Access Driver] Too few
parameters. Expected 2. in EXEC


好的,我知道我错了:)我希望你的代码将

退还我的列名,但它希望我列出要使用的

列。我想知道如何通过python检索列表列表



ok, I know what I had wrong :) I was hoping that your code would
return the column names for me, but it was expecting me to list the
columns to use. I want to know how to retrieve that list of columns
through python.


这篇关于MS Access db(mdb):查看表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆