39 lines
1023 B
Python
39 lines
1023 B
Python
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import pymysql
|
|
import configparser
|
|
cfp = configparser.ConfigParser()
|
|
cfp.read("config.ini")
|
|
|
|
class Mysql():
|
|
def __init__(self):
|
|
ip = cfp.get("DB","IP")
|
|
self.conn = pymysql.connect(host=ip, user='root', password='root', port=3306, db='datanodedb',
|
|
charset='utf8');
|
|
print(self.conn)
|
|
try:
|
|
self.cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
|
|
except pymysql.err.MySQLError as _error:
|
|
raise _error
|
|
|
|
def _Query(self, sql):
|
|
|
|
print(sql)
|
|
try:
|
|
self.cursor.execute(sql)
|
|
data = self.cursor.fetchall()
|
|
return data
|
|
except pymysql.err.MySQLError as _error:
|
|
raise _error
|
|
|
|
|
|
def _Operate(self, sql):
|
|
try:
|
|
print(sql)
|
|
self.cursor.execute(sql)
|
|
# 提交数据
|
|
self.conn.commit()
|
|
except pymysql.err.MySQLError as _error:
|
|
raise _error
|