ShwSvn
ShwSvn

Reputation: 79

python, flask and mysql

I would like help in just one thing

@app.route('/getdata', methods =['GET', 'POST'])
#@requires_auth
def get_data():

    config = {
        'user': 'user',
        'password': 'password.',
        'host': '127.0.0.1',
        'database': 'database',
        'raise_on_warnings': True,
        'use_pure': False,
    }

    cnx = mysql.connector.connect(**config)

    cur = cnx.cursor()

    cur.execute("SELECT hash FROM hash_files")


     rows = cur.fetchall()
     response = [row[0] for row in rows]

    return jsonify(response)


    cnx.close()

I get this error "tuple indices must be integers, not str" can someone help me please?

I got that error before, but now I have this one

      Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python27\webapi\venv\webapi.py", line 59, in get_data
    cnx = mysql.connector.connect(**config)
  File "C:\Python27\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 98, in __init__
    self.close()
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 252, in close
    self.cmd_quit()
  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 581, in cmd_quit
    self._socket.send(packet, 0, 0)
  File "C:\Python27\lib\site-packages\mysql\connector\network.py", line 143, in send_plain
    errno=2055, values=(self.get_address(), _strioerror(err)))
  File "C:\Python27\lib\site-packages\mysql\connector\errors.py", line 185, in __init__
    self.msg = self.msg % values
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 46: ordinal not in range(128)

Sorry for the long error, I didin't knew if you need it all so I send like this

I had de program working with sqlite3 but I needed to use mysql so to get all the data on the web api and the other program getting the data to an array and it's not working now

Upvotes: 0

Views: 130

Answers (1)

anupsabraham
anupsabraham

Reputation: 3039

.fetchall() function returns a list of tuple. So row is a tuple and you can't do row['hash']. That is why you're getting the error tuple indices must be integers, not str.

To access the values in the tuple, you need to use index: row[0]

Edit:

For fixing the decode error, you need to read this. Since it is a long answer to post here and someone else has answered it very well, I'm not typing it all here.

Upvotes: 1

Related Questions