未优化,一个Python3.4的mysql连接类

这个类依赖于 dev.mysql.com/doc/connector-python/en/index.html

import mysql.connector
import json
import os
import framework as f

class Hel_MySql_Link:
	CONST_INS = {'db1':0,'db2':0}
	_cnx = 0
	_cursor = 0

	def __init__(self, database):
		dbInfo = json.loads(open(f.CONST_FILE_Mysql_Config).read())[database]
		print(dbInfo)
		self._cnx = mysql.connector.connect(user = dbInfo['user'], password = dbInfo['password'], host = dbInfo['host'], database = dbInfo['database'])
		
	def instance(database):
	
		if(Hel_MySql_Link.CONST_INS[database] == 0):
			Hel_MySql_Link.CONST_INS[database] = Hel_MySql_Link(database)
		
		return Hel_MySql_Link.CONST_INS[database]
		
	def cnx(self):
		return self._cnx
	
	def __del__(self):
		self._cnx.close()



你可能感兴趣的:(mysql,python)