猿问

MYSQL选择只选择字符而不是名称

我的数据库中有 2 列被调用description,url 我想选择这两列,但只description向用户显示。问题是,当我遍历 fetchall 命令时,它要么选择这两个字符,要么只选择单个字符。这是我的代码:


connection = pymysql.connect(user='fake', passwd='fake', host='db', port=1111, db='fake_db_name')

searchResult = []

try:

    with connection.cursor() as cursor:

        pushpin_sql = "SELECT description, url FROM pushpin WHERE pushpin.description LIKE '%" + str(searchTerm) + "%'"

        result = cursor.fetchall()

        for col in result:

            searchResult+=col

        connection.commit()

finally:

    connection.close 

输出是这样的:


这是一只可爱的狗


http://jiangzhenling.com/img/CS6400/dog/dog1.png


如果我将其更改为:


        for col1,col2 in result:

            searchResult+=col1

然后它只连接单个字符


输出是这样的:



H


一世


..等等。我怎样才能让它只连接我的描述而不是我的网址?


精慕HU
浏览 117回答 1
1回答

千万里不及你

您应该只将结果的描述部分添加到列表中。试试这个:for col in result:     searchResult.append(col[0])我给你留下了有关列表对象方法信息的文档
随时随地看视频慕课网APP

相关分类

Python
我要回答