Fetching values from mysql using MySQLdb and Python#!/usr/bin/env python import MySQLdb
sSQL = """ select * from emp """ # You have table emp having two fields you can use following SQL
# CREATE TABLE IF NOT EXISTS `emp` ( # `id` bigint(20) NOT NULL AUTO_INCREMENT, # `name` varchar(100) NOT NULL, # PRIMARY KEY (`id`) # ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
# Dumping data for table `emp`
#INSERT INTO `emp` (`id`, `name`) VALUES #(1, 'emp1'), #(2, 'emp2');
conn = MySQLdb.connect (host = "localhost",user = "YOUR USERNAME",passwd = "YOUR PASSWORD",db = "YOUR DATABASE") cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute (sSQL) result_set = cursor.fetchall ()
for row in result_set: print row["name"] cursor.close () conn.close () |
Categories
|