<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: sql code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 06:22:27 GMT</pubDate>
    <description>DZone Snippets: sql code</description>
    <item>
      <title>dbms database class</title>
      <link>http://snippets.dzone.com/posts/show/4242</link>
      <description>I'm at my first attempt at creating something for the PyS60 and I needed a simple sql wrapper class...&lt;br /&gt;I'm an experienced programmer, but has never programmed in python for s60 and it's been a while since I've used Python... So I hope this will help somebody, not annoy ;-)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Filename: db.py save in your Python dir&lt;br /&gt;import e32db&lt;br /&gt;&lt;br /&gt;class db:&lt;br /&gt;    def __init__(self, dbpath):&lt;br /&gt;	self.db = e32db.Dbms()&lt;br /&gt;	self.dbv = e32db.Db_view()&lt;br /&gt;	self.reset_counters()&lt;br /&gt;	try:&lt;br /&gt;	    self.db.open(unicode(dbpath))&lt;br /&gt;	except:&lt;br /&gt;	    self.db.create(unicode(dbpath))&lt;br /&gt;	    self.db.open(unicode(dbpath))&lt;br /&gt;&lt;br /&gt;    def reset_counters(self):&lt;br /&gt;	self.affected_rows = 0&lt;br /&gt;	self.num_rows = 0&lt;br /&gt;	self.__internal_counter = 0&lt;br /&gt;&lt;br /&gt;    def query(self, sql):&lt;br /&gt;	self.reset_counters()&lt;br /&gt;	if sql.lower().startswith('select'):&lt;br /&gt;	    self.dbv.prepare(self.db, unicode(sql))&lt;br /&gt;	    self.dbv.first_line()&lt;br /&gt;	    self.num_rows = self.dbv.count_line()&lt;br /&gt;	else:&lt;br /&gt;	    self.affected_rows = self.db.execute(unicode(sql))&lt;br /&gt;&lt;br /&gt;    def next(self):&lt;br /&gt;	row = {'id': 0}&lt;br /&gt;	if self.num_rows &lt; 1:&lt;br /&gt;	    self.reset_counters()&lt;br /&gt;	    raise StopIteration&lt;br /&gt;	elif self.__internal_counter &lt; self.num_rows:&lt;br /&gt;	    self.dbv.get_line()&lt;br /&gt;	    for i in range(self.dbv.col_count()):&lt;br /&gt;		row[i] = self.dbv.col(i+1)&lt;br /&gt;	    self.dbv.next_line()&lt;br /&gt;	    self.__internal_counter += 1&lt;br /&gt;	    return row&lt;br /&gt;	else:&lt;br /&gt;	    self.reset_counters()&lt;br /&gt;	    raise StopIteration&lt;br /&gt;&lt;br /&gt;    def __iter__(self):&lt;br /&gt;	return self&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now to create and fill db, create a file in the same dir as the db.py with this content:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Change __exec_path to the path of your python script dir&lt;br /&gt;__exec_path = "E:\\Python\\"&lt;br /&gt;dbname = "test"&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;sys.path.append(__exec_path)&lt;br /&gt;from db import db&lt;br /&gt;&lt;br /&gt;# This will open E:\\Python\test.db - it will be created first, if not existing&lt;br /&gt;mydb = db(__exec_path+dbname+'.db')&lt;br /&gt;mydb.query("create table testing (id counter, name varchar)")&lt;br /&gt;mydb.query("insert into testing (name) values ('test 1')")&lt;br /&gt;mydb.query("insert into testing (name) values ('test 2')")&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now go ahead and have fun:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Again change __exec_path to the path of your python script dir&lt;br /&gt;__exec_path = "E:\\Python\\"&lt;br /&gt;dbname = "test"&lt;br /&gt;&lt;br /&gt;import sys&lt;br /&gt;sys.path.append(__exec_path)&lt;br /&gt;from db import db&lt;br /&gt;&lt;br /&gt;# Opens E:\\Python\test.db&lt;br /&gt;mydb = db(__exec_path+dbname+'.db')&lt;br /&gt;mydb.query("select * from testing")&lt;br /&gt;for row in mydb:&lt;br /&gt;    print "-&gt; ",row[0]," ",row[1]," &lt;-"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Have fun!&lt;br /&gt;Dan</description>
      <pubDate>Tue, 03 Jul 2007 21:46:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4242</guid>
      <author>nerdcoder (Dan Larsen)</author>
    </item>
  </channel>
</rss>
