pymysql
import pymysql
class Mysql:
def __init__(self):
self.host = "192.168.0.1"
self.port = 3306
self.user = "root"
self.password = "123456"
self.db = "pymysql"
self.charset = "utf8"
self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password,
db=self.db, charset=self.charset)
self.cursor = self.conn.cursor()
def Create_Table(self, sql):
self.cursor.execute(sql)
self.conn.commit()
self.cursor.close()
self.conn.close()
def Select(self, sql):
self.cursor.execute(sql)
results = self.cursor.fetchall()
for row in results:
print(row)
self.conn.commit()
self.cursor.close()
self.conn.close()
def Insert(self, sql, *data):
for sql_data in data:
self.cursor.executemany(sql, sql_data)
self.conn.commit()
self.cursor.close()
self.conn.close()
def Update_or_Delete(self, sql):
self.cursor.execute(sql)
self.conn.commit()
self.cursor.close()
self.conn.close()
sql = "DELETE FROM departments WHERE dep_id=6;"
Mysql().Create_Table(sql)
Mysql().Select(sql)
Mysql().Insert(sql, data)
Mysql().Update_or_Delete(sql)