我正在使用python-2.7和newbie到mysql / mysql-python连接器。
我只想简单地使用以下查询来检索数据 -
SELECT d_id,d_link,d_name FROM d_details
但是它给出/返回无。以下是我的代码 -
def getdbconnection(self):
try:
self.cnx = mysql.connector.connect(user='abc',password='xxx',host = 'localhost',port = 'xxx', database='details',buffered=True)
print "Done"
self.cursor = self.cnx.cursor()
except MySQLdb.Error as error:
print "ERROR IN CONNECTION"
def selectst(self):
try:
print "S E L E C T"
self.d_deta = self.cursor.execute("SELECT d_id,d_link,d_name FROM `d_details`")
print self.d_deta
except MySQLdb.Error as error:
print "---------------------"""
print(error)
self.cnx.commit()
AND输出
Done
S E L E C T
None
虽然查询在工作台中运行良好
欢迎任何形式的帮助/指导。
答案 0 :(得分:1)
您应该像这样修改您的代码
<强>代码:强>
def selectst(self):
try:
print "S E L E C T"
self.cursor.execute("SELECT d_id,d_link,d_name FROM `d_details`")
print self.cursor.fetchall() # or fetchone() for one record
except MySQLdb.Error as error:
print "---------------------"""
print(error)
#self.cnx.commit() there is no need of commit here since we are not changing the data of the table
备注:强>
cursor.execute
是一个像list.sort()cursor object
或使用fetch*()