python 操作MySQL存储PyMySQL写成类
使用连接数据库 pymysql库 例子
import pymysqlfrom sqlalchemy import create_engineclass Mysql(): def __init__(self): self.ip = "127.0.0.1" self.port = "3306" self.port2 = 3306 self.username = "root" self.secret = "xxxxxx" self.dbname = "dbname" self.tablename = "tablename" def connect(self): url = 'mysql+pymysql://'+self.username+':'+self.secret+'@'+self.ip+':'+self.port+'/'+self.dbname+'?charset=utf8mb4' print(url) connect = create_engine(url) return connect def connect2(self): db = pymysql.connect(host=self.ip, user=self.username, password=self.secret, port=self.port2, database=self.dbname) cursor = db.cursor() # 获得指针 return cursor def select(self): cursor = self.connect2() sql = 'select * from dbname.tablename;' cursor.execute(sql) # 执行 return cursor