这是我的代码片段...
首先,我调用了 __execute_query 函数来插入一些行,然后选择几行。但是,如果执行选择查询,则该函数返回 None 或 Empty rows ... 但是手动 sql 查询返回几行。
def __execute_query(self, query, parameters = [], set_row_id = False):
conn = MySQL.get_conn()
cursor = conn.cursor()
cursor.execute(query, parameters)
result = None
query_type = query[0:query.find(" ")]
query_type = query_type.lower()
if query_type in ('insert', 'update', 'delete'):
result = cursor.rowcount > 0
if result:
conn.commit()
if query_type == 'insert' and set_row_id == True:
"""
Update the object with the newly saved row ID and mark
the object as not new.
"""
self.__new_row = False
self.__row_id = conn.insert_id()
if self.__row_id == 0 or self.__row_id == "0":
self.__row_id = cursor.lastrowid
else:
result = cursor.fetchall()
cursor.close()
return result