我使用python-2.7和newbie连接mysql/mysql-python连接器。
我只想通过以下查询来检索数据-
从d_id、d_link、d_name中选择d_details
但是它给/返回None。以下是我的代码-
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()
输出是
Done
S E L E C T
None
尽管查询在工作台中运行良好
欢迎任何形式的帮助/指导。
发布于 2015-12-14 22:50:33
您应该像这样修改代码
代码:
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*()
获得。https://stackoverflow.com/questions/34282549
复制