Connecting to MySQL via Python

Electronics being social! What could possibly go wrong?
Post Reply
Paul Vermeeren
n00b
Posts: 8
Joined: Sun Sep 06, 2015 3:38 pm

Connecting to MySQL via Python

Post by Paul Vermeeren » Tue Sep 15, 2015 7:08 pm

Hi,

I am trying for a couple of days now but I can't figure out how to connect via python to my MySQL database.

My plan is to connect from a website to the raspberry pi and control the wheels.
I am trying to control the speed of the wheels via a MySQL database.

The last thing I tried is:
(I have more things imported but this is the only thing for MySQL.

Code: Select all

import MySQLdb

conn = MySQLdb.connect(database="XX",host="XX",user="XX",password="XX")

cur = conn.cursor()

cur.execute("SELECT * FROM XX")
for row in cur.fetchall() :
     PBR.SetMotor1(row[0])
     PBR.SetMotor2(row[1])

User avatar
Jake
ModMyPi
Posts: 1443
Joined: Thu Aug 27, 2015 3:57 pm

Re: Connecting to MySQL via Python

Post by Jake » Thu Sep 17, 2015 2:12 pm

Hi Paul,

Have you installed the MySQLdb library?

sudo apt-get install python-mysqldb

Good resource on connecting MySQL via Python

http://stackoverflow.com/questions/3728 ... -in-python

-- Jake
Forum Administrator & Technical Support

Aikidokajeff
n00b
Posts: 8
Joined: Thu Aug 27, 2015 2:32 pm

Re: Connecting to MySQL via Python

Post by Aikidokajeff » Thu Sep 17, 2015 3:47 pm

Hi Paul,

Are you able to connect to MySQL via the command line and execute your queries successfully?

Jeff

Paul Vermeeren
n00b
Posts: 8
Joined: Sun Sep 06, 2015 3:38 pm

Re: Connecting to MySQL via Python

Post by Paul Vermeeren » Fri Sep 18, 2015 2:42 pm

Hi,

I'm not sure if I tried via the command line but I think that it also did not work.

I had already tried that post.
But the error that most occurred was about the logging in part.

So I gave up and got it working via a text file on my website and outputted the value with PHP and then read it from the website with python.
That way is a little more complex but I got it working eventually.


Paul

rahuldev
n00b
Posts: 1
Joined: Sat Aug 04, 2018 2:01 pm

Re: Connecting to MySQL via Python

Post by rahuldev » Sat Aug 04, 2018 2:06 pm

Best way to connect to MySQL from python is to Use MySQL Connector/Python because it is official Oracle driver for MySQL for working with Python and it works with both Python 3 and Python 2.

follow the steps mentioned below to connect MySQL

install connector using pip

Code: Select all

pip install mysql-connector-python
or you can download the installer Refer this article to download and install MySQL Connector Python https://pynative.com/install-mysql-connector-python/

Use connect() method of mysql connector python to connect to MySQL. Refer to this article to connect MySQL from Python https://pynative.com/python-mysql-database-connection/


Example:

Code: Select all

import mysql.connector
 from mysql.connector import Error
 try:
     conn = mysql.connector.connect(host='hostname',
                         database='db',
                         user='root',
                         password='passcode')
     if conn.is_connected():
       cursor = conn.cursor()
       cursor.execute("Your Query")
       record = cursor.fetchall()
       
 except Error as e :
    print ("Print your error msg", e)
 finally:
    #closing database connection.
    if(conn.is_connected()):
       cursor.close()
       conn.close()

Post Reply