Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Romain De Laage De Bellefaye
LO21-projet
Commits
e48051ca
Verified
Commit
e48051ca
authored
Jun 02, 2021
by
Romain De Laage De Bellefaye
🌳
Browse files
First tests sql
parent
0ca8f77f
Changes
4
Hide whitespace changes
Inline
Side-by-side
files/autosql.cpp
0 → 100644
View file @
e48051ca
#include
<iostream>
#include
"autosql.h"
Database
::
Database
(
std
::
string
path
)
:
db
(
QSqlDatabase
::
addDatabase
(
"QSQLITE"
))
{
db
.
setDatabaseName
(
path
.
c_str
());
if
(
!
db
.
open
())
{
throw
"Unable to open database"
;
}
db
.
exec
(
"CREATE TABLE IF NOT EXISTS automates (nom VARCHAR(30) PRIMARY KEY, voisinage INTEGER, fonction INTEGER)"
);
}
std
::
vector
<
QString
>
Database
::
getAutomates
()
const
{
QSqlQuery
query
=
db
.
exec
(
"SELECT nom FROM automates"
);
std
::
vector
<
QString
>
names
;
if
(
query
.
first
())
{
names
.
push_back
(
query
.
value
(
"nom"
).
toString
());
}
while
(
query
.
next
())
{
names
.
push_back
(
query
.
value
(
"nom"
).
toString
());
}
return
names
;
}
int
main
()
{
Database
db
(
"test.sqlite"
);
auto
automates
=
db
.
getAutomates
();
for
(
auto
&
automate
:
automates
)
{
std
::
cout
<<
automate
.
toStdString
()
<<
std
::
endl
;
}
return
0
;
}
files/autosql.h
0 → 100644
View file @
e48051ca
#ifndef _AUTOSQL_H
#define _AUTOSQL_H
#include
<QSqlDatabase>
#include
<QSqlDriver>
#include
<QSqlError>
#include
<QSqlQuery>
#include
<QVariant>
class
Database
{
private:
QSqlDatabase
db
;
public:
Database
(
std
::
string
path
);
~
Database
()
{
db
.
close
();
}
std
::
vector
<
QString
>
getAutomates
()
const
;
};
#endif
files/autosql.pro
0 → 100644
View file @
e48051ca
QT
+=
core
QT
+=
sql
HEADERS
+=
autosql
.
h
SOURCES
+=
autosql
.
cpp
files/test.sqlite
0 → 100644
View file @
e48051ca
File added
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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