Python MySql API
Python MySql API
我正在使用 python 为 mysql 创建一个 API。我正在尝试获取多列。在 SQL 查询中,你通常会做类似 SELECT col1, col2.... FROM tablename;
我的 python API 看起来是这样的:
def get_multiple_columns(self, tablename, limit, *colname):
sql = 'Select {} From {} limit {}'.format(*colname, tablename, limit)
return self.query_exec_fetch_res(sql)
为了参考,我附上了其余的代码:
class database(object):
def __init__(self, host = MYSQL_DB_IP,
port = MYSQL_DB_PORT,
username = MYSQL_DB_USERNAME,
password = MYSQL_DB_PASSWORD,
dbname = MYSQL_DB_TRADING):
self.dbname = dbname
self.host = host
self.port = port
self.username = username
self.password = password
self.session = None
self.cursor = None
self.connect_to_db()
# Function to connect to database.
def connect_to_db(self):
if not self.session:
try:
self.session = pymysql.connect(host=self.host,
port=self.port,
user=self.username,
passwd=self.password,
db=self.dbname)
self.cursor = self.session.cursor()
except Exception as e:
print("database init failed \n{}".format(e))
def get_db_client(self):
return self.session
def get_db_cursor(self):
return self.cursor
def disconnect_from_db(self):
if self.session:
self.session.close()
self.session = None
self.cursor = None
#function to execute query and fetch the result
def query_exec_fetch_res(self,query):
self.cursor.execute(query)
return self.cursor.fetchall()
def get_mysql_version(self):
sql = "SELECT VERSION()"
return self.query_exec_fetch_res(sql)
def get_all_column_info(self, tablename):
sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \"{}\"".format(tablename)
return self.query_exec_fetch_res(sql)
def get_all_column_names(self, tablename):
result = self.get_all_column_info(tablename)
return [row[3] for row in result]
def get_info_where_clause(self,tablename,colname,fieldname, value):
sql = "SELECT {} FROM {} WHERE {}=\"{}\"".format(colname, tablename, fieldname, value)
result = self.query_exec_fetch_res(sql)
info = [ row for row in result ]
return info
def get_multiple_columns(self, tablename, limit, *colname):
sql = 'Select {} From {} limit {}'.format(*colname, tablename, limit)
return self.query_exec_fetch_res(sql)
除get_multiple_columns 函数外,所有函数都有效。有人可以帮我吗?
在您的方法 get_multiple_columns
中,参数 colname
作为 tuple
出现,因此您需要:
sql = 'Select {} From {} limit {}'.format(', '.join(colname), tablename, limit)
我正在使用 python 为 mysql 创建一个 API。我正在尝试获取多列。在 SQL 查询中,你通常会做类似 SELECT col1, col2.... FROM tablename; 我的 python API 看起来是这样的:
def get_multiple_columns(self, tablename, limit, *colname):
sql = 'Select {} From {} limit {}'.format(*colname, tablename, limit)
return self.query_exec_fetch_res(sql)
为了参考,我附上了其余的代码:
class database(object):
def __init__(self, host = MYSQL_DB_IP,
port = MYSQL_DB_PORT,
username = MYSQL_DB_USERNAME,
password = MYSQL_DB_PASSWORD,
dbname = MYSQL_DB_TRADING):
self.dbname = dbname
self.host = host
self.port = port
self.username = username
self.password = password
self.session = None
self.cursor = None
self.connect_to_db()
# Function to connect to database.
def connect_to_db(self):
if not self.session:
try:
self.session = pymysql.connect(host=self.host,
port=self.port,
user=self.username,
passwd=self.password,
db=self.dbname)
self.cursor = self.session.cursor()
except Exception as e:
print("database init failed \n{}".format(e))
def get_db_client(self):
return self.session
def get_db_cursor(self):
return self.cursor
def disconnect_from_db(self):
if self.session:
self.session.close()
self.session = None
self.cursor = None
#function to execute query and fetch the result
def query_exec_fetch_res(self,query):
self.cursor.execute(query)
return self.cursor.fetchall()
def get_mysql_version(self):
sql = "SELECT VERSION()"
return self.query_exec_fetch_res(sql)
def get_all_column_info(self, tablename):
sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \"{}\"".format(tablename)
return self.query_exec_fetch_res(sql)
def get_all_column_names(self, tablename):
result = self.get_all_column_info(tablename)
return [row[3] for row in result]
def get_info_where_clause(self,tablename,colname,fieldname, value):
sql = "SELECT {} FROM {} WHERE {}=\"{}\"".format(colname, tablename, fieldname, value)
result = self.query_exec_fetch_res(sql)
info = [ row for row in result ]
return info
def get_multiple_columns(self, tablename, limit, *colname):
sql = 'Select {} From {} limit {}'.format(*colname, tablename, limit)
return self.query_exec_fetch_res(sql)
除get_multiple_columns 函数外,所有函数都有效。有人可以帮我吗?
在您的方法 get_multiple_columns
中,参数 colname
作为 tuple
出现,因此您需要:
sql = 'Select {} From {} limit {}'.format(', '.join(colname), tablename, limit)