-1

I am trying to connect to mysql dataase using python but I couldn't find the correct connector and module called MySQLdb how can I find the correct sources? when I import the module it shows an error like this because I have not added the correct module

>>> import MySQLdb
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import MySQLdb
ImportError: No module named 'MySQLdb'
ImportError: No module named 'sqlobject'
>>> 
Dimuth Ruwantha
  • 671
  • 2
  • 12
  • 26

1 Answers1

1

Download the module from https://github.com/PyMySQL/PyMySQL/ copy the extracted file to the lib folder in your python and then code the following

import pymysql

hostname = 'localhost'
username = 'username'
password = 'password'
database = 'db_name'

# Simple routine to run a query on a database and print the results:
def doQuery( conn ) :
  cur = conn.cursor()

  cur.execute( "SELECT col1, col2 FROM table_name" )

  for column1, column2 in cur.fetchall() :
    print(column1, column2)

print ("Using pymysql…")
myConnection = pymysql.connect( host=hostname, user=username,passwd=password, db=database )
doQuery( myConnection )
myConnection.close()
Dimuth Ruwantha
  • 671
  • 2
  • 12
  • 26