Sufficiency Economy
Group Blog
 
All blogs
 
Python อ่านโครงสร้างของ Table ของ MS Access




วันนี้เรามาดูวิธ๊หาชื่อตาราง และชื่อField ของฐานข้อมูล MS Access กันดีกว่า (ใครเดาได้หรือไม่ ว่าจะเอาไปทำอะไรต่อ?)
วิธ๊การก็ง่ายๆ โดยเรียกข้อมูล ผ่าน object "cursor"

1) strConn = 'DRIVER={Microsoft Access Driver (*.mdb)};DBQ=' + E1.get()+'.mdb'
2) db = pyodbc.connect(strConn,autocommit=True)
3) cursor = db.cursor()


ต่อไปก็วนลูป ดึงชื่อตารางจากฐานข้อมูลที่เลือก แล้วเอาไป
เก็บในตัวแปรแบบ List ที่ชื่อว่า ListTable
for row in cursor.tables(tableType='TABLE'):
ListTable.append(row.table_name)

ต่อไปก็เลือกที่ละ Table มาหาชื่อ Field แล้วเก็บในตัวแปรแบบString ชื่อ L
for x in ListTable:
L = L + x + "n"
L = L + "Fields Detail" + "n"
for row in cursor.columns(table=x):
L = L + "t" +row.column_name + "n"

สุดท้ายก็จะได้ข้อมูลชื่อตาราง และชื่อField ในตัวแปร L

แล้วนำมาแสดง ใน Text widgets โดยนำ scrollbar widgets มาติดไว้ลองดูใน sourcecode ล่ะกัน ที่นี้เลย




Create Date : 01 มิถุนายน 2554
Last Update : 1 มิถุนายน 2554 20:06:06 น. 1 comments
Counter : 1433 Pageviews.

 
ลึกซึ้งมากๆ


โดย: boodoox วันที่: 1 มิถุนายน 2554 เวลา:20:19:41 น.  

ชื่อ :
Comment :
  *ใช้ code html ตกแต่งข้อความได้เฉพาะสมาชิก
 

wink99_th
Location :
พิษณุโลก Thailand

[Profile ทั้งหมด]

ฝากข้อความหลังไมค์
Rss Feed
Smember
ผู้ติดตามบล็อก : 8 คน [?]




New Comments
Friends' blogs
[Add wink99_th's blog to your web]
Links
 

 Pantip.com | PantipMarket.com | Pantown.com | © 2004 BlogGang.com allrights reserved.