I\'m using sqlite3 in python 2.5. I\'ve created a table that looks like this:
create table votes (
bill text,
senator_id text,
vote text)
<
I know you're not asking this, but why not just use sqlalchemy to build an orm for the database? then you can do things like,
entry = model.Session.query(model.Votes).first()
print entry.bill, entry.senator_id, entry.vote
as an added bonus your code will be easily portable to an alternative database, and connections and whatnot will be managed for free.
There is actually an option for this in sqlite3. Change the row_factory
member of the connection object to sqlite3.Row
:
conn = sqlite3.connect('db', row_factory=sqlite3.Row)
or
conn.row_factory = sqlite3.Row
This will allow you to access row elements by name--dictionary-style--or by index. This is much more efficient than creating your own work-around.
Sure, make yourself a DictConnection and DictCursor as explained and shown at http://trac.edgewall.org/pysqlite.org-mirror/wiki/PysqliteFactories for example.
I use something like this:
class SqliteRow(object):
def __init__(self):
self.fields = []
def add_field(self, name, value):
self.fields.append(name)
setattr(self, name, value)
def to_tuple(self):
return tuple([getattr(self, x) for x in self.fields])
with this:
def myobject_factory(cursor, row):
myobject= MyObject()
for idx, col in enumerate(cursor.description):
name, value = (col[0], row[idx])
myobject.add_field(name, value)
return myobject
MyObject()
is a class that inherits from SqliteRow
.
SqliteRow class is a base class for every object that I want to have returned by a query.
Every column becomes an attribute and is logged into the fields
list.
Function to_tuple
is used to change the whole object to a form suitable for queries (simply pass the whole object and forget).
To get different class types of that function. You would need to make a factory object, that will generate objects based on the list of fields (for example: dict with { some_unique_value_made_of_fields: class} )
This way I get a simple ORM.
I've used this:
def get_dict(sql):
return dict(c.execute(sql,()).fetchall())
Then you can do this:
c = conn.cursor()
d = get_dict("select user,city from vals where user like 'a%'");
Now d
is a dictionary where the keys are user
and the values are city
. This also works for group by
Simple solution, initialize a cursor object:
cursor = conn.cursor(buffered = True, dictionary = True)
Another option:
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
Rest of Code:
query = "SELECT * FROM table"
cursor.execute(query)
row = cursor.fetchone()
Sources: mysql.connector.cursor , MySQLdb.cursors.DictCursor