edmozley
Posts: 15
Joined: Tue Sep 04, 2018 10:27 am

Connecting to mySQL in python

Thu Sep 20, 2018 11:02 pm

I have installed mySQL on my Pi and would now like to run a python script to retrieve info from the database.

I have installed myPHPAdmin and have created a table with some data.

My python script is as follows:

Code: Select all

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="myusername",
  passwd="mypassword",
  database="mydb"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM settings")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)
However I am getting an error:

Traceback (most recent call last):
File "dbtest.py", line 1, in <module>
import mysql.connector
File "/home/pi/Documents/mysql.py", line 1, in <module>
import mysql.connector
ImportError: No module named connector

edmozley
Posts: 15
Joined: Tue Sep 04, 2018 10:27 am

Re: Connecting to mySQL in python

Thu Sep 20, 2018 11:05 pm

Apologies I realised I made a stupid mistake and had a file called mysql.py in my documents folder.

Return to “Python”

Who is online

Users browsing this forum: paddyg and 14 guests