Permalink
Browse files

When db file exist,not try to create new tables.

  • Loading branch information...
1 parent 6326b4b commit 2ff16e868ffd4d92e77d1c83583ff891908017be @fy0 committed Apr 7, 2012
Showing with 9 additions and 6 deletions.
  1. +7 −5 easydb/__init__.py
  2. +1 −0 readme.md
  3. +1 −1 setup.py
View
@@ -6,12 +6,13 @@
class EasyDB:
#conn = sqlite3.Connection
def __init__(self, filename, schema = None, **kwargs):
- if not (os.path.exists(filename) or schema):
- raise Exception, "The specified database file does not exist, and you haven't provided a schema"
+ exist = os.path.exists(filename)
+ if not (exist or schema):
+ raise Exception("The specified database file does not exist, and you haven't provided a schema")
self.conn = sqlite3.connect(filename)
- if schema:
+ if not exist:
for table_name, fields in schema.items():
- query = u"CREATE TABLE %s (%s)" % (table_name, ", ".join(fields))
+ query = "CREATE TABLE %s (%s)" % (table_name, ", ".join(fields))
self.query(query)
self.conn.commit()
@@ -23,7 +24,7 @@ def query(self, *args, **kwargs):
cursor = self.conn.cursor()
result = cursor.execute(*args, **kwargs)
ret = result.fetchall()
- if ret:
+ if ret and len(ret)==1:
return ret[0]
return ret
@@ -44,3 +45,4 @@ def __getitem__(self,key):
def __setitem__(self,key,value):
return self.query('replace into data values("%s","%s")'%(key,value))
+
View
@@ -72,3 +72,4 @@ print db['test']
# => ('123', '456')
```
+
View
@@ -6,7 +6,7 @@ def read(fname):
setup(
name='easydb-fy',
- version='0.1.1',
+ version='0.1.2',
description='Simple SQLite wrapper, and a small key-value db implement',
author='fy',
author_email='fy0748@gmail.com',

0 comments on commit 2ff16e8

Please sign in to comment.