python调用shell链接mysql数据库进行操作

话不多说,直接上code

python 脚本:test_mysql.py

#!/user/bin/env python
#coding=utf-8


import subprocess

def system_command(command):
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None, shell=True)
    result = process.stdout.readlines()
    return result

if __name__ == "__main__":
    result = system_command('/root/test_mysql.sh')
    print result

shell脚本:test_mysql.sh

#!/bin/bash


exe_secelt(){
HOST_NAME="192.168.165.101";
PORT="3306";
USER_NAME="root";
PASS_WORD="123456";
DBNAME="pear1.9.0-data";
TABLE_NAME="host";
HOST=$1;
select_sql="select * from ${TABLE_NAME} where name='${HOST}'";


mysql -h${HOST_NAME} -P${PORT} -u${USER_NAME} -p${PASS_WORD} ${DBNAME} -e "${select_sql}"
}
HOST=$1;
exe_secelt "$HOST"
运行python 脚本

python test_mysql.py

注:在编写shell的工程中发现一个很有趣的mysql问题


python调用shell链接mysql数据库进行操作_第1张图片

在此脚本中,我select了名称叫order的表,报错:

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL serve
r version for the right syntax to use near 'order' at line 1


原因竟是因为我order表中存在名为order的字段。

你可能感兴趣的:(shell)