惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
博客园_首页
N
News | PayPal Newsroom
有赞技术团队
有赞技术团队
The Hacker News
The Hacker News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
S
SegmentFault 最新的问题
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
P
Privacy & Cybersecurity Law Blog
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Schneier on Security
Schneier on Security
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
量子位
Forbes - Security
Forbes - Security
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
SecWiki News
SecWiki News
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
Recorded Future
Recorded Future
A
About on SuperTechFans
J
Java Code Geeks
The Register - Security
The Register - Security
PCI Perspectives
PCI Perspectives
H
Hacker News: Front Page
V2EX - 技术
V2EX - 技术
S
Secure Thoughts
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
Y
Y Combinator Blog
AWS News Blog
AWS News Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
IT之家
IT之家
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
L
LangChain Blog
F
Full Disclosure
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The GitHub Blog
The GitHub Blog

博客园 - Haozes

新Blog 拼写检查算法 Golang 版 Windbg 离线调试.Net 程序入门 (译)你必须知道的位运算技巧 Low Level Bit Hacks You Absolutely Must Know Windows 下 命令行增强工具 WPF Layout & Image异步加载 几篇文章了解编译原理 WPF Binding Validation 数据验证 WPF 实现Loading效果 推荐一个.NET 命令行参数Parser 库 常用开发工具介绍 使用.Net Memory Profiler 分析.Net程序内存泄露 使用Mdbg.exe 调试.Net 程序 WPF 多语言方案 .Net 2 Tip :捕获CSE和Thread.Timer与Thread.Sleep比较 使用CSharp Driver操作Mongodb介绍 运行.Net4.0程序是否要安装之前的.Net版本 javascript Disable <div> or other tag in Other Browser like FF,Chrome Delphi 无类型参数传递动态数组和静态数组
使用Python操作MSSQL数据库.
Haozes · 2011-09-27 · via 博客园 - Haozes

有时想更新或迁移部分SqlServer数据,用SQL只会整几句select,高级点的连个游标都玩不转有木有?拿起VS写个小程序不停的改又编译很烦,有木有?

想用脚本写了有木有?

本文介绍使用python,adodbapi操作数据库:

adodbapi 库

python 可以操作mssqlserver的类库有几个,支持python3.x的目前没几个靠谱的.adodbapi是使用ADO操作的.(不能跨平台?需要么?)

如:

'''

     self.xlBook=self.xlApp.Workbooks.Open(filename)

     sht = self.xlBook.Worksheets(sheet)   
      sht.Cells(row, col).Value = value 

'''

详细过程:

首先添加引用:

import adodbapi
adodbapi.adodbapi.verbose = False # adds details to the sample printout
import adodbapi.ado_consts as adc

创建连接:

    Cfg={'server':'192.168.29.86\\eclexpress','password':'xxxx','db':'pscitemp'}
    constr = r"Provider=SQLOLEDB.1; Initial Catalog=%s; Data Source=%s; user ID=%s; Password=%s; " \
         % (Cfg['db'], Cfg['server'], 'sa', Cfg['password'])
    conn=adodbapi.connect(constr)

其中Cfg是个key-value字典,constr格式化语法是python2.x常用,在3.x可以使用下面的.

执行sql语句:

    cur=conn.cursor()
    sql='''select * from softextBook where title='{0}' and remark3!='{1}'
    '''.format(bookName,flag)
    cur.execute(sql)

    data=cur.fetchall()
    cur.close()

其中三个引号类似于C#字符串前的"@",python中字符串可以用一个或两个,三个括起来,format格式化语法也类似

执行存储过程:

   

 #假设proName有三个参数,最后一个参数传了null

    ret=cur.callproc('procName',(parm1,parm2,None))

    conn.commit()

关闭连接

conn.close()

很简单有木有?

更多代码示例见安装目录下里的unit test代码:

C:\Python31\Lib\site-packages\adodbapi\tests