linux下 python连接mysql 入门

1. 环境安装

yum -y install python.x86_64
yum -y install MySQL-python.x86_64

 

2. 测试demo

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import os,sys
import  MySQLdb

try:
   db = MySQLdb.connect(host ='10.232.31.33', user='xxx', passwd='xxx', db ='xxx')
except MySQLdb.ERROR,e:
   print "Error %d:%s"%(e.args[0],e.args[1])
   exit(1)
cursor = db.cursor()
cursor.execute('select * from xxx_task')
result_set=cursor.fetchall()
print result_set
cursor.close()
db.close()

   将例子保存为test.py

 

3. 执行

   $chmod +x test.py

   $python  test.py

    如果看到有记录输出,说明连接成功了!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值