免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5446 | 回复: 6
打印 上一主题 下一主题

python 连接 mysql 连接 sqlserver [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-24 11:17 |只看该作者 |倒序浏览
Python连接 mysql  连接 sqlserver
安装 python
Yum install python (相应ubuntu 用apt-get 千万别一个包一个包自己装 会死的很难看的 )
输入python 出现如下界面表示 安装成功。
Python 2.7 (r27:82500, Sep 16 2010, 18:03:06)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Ctrl+D 退出

安装 python 与mysql的连接器 mysqldb
输入 yum search mysqldb 出现如下信息
MySQL-python.i686
输入 yum install MySQL-python
安装 mysqldb成功。

测试文件 请看 utils 和 test1.py 如果不报错则通过。。。

test1.py  
[root@localhost test]# cat test1.py
#!/usr/bin/python
#-*- coding: UTF-8 -*-
from utils import msutil

util=msutil.MSUtil()

con=util.getcon()
cur=con.cursor()
sql='select top 100 wname from ware where wid > %s and wpid=%d'
cur.execute(sql,('1110057019',80)

for row in cur:
  temp=row[0]
  flag=isinstance(temp, unicode)
     =temp.encode('utf8')
  print dd

cur.close()
con.close()

utils 包中 mysqlutil.py

[root@localhost utils]# cat mysqlutil.py
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import MySQLdb

class MySQLdbUtil:

    def getCon():
        con=MySQLdb.connect(host='10.10.242.237' ,user='root' ,passwd='123456')
        con.select_db('python')
        return con
      



安装 python 与sqlserver连接器 pymssql
输入 yum search pymssql 出现如下信息
pymssql.i686 : A simple database interface to MS-SQL for Python
输入 yum install pymssql
安装成功
测试文件请看utils 如果不报错 通过。。。。。(测试类暂无,与上面的mysql test1.py类似)

Utils 包中 msutil.py
[root@localhost utils]# cat msutil.py
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import pymssql

class MSUtil:
  def __init__(self):
      print 'this ms util'
  def getCon(self):
     con = pymssql.connect(host='10.10.224.150',user='testwrite',password='654321',database='product',charset='gbk')
     return con
    # print   'getcon is diaoyong'

论坛徽章:
0
2 [报告]
发表于 2011-06-24 11:21 |只看该作者
自己顶一下 呵呵

论坛徽章:
0
3 [报告]
发表于 2011-06-28 15:13 |只看该作者
怎么没有人顶呢?这是我对互联网唯一的贡献呀 。。。。

论坛徽章:
0
4 [报告]
发表于 2011-06-28 15:14 |只看该作者
怎么没人顶呢?? 这是我对互联网唯一的贡献呀。。。

论坛徽章:
0
5 [报告]
发表于 2011-06-28 16:05 |只看该作者
怎么没人顶呢?? 这是我对互联网唯一的贡献呀。。。
longmm1988 发表于 2011-06-28 15:14


顶一下。
你要是编辑一下,代码都用代码格式,这样就不会显得编排很乱了。

论坛徽章:
0
6 [报告]
发表于 2011-08-09 16:49 |只看该作者
可能大家都懂吧

论坛徽章:
0
7 [报告]
发表于 2011-08-09 19:34 |只看该作者
代码质量有待提高, 本来很简单的, 整得这么凌乱。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP