Werbrouck Bram
Werbrouck Bram

Reputation: 21

Connect to mySQL with python 3

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

Upvotes: 2

Views: 6061

Answers (1)

Lasha Kitia
Lasha Kitia

Reputation: 141

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

Upvotes: 1

Related Questions