2

I have a problem with mySQL

I installed it on my raspberry pi - python 3 virtual environment with the following command:

sudo pip install mysql-connector

I have the following script:

import mysql.connector

mydb = mysql.connector.connect(
    host="127.0.0.1",
    port="11300",
    user="pi",
    passwd="1234"
 )

print(mydb)

But nothing happens, no error, no connection, ... I found the port number with the command 'netstat' under TCP connection

5
  • That means its working so go ahead and do something do some query etc.. Commented Aug 17, 2018 at 7:00
  • I was expecting something like <mysql.connector.connection.MySQLConnection object ar 0x16645F0> but ok, I did another test with mycursor = mydb.cursor() and mycursor.execute("CREATE DATABASE myDatabase") and after this, search for the database: mycursor.execute("SHOW DATABASE") but still nothing happens... Commented Aug 17, 2018 at 8:27
  • you have to fetch to see response of your query Commented Aug 17, 2018 at 8:39
  • stackoverflow.com/a/20959654/6813858 would be helpful Commented Aug 17, 2018 at 8:40
  • I read those comments, and tried some things out, but still nothing happens... Commented Aug 17, 2018 at 9:12

1 Answer 1

1

after execute you need to open cursor, to get result you can open cursors like:

mycursor.fetchall() returns iterable object.

for row in mycursor.fetchall():
    print(row)

mycursor.fetchone() fetches first row from result

print(mycursor.fetchone())

P.S. are you sure that you've installed that package properly?

e.g.: for ubuntu you can install this package for python3 by typing:

apt install python3-mysql.connector

Sign up to request clarification or add additional context in comments.

15 Comments

The installation was successful, When I type mysql --help in the prompt, and look at port I have a value 0 is this normal? and now with netstat I don't see the TCP/IP anymore ??
port value 0 is normal. you've executed mycursor.execute("SHOW DATABASE") can you please try: mycursor.execute("SHOW DATABASES") and get rows by opening cursor
this is what I have import mysql.connector try: mydb = mysql.connector.connect(user='pi',password='1234',host='127.0.0.1',port='11300',database='test') mycursor = mydb.cursor() mycursor.execute("SHOW DATABASES") mycursor.fetchall() except: print("error") But still I got nothing, It's like the prompt is waiting for something, but I can do anything..
after executing try running this: for row in mycursor.fetchall(): print(row)
@WerbrouckBram I've edited main answer, check examples there
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.