本文整理汇总了Python中mysql.Mysql.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Mysql.connect方法的具体用法?Python Mysql.connect怎么用?Python Mysql.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mysql.Mysql
的用法示例。
在下文中一共展示了Mysql.connect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MySQLDB
# 需要导入模块: from mysql import Mysql [as 别名]
# 或者: from mysql.Mysql import connect [as 别名]
class MySQLDB(object):
def __init__(self, host, db, user, password):
self.connection = None
self.host, self.db, self.user, self.password = host, db, user, password
self.connect()
def connect(self):
if not self.connection:
self.connection = Mysql(host=self.host, db=self.db, user=self.user, password=self.password)
self.connection.connect()
return self.connection
def insertSQL(self, table, columnValueDict):
sql = 'INSERT INTO %s(%s) VALUES(%s)' % (table, ",".join(columnValueDict.keys()), ",".join(self.to_str(columnValueDict.values())))
return self.connection.execute(sql)
def insert(self, table, columns, data):
sql = 'INSERT INTO %s(%s) VALUES(%s)' % (table, ",".join(columns), ",".join(self.to_str(data)))
return self.connection.execute(sql)
def insertSQLIgnore(self, table, columnValueDict):
sql = 'INSERT IGNORE INTO %s(%s) VALUES(%s)' % (table, ",".join(columnValueDict.keys()), ",".join(self.to_str(columnValueDict.values())))
return self.connection.execute(sql)
def execute(self, sql):
return self.connection.execute(sql)
def commit(self):
self.connection.commit()
def close(self):
self.connection.commit()
self.connection.close()
self.connection = None
def to_str(self, arr):
a = arr[:]
for i in range(len(arr)):
a[i] = "'%s'" % arr[i]
return a