import pymysql
class py_mysql():
def __init__(self,host,user,pwd,db):
self.user=user
self.pwd = pwd
self.db = db
self.host = host
self.connet=pymysql.connect(host=self.host,user =self.user,password=self.pwd,database=self.db,charset='utf8',)
def cha(self,sql):
cursor=self.connet.cursor()
cursor.execute(sql)
results = cursor.fetchall()
print(results)
return results
def insert(self,sql):
cursor = self.connet.cursor()
try:
cursor.execute(sql)
self.connet.commit()
last_id = cursor.lastrowid
print(last_id)
except Exception as e:
print(e)
self.connet.rollback()
cursor.close()
self.connet.close()
date=py_mysql("127.0.0.1","root","123456","test")
date.cha("select * from person")
sql = "INSERT INTO person(user_id, username,sex,email) VALUES (125,'wan','nan','4704661@qq.com')"
date.insert(sql)