Connecting to mysql using python
In [1]: import MySQLdb
#MySQLdb.connect("localhost","username","password","dbname")
In [2]: db = MySQLdb.connect("localhost","root","hPMkXT1vU7rh","erpdatabase")
In [3]: cursor = db.cursor()
In [4]: cursor.execute("SELECT VERSION()")
Out[4]: 1L
In [5]: data = cursor.fetchone()
In [6]:
In [6]: print data
('10.0.25-MariaDB-1~trusty',)
In [7]: cursor.execute("SELECT name from tabItem")
Out[7]: 8L
In [8]: data = cursor.fetchall()
In [9]: print data
(('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',))
In [10]: print list(data)
[('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',)]
//print in list
In [14]: a=data
In [15]: print a
(('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',))
In [16]: [element for tupl in a for element in tupl]
Out[16]:
['chemical',
'Furan',
'i',
'T Meter',
'Water Content Test IS 1001',
'Water Content Test IS 9001',
'Water Content Test IS 91',
'Water Content Test IS91']
#print in list
In [23]: abc = [e[0] for e in a]
In [24]: abc
Out[24]:
['chemical',
'Furan',
'i',
'T Meter',
'Water Content Test IS 1001',
'Water Content Test IS 9001',
'Water Content Test IS 91',
'Water Content Test IS91']
In [1]: import MySQLdb
#MySQLdb.connect("localhost","username","password","dbname")
In [2]: db = MySQLdb.connect("localhost","root","hPMkXT1vU7rh","erpdatabase")
In [3]: cursor = db.cursor()
In [4]: cursor.execute("SELECT VERSION()")
Out[4]: 1L
In [5]: data = cursor.fetchone()
In [6]:
In [6]: print data
('10.0.25-MariaDB-1~trusty',)
In [7]: cursor.execute("SELECT name from tabItem")
Out[7]: 8L
In [8]: data = cursor.fetchall()
In [9]: print data
(('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',))
In [10]: print list(data)
[('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',)]
//print in list
In [14]: a=data
In [15]: print a
(('chemical',), ('Furan',), ('i',), ('T Meter',), ('Water Content Test IS 1001',), ('Water Content Test IS 9001',), ('Water Content Test IS 91',), ('Water Content Test IS91',))
In [16]: [element for tupl in a for element in tupl]
Out[16]:
['chemical',
'Furan',
'i',
'T Meter',
'Water Content Test IS 1001',
'Water Content Test IS 9001',
'Water Content Test IS 91',
'Water Content Test IS91']
#print in list
In [23]: abc = [e[0] for e in a]
In [24]: abc
Out[24]:
['chemical',
'Furan',
'i',
'T Meter',
'Water Content Test IS 1001',
'Water Content Test IS 9001',
'Water Content Test IS 91',
'Water Content Test IS91']
No comments:
Post a Comment