<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dbms code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 16:51:37 GMT</pubDate>
    <description>DZone Snippets: dbms 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>
    <item>
      <title>SQL for Symbian DBMS</title>
      <link>http://snippets.dzone.com/posts/show/1668</link>
      <description>I have summarized the allowable SQL below.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# 'SELECT' is the most popular SQL&lt;br /&gt;SELECT select-list FROM table-name [ WHERE search-condition ] [ ORDER BY sort-order ] &lt;br /&gt;# 3 search condition types: compare, like, null&lt;br /&gt;&lt;br /&gt;# DML : INSERT, DELETE, UPDATE &lt;br /&gt;INSERT INTO table-name [ ( column-identifier,&#8230; ) ] VALUES ( column-value,&#8230; ) &lt;br /&gt;DELETE FROM table-name [ WHERE search-condition ] &lt;br /&gt;UPDATE table-name SET update-column,&#8230; [ WHERE search-condition ] &lt;br /&gt;&lt;br /&gt;# DDL : Work with the schema&lt;br /&gt;CREATE TABLE table-name (column-definition,&#8230;) &lt;br /&gt;DROP TABLE table-name &lt;br /&gt;ALTER TABLE table-name { ADD add-column-set [ DROP drop-column-set ] | DROP drop-column-set } &lt;br /&gt;CREATE [ UNIQUE ] INDEX index-name ON table-name ( sort-specification,&#8230; ) &lt;br /&gt;DROP INDEX index-name FROM table-name &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Plus a few column types (not all of them)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;['BIT', 'INTEGER', 'COUNTER', 'BIGINT', 'FLOAT',&lt;br /&gt; 'TIMESTAMP', 'VARCHAR(n)', 'LONG VARCHAR']&lt;br /&gt;# COUNTER is an auto-incremented unsigned integer&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 09 Mar 2006 13:04:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1668</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Finding all symbian database files</title>
      <link>http://snippets.dzone.com/posts/show/1465</link>
      <description>Hmm.. I am considering hacking the Dbms format.&lt;br /&gt;I want to know what tables are there in each db file.&lt;br /&gt;The first step is to list all such files.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; def print_db(arg, dirname, names):&lt;br /&gt;...   for name in names:&lt;br /&gt;...     if name.endswith('.db'):&lt;br /&gt;...       print dirname + '\\' + name&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; os.path.walk('C:\\', print_db, None)&lt;br /&gt;C:\\SportsDiary.db&lt;br /&gt;C:\\test.db&lt;br /&gt;C:\system\Data\Bookmarks1.db&lt;br /&gt;C:\system\Data\lcscfg.db&lt;br /&gt;C:\system\Data\lcs.db&lt;br /&gt;C:\system\Data\NSmlDSSettings.db&lt;br /&gt;C:\system\Data\SavedDecks1.db&lt;br /&gt;C:\system\Data\midp2\systemams\MIDP2SystemAMSDynamic.db&lt;br /&gt;C:\system\Data\midp2\systemams\MIDP2SystemAMSStatic.db&lt;br /&gt;C:\system\Data\srsplugin\SDDatabase.db&lt;br /&gt;C:\system\Data\vasdb\vasdatabase.db&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 12 Feb 2006 20:16:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1465</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
