python管理mysql

1. 方法一
pip3 config set global.index-url
https://pypi.tuna.tsinghua.edu.cn/simple
yum -y install sqlalchemy
pip3 install pandas
import pandas as pd
from sqlalchemy import create_engine
class Python_Mysql ( object ):
def init ( self ):
print ( "test" )
def getEngine ( seft ):
host = input ( "sign mysql
server host:" )
username = input ( "sign mysql
username:" )
password = input ( "sign mysql
password:" )
databasename = input ( "sign
database name:" )
port = input ( "sign mysql
port:" )
engine = create_engine ( f"mysql+pymysql:// { use
rname } : { password } @ { host } :
{ port } / { databasename } " )
return engine
def querySql ( self , conn ):
sql = input ( "sign your sql:" )
return
pd . read_sql ( sql = sql , con = conn )
if name == "main" :
demo = Python_Mysql ()
#sql=input("sign sql:")

sql="select * from user"

rs = demo . querySql ( demo . getEngine ())
print ( rs )
2. 方法二

  1. 设置清华镜像站(从国内下载安装包,提高下载和安
    装速度)
  2. 安装 pandas 数据分析工具 (pandas 是知名的数据分析
    工具, pandas 有完整的读取数据的工具,以及
    DateFrame 数据框架,用于保存从数据库中读取的数
    据 )
  3. 安装 pymysql 连接器( oracle 为开发者提供的 python
    管理 mysql 的工具,通过这个工具,就恶意在不替原
    有代码的情况下,应对数据库软件的升级)
    pip3 config set global.index-url
    https://pypi.tuna.tsinghua.edu.cn/simple
    yum -y install pandas
    yum -y install pymysql
    >>> import pandas as pd
    >>> import pymysql
    >>> conn = pymysql . connect (
    ... host = '10.1.1.100' ,
    ... user = 'zhangmin' ,
    ... password = 'zhangmin' ,
    ... database = 'test' ,
    ... port = 3306
    ... )
    >>> conn
    < pymysql . connections . Connection object at
    0x7f9e24ba2c88 >
    >>> cursor = conn . cursor ()
    >>> cursor
    < pymysql . cursors . Cursor object at
    0x7f9e24ba2668 >
    >>> sql = "select * from user"
    >>> cursor . excute ( sql )
    Traceback ( most recent call last ):
    File "<stdin>" , line 1 , in < module > AttributeError : 'Cursor' object has no
    attribute 'excute'
    >>> cursor . execute ( sql )
    3
    >>> cursor . description
    (( 'id' , 3 , None , 11 , 11 , 0 , False ),
    ( 'username' , 253 , None , 180 , 180 , 0 , False ),
    ( 'password' , 253 , None , 180 , 180 , 0 , False ))
    >>> desc = cursor . description
    >>> res
    3
    >>> res = cursor . fetchall ()
    >>> res
    (( 1 , 'aaa' , '123' ), ( 8 , 'baba' , 'pipi' ),
    ( 20 , 'aaaaaaaa' , 'bbbbbbbb' ))
    >>> desc
    (( 'id' , 3 , None , 11 , 11 , 0 , False ),
    ( 'username' , 253 , None , 180 , 180 , 0 , False ),
    ( 'password' , 253 , None , 180 , 180 , 0 , False ))
    >>> [ item [ 0 ] for item in desc ]

'id' , 'username' , 'password'

>>> col = [ item [ 0 ] for item in desc ]
>>> df = pd . DataFrame ( res , columns = col )
>>> df
id username password
0 1 aaa 123
1 8 baba pipi
2 20 aaaaaaaa bbbbbbbb 总结

  1. 和 shell 脚本一样 python 文件也可以称为 py 脚本,也是
    将 pyhton 指令做一个集合
  2. 为了脚本更加的智能化和自动化,添加选择语句(智
    能)循环语句(自动化)
  3. 同时为了开发效率,可读性,做了方法,类,模块
相关推荐
田里的水稻几秒前
C++_python_相互之间的包含调用方法
c++·chrome·python
2501_941870565 分钟前
面向微服务熔断与流量削峰策略的互联网系统稳定性设计与多语言工程实践分享
开发语言·python
modelmd10 分钟前
Go 编程语言指南 练习题目分享
开发语言·学习·golang
带土128 分钟前
4. C++ static关键字
开发语言·c++
C++ 老炮儿的技术栈35 分钟前
什么是通信规约
开发语言·数据结构·c++·windows·算法·安全·链表
@大迁世界38 分钟前
TypeScript 的本质并非类型,而是信任
开发语言·前端·javascript·typescript·ecmascript
kaico201841 分钟前
MySQL的索引
数据库·mysql
栗子叶43 分钟前
Java对象创建的过程
java·开发语言·jvm
GIS之路1 小时前
GDAL 实现矢量裁剪
前端·python·信息可视化
Amumu121381 小时前
React面向组件编程
开发语言·前端·javascript