Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > Python ADO demo
【标  题】:Python ADO demo
【关键字】:Python,ADO,demo
【来  源】:http://blog.chinaunix.net/article.php?articleId=22029&blogId=6123

Python ADO demo

Your Ad Here

python 操作access, 使用win32com 库.

如果你的python 版本是2.3或者以下版本需要到http://cjkpython.i18n.org/下载中文支持包.

本程序是一个简单的查询表中全部数据的demo, 包括unicode -> gb2312 的转化, 

具体的细节请阅读全文...

# -*- coding: gb2312 -*-
FileInfo = """\
#====================================================================
#========Project:  --
#========Module:  A ADO Demo
#========Author:  junsi
#========Email :  smppms2002@yahoo.com.cn or opindd@hotmail.com
#========Create Date: 2005.02.28
#========Update Date: 2005-04-18 add sql  
#====================================================================
"""

import cjkcodecs.aliases
import win32api
import win32com.client
import string

     
def EncodeVlue( AStr ):
 if isinstance(AStr, unicode):
  return AStr.encode("gb2312")
 else:
  return AStr

def AdoDemo():
    try:
        conn = win32com.client.Dispatch("ADODB.Connection")
        dsn ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=score.mdb;"
        conn.Open(dsn)
        if conn.State == 1:
            print "We've connected to the database."
        else:
            print "We failed to connect to the database."       
       
        rs = win32com.client.Dispatch("ADODB.Recordset")
        rs.ActiveConnection = conn
        rs.Open( "score" )
        if rs:
            print "Query Data Success"
        else:
            print "Query Data Failure"
       
       
        #if don't use EncodeVlue error: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
        while not rs.EOF:
            for x in range(rs.Fields.count): 
                print  EncodeVlue( rs.Fields.Item(x).Value )
            rs.MoveNext()
    finally:
        print "DataBase Error = ", win32api.GetLastError()
        conn = None
        rs = None

if __name__ == "__main__":
    AdoDemo()
    print "Demo End"
   
"""
table socre sql
drop   table  score ;
create table  score
(
     zkz                 text                          ,
     sfz                 text                          ,
     mz                  text                          ,
     sx                  text                          ,
     yy                  text                          ,
     zz                  text                         
); 

create index ix_score on score( zkz,sfz ) with primary ;  
"""   

python 电子书籍:【上一篇】
Python 开发工具:【下一篇】
【相关文章】
  • python 电子书籍
  • Solaris9 x86 下安装python
  • python in microsoft.com
  • 我在linuxhero的python的学习的版
  • Python书籍连接
  • Python Challenge的答案
  • Python类与对象
  • ado .net
  • ado .net calling stored Procedure
  • 在perl和python之间,我选择了perl
  • 【随机文章】
  • 软件项目规模估计方法介绍
  • Java的秘密:使用全屏幕模式
  • 在linux下如何设置IP地址并将其激活
  • 深入了解微软个人服务器PWS之二
  • ORACLE 使用AUTOTRACE功能
  • 在win2000、win2003上安装卡巴6的方法
  • 关于c++类成员函数重载
  • 怎样从一名程序员过度到项目经理(整理自csdn论坛)
  • 关于指针和内存的几个问题
  • JScript 方法 - escape 方法
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.