Belle II Software
release-05-02-19
Main Page
Modules
Namespaces
Namespace List
Namespace Members
All
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Functions
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
r
s
t
u
v
w
x
z
Variables
a
b
c
d
e
f
g
h
i
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Typedefs
a
b
c
d
e
h
i
l
m
n
p
r
s
t
v
w
Enumerations
Enumerator
c
d
f
p
t
u
v
w
Classes
Class List
Class Hierarchy
Class Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
Enumerations
a
b
c
d
e
f
g
h
i
l
m
o
p
r
s
t
u
v
Enumerator
a
b
c
d
e
f
g
h
k
l
m
n
o
p
r
s
t
u
v
w
z
Related Functions
b
c
d
g
i
o
r
s
t
Files
File List
File Members
All
Functions
using_databases.py
1
from
caf.database
import
SQLiteDB
2
import
pathlib
3
4
# Moving over to pathlib objects has nice benefits like being able to avoid a lot of os.path.join/exists calls
5
database_file = pathlib.Path(
"example.db"
)
6
7
if
database_file.exists():
8
print(
"Removing old database file"
)
9
# Slightly odd api for removing a file. Use rmdir() for directories
10
database_file.unlink()
11
12
# The SQLiteDB is made as a context manager so it will open a connection and close it for you
13
# If the database doesn't exist you ned to pass in a 'schema' that describes the tables to create
14
# If the database already exists, no table creation is done so you don't need to pass in a schema
15
with
SQLiteDB(database_file, schema={
"users"
: [
"name text primary key"
,
"age int"
,
"gender text"
,
"email text"
]})
as
db:
16
# Database file and tables are created and committed automatically when opening
17
# Can now insert some rows
18
db.query(
"INSERT INTO users VALUES (?,?,?,?)"
, (
"Tom"
, 25,
"M"
,
"tom@fakedomain.edu.au"
))
19
db.query(
"INSERT INTO users VALUES (?,?,?,?)"
, (
"Charlotte"
, 37,
"F"
,
"charlotte@fakedomain.edu.au"
))
20
db.query(
"INSERT INTO users VALUES (?,?,?,?)"
, (
"Taya"
, 36,
"F"
,
"taya@fakedomain.edu.au"
))
21
db.query(
"INSERT INTO users VALUES (?,?,?,?)"
, (
"Simon"
, 36,
"O"
,
"simon@fakedomain.edu.au"
))
22
# Need to commit the changes using the connection object
23
db.conn.commit()
24
# Can now query for the values. It returns a cursor object which can be used to get the values
25
cursor1 = db.query(
"SELECT name, email FROM users WHERE gender=?"
, (
"F"
))
26
cursor2 = db.query(
"SELECT name, email FROM users WHERE gender=?"
, (
"M"
))
27
28
print(
"Returned rows:"
)
29
30
# You could iterate over the cursor to return each row
31
def
iterate_cursor():
32
for
row
in
cursor1:
33
print(
" "
, row)
34
35
# Or you could just get the first entry remaining in the statement (can be called multiple times)
36
def
fetch_singles():
37
print(
" "
, cursor1.fetchone())
38
print(
" "
, cursor1.fetchone())
39
print(
" "
, cursor1.fetchone())
# Returns None as there are no matching rows left
40
41
# Or you could get all rows at once, this is not advised when you expect thousands of rows. But for a smaller number its fine
42
def
fetch_all():
43
for
row
in
cursor1.fetchall():
44
print(
" "
, row)
45
46
iterate_cursor()
47
48
# We are deliberately not using cursor2 at all. It should therefore be closed because otherwise it is an unfinished statement
49
# cursor1 doesn't need to be closed because its results were fetched, even if only one of them
50
cursor2.close()
51
52
print(
"Database connection is now closed. It will have automatically committed any remaining changes."
)
53
print(
"Removing database file."
)
54
database_file.unlink()
calibration
examples
databases
using_databases.py
Generated on Tue Jan 4 2022 02:52:38 for Belle II Software by
1.8.17