0

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

ShwSvn
  • 79
  • 2
  • 8
  • Please edit your post to include the error printout – 0TTT0 Oct 18 '17 at 20:44
  • @0TTT0 I add it now, thanks – ShwSvn Oct 18 '17 at 20:54
  • @ShwSvn you're trying to call the api using a different script and you're posting the trace back of that script. Please post the trace back from your original program. – anupsabraham Oct 18 '17 at 21:31
  • The traceback you've posted says that you're trying to json decode the response you're getting from the API. When your API isn't returning you a json object at all. – anupsabraham Oct 18 '17 at 21:32
  • @anupsabraham you are rigth, I'm sorry, I updated it – ShwSvn Oct 18 '17 at 21:32
  • 1
    The error seems to happen during the `mysql.connector.connect()` call. Do you perhaps have any unicode characters in the hostname? This seems to be an encoding error that happens while processing a failed connection attempt. – mata Oct 18 '17 at 21:59
  • @mata I don't think that I have – ShwSvn Oct 18 '17 at 22:07

1 Answers1

1

.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.

anupsabraham
  • 2,781
  • 2
  • 24
  • 35
  • Like that? I changed the post But know I'm getting the other error that I posted UnicodeDecodeError UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 46: ordinal not in range(128) – ShwSvn Oct 18 '17 at 21:29
  • @ShwSvn Please read this to have a better understanding of your current problem. https://stackoverflow.com/a/35444608/1666143 – anupsabraham Oct 18 '17 at 21:49
  • 1
    No, the stack trace shows that the error doesn't happen when getting the results but during `connect()` – mata Oct 18 '17 at 21:55
  • I don't know, I restarted the pycharm and it was fixed with this change of row[0] Thanks a lot to everyone – ShwSvn Oct 18 '17 at 22:09