search

Home  >  Q&A  >  body text

python problem of retrieving URLs from mysql database

After fetching the link from the database, it prints out like this,

(u'https://www.baidu.com',)

The type of this field in the database is Varchar, this is the code

for row in results:
    print row

I printed it directly as https://www.baidu.com. What should I do?

曾经蜡笔没有小新曾经蜡笔没有小新2704 days ago1027

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-07-04 13:45:48

    You should be

    select url from xx_table limit 1;

    Then the database is taken out as a tuple, which is returned in the order of your select, so just print the first element directly

    results = (u'https://www.baidu.com',)
    print results[0] # python2
    # print(results[0]) # python3

    reply
    0
  • 为情所困

    为情所困2017-07-04 13:45:48

    The database field of VARCHAR type corresponds to the string in Python. Haven’t you already obtained it?
    If you want to use it as a value, just write:

    url = results[0]

    reply
    0
  • Cancelreply