Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
de375094
"src/kernel/vscode:/vscode.git/clone" did not exist on "5f44a4efce7a064aeba9b50e759a0f27cb2ccf75"
Commit
de375094
authored
Jan 29, 2015
by
Davis King
Browse files
added a SQLite example program
parent
11983d72
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
9 deletions
+81
-9
examples/sqlite_ex.cpp
examples/sqlite_ex.cpp
+81
-9
No files found.
examples/sqlite_ex.cpp
View file @
de375094
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
/*
This example gives a quick overview of dlib's C++ API for the popular SQLite library.
*/
*/
...
@@ -12,28 +13,61 @@ using namespace dlib;
...
@@ -12,28 +13,61 @@ using namespace dlib;
using
namespace
std
;
using
namespace
std
;
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
bool
table_exists
(
bool
table_exists
(
database
&
db
,
database
&
db
,
const
std
::
string
&
tablename
const
std
::
string
&
tablename
)
)
{
{
// Sometimes you want to just run a query that returns one thing. In this case, we
// want to see how many tables are in our database with the given tablename. The only
// possible outcomes are 1 or 0 and we can do this by looking in the special
// sqlite_master table that records such database metadata. For these kinds of "one
// result" queries we can use the query_int() method which executes a SQL statement
// against a database and returns the result as an int.
return
query_int
(
db
,
"select count(*) from sqlite_master where name = '"
+
tablename
+
"'"
)
==
1
;
return
query_int
(
db
,
"select count(*) from sqlite_master where name = '"
+
tablename
+
"'"
)
==
1
;
}
}
// ----------------------------------------------------------------------------------------
int
main
()
try
int
main
()
try
{
{
// Open the SQLite database in the stuff.db file (or create an empty database in
// stuff.db if it doesn't exist).
database
db
(
"stuff.db"
);
database
db
(
"stuff.db"
);
if
(
!
table_exists
(
db
,
"davis"
))
// Create a people table that records a person's name, age, and their "data".
db
.
exec
(
"create table davis (name, age, data)"
);
if
(
!
table_exists
(
db
,
"people"
))
db
.
exec
(
"create table people (name, age, data)"
);
statement
st
(
db
,
"insert into davis VALUES(?,?,?)"
);
// Now let's add some data to this table. We can do this by making a statement object
// as shown. Here we use the special ? character to indicate bindable arguments and
// below we will use st.bind() statements to populate those fields with values.
statement
st
(
db
,
"insert into people VALUES(?,?,?)"
);
string
name
=
"davis"
;
// The data for Davis
string
name
=
"Davis"
;
int
age
=
32
;
int
age
=
32
;
matrix
<
double
>
m
=
randm
(
3
,
3
);
matrix
<
double
>
m
=
randm
(
3
,
3
);
// some random "data" for Davis
// You can bind any of the built in scalar types (e.g. int, float) or std::string and
// they will go into the table as the appropriate SQL types (e.g. INT, TEXT). If you
// try to bind any other object it will be saved as a binary blob if the type has an
// appropriate void serialize(const T&, std::ostream&) function defined for it. The
// matrix has such a serialize function (as do most dlib types) so the bind below saves
// the matrix as a binary blob.
st
.
bind
(
1
,
name
);
st
.
bind
(
2
,
age
);
st
.
bind
(
3
,
m
);
st
.
exec
();
// execute the SQL statement. This does the insert.
// We can reuse the statement to add more data to the database. In fact, if you have a
// bunch of statements to execute it is fastest if you reuse them in this manner.
name
=
"John"
;
age
=
82
;
m
=
randm
(
2
,
3
);
st
.
bind
(
1
,
name
);
st
.
bind
(
1
,
name
);
st
.
bind
(
2
,
age
);
st
.
bind
(
2
,
age
);
st
.
bind
(
3
,
m
);
st
.
bind
(
3
,
m
);
...
@@ -41,19 +75,57 @@ int main() try
...
@@ -41,19 +75,57 @@ int main() try
statement
st2
(
db
,
"select * from davis"
);
// Now lets print out all the rows in the people table.
statement
st2
(
db
,
"select * from people"
);
st2
.
exec
();
st2
.
exec
();
// Loop over all the rows obtained by executing the statement with .exec().
while
(
st2
.
move_next
())
while
(
st2
.
move_next
())
{
{
string
name
;
string
name
;
int
age
;
int
age
;
matrix
<
double
>
m
;
matrix
<
double
>
m
;
// Analogously to bind, we can grab the columns straight into C++ types. Here the
// matrix is automatically deserialized by calling its deserialize() routine.
st2
.
get_column
(
0
,
name
);
st2
.
get_column
(
0
,
name
);
st2
.
get_column
(
1
,
age
);
st2
.
get_column
(
1
,
age
);
st2
.
get_column
(
2
,
m
);
st2
.
get_column
(
2
,
m
);
cout
<<
name
<<
" "
<<
age
<<
"
\n
"
<<
m
<<
endl
<<
endl
;
cout
<<
name
<<
" "
<<
age
<<
"
\n
"
<<
m
<<
endl
<<
endl
;
}
}
// Finally, if you want to make a bunch of atomic changes to a database then you should
// do so inside a transaction. Here, either all the database modifications that occur
// between the creation of my_trans and the invocation of my_trans.commit() will appear
// in the database or none of them will. This way, if an exception or other error
// happens halfway though your transaction you won't be left with your database in an
// inconsistent state.
//
// Additionally, if you are going to do a large amount of inserts or updates then it is
// much faster to group them into a transaction.
transaction
my_trans
(
db
);
name
=
"Dude"
;
age
=
49
;
m
=
randm
(
4
,
2
);
st
.
bind
(
1
,
name
);
st
.
bind
(
2
,
age
);
st
.
bind
(
3
,
m
);
st
.
exec
();
name
=
"Bob"
;
age
=
29
;
m
=
randm
(
2
,
2
);
st
.
bind
(
1
,
name
);
st
.
bind
(
2
,
age
);
st
.
bind
(
3
,
m
);
st
.
exec
();
// If you comment out this line then you will see that these inserts do not take place.
// Specifically, what happens is that when my_trans is destructed it rolls back the
// entire transaction unless commit() has been called.
my_trans
.
commit
();
}
}
catch
(
std
::
exception
&
e
)
catch
(
std
::
exception
&
e
)
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment