+ 1

How connect python with database???

python & database

30th Jun 2016, 4:20 AM
Amarjit Dhillon
Amarjit Dhillon - avatar
2 Answers
+ 4
To connect python with a database you need it's connector. For example let's take MySQL, if you go to their site you will find various connector for Python, Java and much more (someone correct me if I'm wrong). First of all if you want MySQL, you must install it, there are various tutorials out there on Google so simply search for it. After you must import it in your script, to do so just simply write: import MySQLdb #this will import the module #needed to connect python to your db database = MySQLdb.connect( host="localhost", #this is your host user="yourUsername", #the username of your #MySQL account passwd="yourPassword", #your password db="yourDbName" #name of your database) my_cursor = database.cursor()#cursor obj needed #to execute your queries my_cursor.execute("YOUR QUERY GOES HERE") #execute your queries database.close()#ALWAYS close your db I didn't had much space to explain everything but I hope it is clear for you and everybody else.
30th Jun 2016, 8:39 AM
Itami - avatar
30th Jun 2016, 3:35 PM
Amarjit Dhillon
Amarjit Dhillon - avatar