Search code examples
pythonmysqlsqlpython-3.xpymysql

Pymysql library does not return pure data in database


I'm new to Python and especially pymysql library.

I want to make query from my database and when query is done , the result is not pure record that is in the database and have some parentheses and "," mark and so on.

This is my code.

I will be thankful if you answer me

from pymysql import *
def database_connector():
db = connect(host="localhost",port=3306,user="root",passwd="", 
db='telegrambot',charset='utf8')
return db
def question_return(tests_id,questions_id):
db=database_connector()
cursor=db.cursor()
cursor.execute("""SELECT question FROM questions WHERE test_id = '%s' AND 
question_id = '%s';""",(tests_id,questions_id))
return_value = cursor.fetchall()
return return_value

print (question_return(1,1))

and it print some thing like this.

(('What is your name ?',),)

and I want to just print something like this without parentheses and other marks

What is your name ?

Please help me how to do this

Thanks a lot


Solution

  • Since it returns tuple of tuples, You have to iterate over that. Find the below code.

    from pymysql import *
    def database_connector():
        db = connect(host="localhost",port=3306,user="root",passwd="", 
        db='telegrambot',charset='utf8')
        return db
    def question_return(tests_id,questions_id):
        db=database_connector()
        cursor=db.cursor()
        cursor.execute("""SELECT question FROM questions WHERE test_id = '%s' AND 
        question_id = '%s';""",(tests_id,questions_id))
        return_value = cursor.fetchall()
        return return_value
    
    for r in question_return(1,1):
        print r[0]