at_yasu's blog

ロード的なことを

間違えていたので訂正。

上のスクリプト、間違ってる箇所があったので訂正。


select文を作る際、fieldまでテイントチェックしているので、返って来る値が全部固定になっていました。

# -*- coding: utf-8 -*-
#
# $Id: mydb.py 201 2008-03-07 13:32:08Z yasui $
#
# ex, http://www.python.org/dev/peps/pep-0249/
#     http://docs.python.org/lib/module-sqlite3.html

import sqlite3

class mydb:
	_conf = {'dbPath':'db'}
	_conn = None
	_debug = False
	
	def __init__ (self,hashref) :
		if 'dbPath' in hashref:
			self._conf['dbPath'] = hashref['dbPath']
		if 'debug' in hashref:
			self._debug = hashref['debug']
		
		self._conn = sqlite3.connect(self._conf['dbPath'])

	def getColumn (self, table, field, opt='') :
		sql = "select " + ",".join(field) + " from " + table+" "+opt
		c = self._conn.cursor()
		
		if self._debug :
			print sql,",",",".join(field)
		
		c.execute(sql)
		buff = c.fetchall()
		c.close()
		return buff
	
	# field =>dict, key=>column data, val => column name
	def append (self, table, field) :
		colname = [field[v] for v in field]
		coldata = [v for v in field]
		sql = "insert into " + table + "(`"+"`,`".join(colname)+"`) values " + \
			"(" +",".join(coldata) + ")"
		
		if self._debug :
			print sql
		
		c = self._conn.cursor()
		c.execute(sql,field)
		c.close()
	
	def delete (self, table, opt) :
		sql = "delete from ? where " + opt
		
		if self._debug :
			print sql
		
		c = self._conn.cursor()
		c.execute(sql,table)
		c.close
	
	# fields => dict, key=>field name, val=>field type
	def createTable (self, table, fields) :
		arr = [k+" "+fields[k] for k in fields]
		c = self._conn.cursor()
		sql = "create table "+table+" (" + ",".join(arr) + ")"
		
		if self._debug :
			print sql
		
		try :
			c.execute(sql)
		except StandardError,e :
			self._conn.rollback()
			c.close()
			return False
		
		self._conn.commit()
		c.close()
		return True
	
	def commit (self) :
		self._conn.commit()
	
	def connection (self):
		return self._conn



def test(dbname):
	tableName = 'test_table'
	db = mydb({'dbPath':dbname, "debug":True})
	
	# create table
	b = db.createTable(tableName,{'id':'INTEGER PRIMARY KEY ','test':'TEXT'})
	if b == False :
		print "Exception Error: db.craeteTable"
	
	# Insert the data
	testdata = {"'test'":'test',"'test1'":'test',"'test2'":'test',"'test3'":'test',
				"'test4'":'test',"'test5'":'test',"'test6'":'test'}
	for i in testdata :
		db.append(tableName,{i:testdata[i]})
	db.commit()
	
	col = lambda d,t,col,q : d.getColumn(t,col,q)
	def fors(a,k):
		print len(a),a
#		for v in a: print v
	
	arr = col(db,tableName,['test','id'],''); fors(arr,'id')
	
	arr = col(db,tableName,['test']," where test like '%1'"); fors(arr,'test')
	
	arr = col(db,tableName,['test']," where test like '%st%'"); fors(arr,'test')
	
	arr = col(db,tableName,['test']," where test like 'test.%'"); fors(arr,'test')
	
	arr =  col(db,tableName,['test']," where test like 'aaa'"); fors(arr,'test')
	


if __name__ == '__main__':
	import os
	try:
		test('testdb')
	except StandardError,e:
		print e
	try:
		os.remove('testdb')
	except : pass