This commit is contained in:
Hamed Masafi 2016-05-12 11:11:57 +04:30
parent 068cc1980c
commit 84fa41942a
1 changed files with 27 additions and 27 deletions

View File

@ -14,42 +14,42 @@
### Read data from database: ### Read data from database:
```cpp ```cpp
auto q = FROM(db.posts()) autoq = FROM(db.posts())
WHERE(Post::id() == %1) WHERE(Post::id() == %1)
BIND(postId); BIND(postId);
auto posts = q->toList(); auto posts = q->toList();
// now posts is a QList<Post*> contain all posts in // now posts is a QList<Post*> contain all posts in
// database that has id equal to postId variable // database that has id equal to postId variable
``` ```
### Adding to database: ### Adding to database:
```cpp ```cpp
Post *newPost = new Post; Post*newPost = new Post;
newPost->setTitle("post title"); newPost->setTitle("post title");
db.posts()->append(newPost); db.posts()->append(newPost);
for(int i = 0 ; i < 3; i++){ for(int i = 0 ; i < 3; i++){
Comment *comment = new Comment; Comment *comment = new Comment;
comment->setMessage("comment #" + QString::number(i)); comment->setMessage("comment #" + QString::number(i));
newPost->comments()->append(comment); newPost->comments()->append(comment);
} }
db.saveChanges(); db.saveChanges();
``` ```
### Modify database data: ### Modify database data:
```cpp ```cpp
auto q = FROM(db.posts()) auto q = FROM(db.posts())
WHERE(Post::id() == %1) WHERE(Post::id() == %1)
BIND(postId); BIND(postId);
Post *post = q->first(); Post *post = q->first();
if(post = 0) { if(post) {
post->setTitle("new name"); post->setTitle("new name");
db.saveChanges(); db.saveChanges();
} else { } else {
qWarning("No post found!"); qWarning("No post found!");
} }
``` ```