]>
git.datanom.net - lensdatabase.git/blob - db.py
3 except ImportError as err
:
9 def __new__(cls
, *args
, **kwargs
):
10 if (cls
.__instance
is None):
11 cls
.__instance
= super(DB
, cls
).__new
__(cls
)
14 def __init__(self
, path
):
15 file = path
+ "/lensdb.db"
16 initialize
= os
.path
.isfile(file)
17 self
.conn
= sqlite3
.connect(path
+ "/lensdb.db")
18 self
.conn
.execute("PRAGMA foreign_keys = '1'")
20 if initialize
is False:
24 cur
= self
.conn
.cursor()
25 cur
.execute('''drop table if exists lens;''')
26 cur
.execute('''drop table if exists lensmaker;''')
28 create table lensmaker(
29 id integer primary key not null,
34 id integer primary key not null,
37 description text not null,
39 constraint fk_maker foreign key (maker) references lensmaker (id)
44 def addMaker(self
, maker
):
45 cur
= self
.conn
.cursor()
46 cur
.execute("insert into lensmaker (maker) values (?)", (maker
, ))
49 def deleteMaker(self
, maker
):
50 cur
= self
.conn
.cursor()
51 cur
.execute("delete from lensmaker where maker = ?", (maker
, ))
54 def addLens(self
, maker
, tuple):
55 cur
= self
.conn
.cursor()
56 cur
.execute("select id from lensmaker where maker = ?", (maker
, ))
57 lensmaker
= cur
.fetchone()
59 raise Exception(maker
+ ": not found")
60 cur
.execute("insert into lens (maker, sn, description, notes) values (?, ?, ?, ?)", lensmaker
+ tuple)
62 cur
.execute("select last_insert_rowid();")
66 def updateLens(self
, tuple):
67 cur
= self
.conn
.cursor()
68 cur
.execute("update lens set sn = ?, description = ?, notes = ? where id = ?", tuple)
71 def deleteLens(self
, id):
72 cur
= self
.conn
.cursor()
73 cur
.execute("delete from lens where id = ?", (id, ))
77 cur
= self
.conn
.cursor()
78 cur
.execute("select maker from lensmaker")
79 rows
= cur
. fetchall()
82 def getLenses(self
, maker
):
83 cur
= self
.conn
.cursor()
84 if type(maker
) == str:
85 cur
.execute("select id from lensmaker where maker = ?", (maker
, ))
86 lensmaker
= cur
.fetchone()
89 cur
.execute("select sn, description, notes, id from lens where maker = ?", lensmaker
)
90 rows
= cur
. fetchall()
93 def getLensByPattern(self
, pattern
):
94 cur
= self
.conn
.cursor()
96 select m.maker, l.sn, l.description, l.notes, l.id from lensmaker m, lens l
97 where l.description like ? and
98 m.id = l.maker COLLATE NOCASE
100 cur
.execute(sql
, ('%'+pattern
+'%', ))
101 rows
= cur
. fetchall()
104 def getLensByMaker(self
, maker
):
105 cur
= self
.conn
.cursor()
107 select m.maker, l.sn, l.description, l.notes, l.id from lensmaker m, lens l
108 where l.maker = ? and
111 if type(maker
) == str:
112 cur
.execute("select id from lensmaker where maker = ? COLLATE NOCASE", (maker
, ))
113 lensmaker
= cur
.fetchone()
115 lensmaker
= (maker
, )
116 #cur.execute("select sn, description, notes, id from lens where maker = ?", lensmaker)
117 cur
.execute(sql
, lensmaker
)
118 rows
= cur
. fetchall()
121 def getLensByMakerPattern(self
, maker
, pattern
):
122 cur
= self
.conn
.cursor()
124 select m.maker, l.sn, l.description, l.notes, l.id from lensmaker m, lens l
125 where l.description like ? and l.maker = ? and
126 m.id = l.maker COLLATE NOCASE
128 if type(maker
) == str:
129 cur
.execute("select id from lensmaker where maker = ? COLLATE NOCASE", (maker
, ))
130 lensmaker
= cur
.fetchone()
132 lensmaker
= (maker
, )
133 #cur.execute("select sn, description, notes, id from lens where description like ? and maker = ? COLLATE NOCASE", ('%'+pattern+'%', ) + lensmaker)
134 cur
.execute(sql
, ('%'+pattern
+'%', ) + lensmaker
)
135 rows
= cur
. fetchall()
This page took 0.098884 seconds and 6 git commands to generate.