Hi Group,
i have a question about working with mysql.
Has Python an Object like the ResultSet in Java? That allows me, after executing a statement, to get the Data by calling the Col-Name:
String str = resSet.getString("col_name");
Is there some similar in Python ?
If yes, please give my a code-exsample
thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Group,
i have a question about working with mysql.
Has Python an Object like the ResultSet in Java? That allows me, after executing a statement, to get the Data by calling the Col-Name:
String str = resSet.getString("col_name");
Is there some similar in Python ?
If yes, please give my a code-exsample
thanks
I use a small class that wraps a row so you can access a column thruogh x.name.
c.execute("select ...")
r = DBRow(c)
print r.foo, r.bar
----cut-here----
class DBRow:
def __init__(self, c, row=None):
if row: self.__row = row
else: self.__row = c.fetchone()
self.__columns = map(lambda x: x[0], c.description)
self.__colmap = {}
for i in range(len(c.description)):
self.__colmap[c.description[i][0]] = i
def __len__(self):
if self.__row: return len(self.__row)
else: return 0
def __str__(self):
return str(self.__row)
def __getitem__(self, key):
return self.__row[key]
def __getattr__(self, name):
return self.__row[self.__colmap[name]]
def columns(self):
return self.__columns
----cut-here----