在linux环境下用python2操作mysql

有时候需要在服务器上通过python脚本操作数据库,这时就有一个问题,linux上默认安装的python2.7,但是没有操作mysql脚本需要的相关模块。

解决:

1、下载pip

wget https://bootstrap.pypa.io/get-pip.py

可以看到安装文件被下载到了/root/script的目录下

2、安装pip

3、利用pip安装操作数据库的pymysql模块

安装好了之后,就可以用python脚本操作数据库了。附上一个脚本

# -*- coding: utf-8 -*-
import pymysql

conn = pymysql.connect(host="ip",port=3307,user="root", passwd="123", db="db")
cursor = conn.cursor()
sql="select id,name_short from project where type in (15,17) "
cursor.execute(sql)
projects = cursor.fetchall()
for project in projects:
    #切换到数据库
    sqlCustomer="use ssb_customer_"+str(project[0])
    cursor.execute(sqlCustomer)
    sqlqueryDraft="select count(1) from ssb_r_draft_version"
    cursor.execute(sqlqueryDraft)
    draftCount = cursor.fetchall()
    if draftCount[0][0]>0:
        print(project[1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值