python链接mysql

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 18:00:40 2016

@author: wu-yj
"""

# encoding: utf-8
import pymysql
# 打开数据库连接
db = pymysql.connect(host='10.1.2.250',port=3306,user='greener',passwd='kr123456',db='mios' )
print('***********************') # 使用cursor()方法获取操作游标
cursor = db.cursor()

# SQL 查询语句
sql = "select TRADE_ID from t_trade"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()

TRADE_ID=[]
for row in results:
TRADE_ID.append(row[0])

# clientname = row[0]
# businessscope = row[1]
# marketarea = row[2]
# transtotal = row[3]
# 打印结果
# print("fname=%s,lname=%s,age=%d,sex=%s,income=%d" ,clientname, businessscope, marketarea, transtotal)

#for i in range(0,len(TRADE_ID)):
print("trade_id:", TRADE_ID)
except:
print("Error: unable to fecth data")
# 关闭数据库连接
db.close()

 

python链接mysql很简单了,直接拿去用吧,不谢!

posted @ 2016-04-13 14:11  酷小子  阅读(264)  评论(0编辑  收藏  举报