Pal*_*shi 5 python mysql mysql-python
我使用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()
Run Code Online (Sandbox Code Playgroud)
和输出是
Done
S E L E C T
None
Run Code Online (Sandbox Code Playgroud)
虽然查询在工作台中运行良好
欢迎任何形式的帮助/指导.
你应该像这样修改你的代码
代码:
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
Run Code Online (Sandbox Code Playgroud)
笔记:
cursor.execute是像 list.sort() 这样的内存操作cursor object或使用来获取fetch*()