Hello All,
I have a program that is running a lot of different SQL queries (mostly SELECT and UPDATE) and I was wondering what the best practice is for creating QSqlQuery objects.
Specifically: Should I create a separate object for each query, or reuse them?
For example:
QSqlQuery findBook
("select title, author from books where id = 1");
//some code...
QSqlQuery findMoreBooks
("select title, author from books where pages > :pages");
findMoreBooks.bindValue(":pages", 40);
while(findMoreBooks.next()){
//some more code..
}
QSqlQuery updateABook
("update books set title='foo' where id= :id");
QSqlQuery updateMoreBooks
("update books set printable=0 where pages > 40");
QSqlQuery findBook("select title, author from books where id = 1");
//some code...
QSqlQuery findMoreBooks("select title, author from books where pages > :pages");
findMoreBooks.bindValue(":pages", 40);
while(findMoreBooks.next()){
//some more code..
}
QSqlQuery updateABook("update books set title='foo' where id= :id");
QSqlQuery updateMoreBooks("update books set printable=0 where pages > 40");
To copy to clipboard, switch view to plain text mode
Or I could do it like this:
bookQuery.prepare("select title, author from books where id = 1");
bookQuery.exec();
//some code...
bookQuery.prepare("select title, author from books where pages > :pages");
bookQuery.bindValue(":pages", 40);
bookQuery.exec();
while(bookQuery.next(){
//some more code...
}
bookQuery.prepare("update books set title='foo' where id=:id");
bookQuery.exec();
bookQuery.prepare("update books set printable=0 where pages > 40");
bookQuery.exec();
QSqlQuery bookQuery;
bookQuery.prepare("select title, author from books where id = 1");
bookQuery.exec();
//some code...
bookQuery.prepare("select title, author from books where pages > :pages");
bookQuery.bindValue(":pages", 40);
bookQuery.exec();
while(bookQuery.next(){
//some more code...
}
bookQuery.prepare("update books set title='foo' where id=:id");
bookQuery.exec();
bookQuery.prepare("update books set printable=0 where pages > 40");
bookQuery.exec();
To copy to clipboard, switch view to plain text mode
Both seem to work, but I want to make sure I'm doing this the correct way.
Bookmarks