Question:

How to retrieve data from a table in MySQL database through Python code? Explain.

Answer:

1. import MySQLdb module as : import MySQLdb
2. establish a connection to the database.
db = MySQLdb.connect(“host”=”local host”, “database-user”=”user-name”, “password”=”password”, “database-name”=”database”)
3. initialize the cursor variable upon the established connection: c1 = db.cursor()
4. retrieve the information by defining a required query string. s = “Select * from dept”
5. fetch the data using fetch() methods and print it. data = c1.fetch(s)
6. close the database connection. db.close()


Keywords:

© 2017 QuizBucket.org