[英]Error while executing a sql query using MySQL db and python client
[英]Getting brackets while executing query into mysql database using python
我需要幫助!! 我編寫了一個python代碼,用於查詢數據庫並在Linux shell提示符下打印結果,代碼如下:
#!/usr/bin/python
import MySQLdb
import sys
import config
import csv
db = MySQLdb.connect(config.host,config.user,config.password,config.dbname)
cursor=db.cursor()
print "Connected to the Mysql database"
cursor.execute("use " + config.dbname)
cursor.execute('SELECT DISTINCT LEARNER FROM EMS_data LIMIT 5')
result= cursor.fetchall()
print result
db.commit()
cursor.close()
這就是我得到的:
(("'Fang ",), ("'Nikhil '",), ("'Gavin '",), ("'Vamsi'",), ("'Shah'",))
如何去除這些牙箍..?
cursor.fetchall()返回的結果是包含所獲取數據的元組元組。 每個元組代表一個獲取的行。
因為你正在打印一個元組元組,所以你會看到大括號。 為了以首選格式顯示數據,您必須迭代結果並相應地打印每一行。
下面的示例將在新行上打印每一行,並使用垂直條分隔列:
for row in result:
print " | ".join(row)
cursor.fetchall()返回的結果是包含所獲取數據的元組元組。 每個元組代表一個獲取的行。
下面的第一行代碼將逐個使用元組。
第二行將打印不取元組本身,但正好是第一行元素(沒有逗號和括號)
for row in result:
print(row[0])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.