首页 > 代码库 > python数据库操作

python数据库操作

#!/usr/src/python
# -*- coding:utf-8 -*-

import pymysql
import socket,struct
def sql_exe(v_sql):
conn = pymysql.Connect(host=‘192.168.110.133‘,port=3306,user="admin",password="mysql123",database="hostmgr",charset=‘utf8‘)
cursor = conn.cursor()
v = cursor.execute(v_sql)
result = cursor.fetchall()
# result = cursor.fetchone()
# result = cursor.fetchmany(2)
print(result)

cursor.close()
conn.close()

# 用户信息
def user_info():
# user_name=input(‘请输入用户名:‘).strip()
# pwd=input(‘请输入用密码:‘).strip()
v_sql=‘select mu.username,mu.email,md.deptname from mgr_empuser mu,mgr_department md where mu.deptid=mu.deptid‘
return v_sql

# 显示当前用户管理的所有主机
def user_mgr_host_info(v_usrname):
v_sql=‘select host_id,socket.inet_ntoa(struct.pack("I",socket.htonl(host_ip))) from mgr_host where username="%s"‘%(v_usrname)
return v_sql


if __name__ == ‘__main__‘:
sql1=user_info()
sql_exe(sql1)

sql2 = user_mgr_host_info(‘root‘)
print(sql2)
sql_exe(sql2)

python数据库操作