SQL / C++ Tutorial using sqlwrapped
13. Add / modify / remove data using generated classes
PreviousNextIndex
The save() method on the generated classes knows if it's a new object or an existing one.
New objects will use insert, existing objects will use update.
To execute a delete statement on an object, use the erase() method.
For erase() and save()/update, the class will use the primary key to identify
which row in the database to delete / update. If there is no primary key
available, the class will use one of the unique indexes of the table. If
there is no unique index available, the erase() method will not be available.
example
#include <sqlite3.h>
#include <libsqlitewrapped.h>
#include <libtutorialdb.h>
int main()
{
StderrLog log;
Database db("tutorial.db", &log); // sqlite3 specific
// create a new entry
db::Player empty(&db);
empty.name = "Ineptus";
empty.save(); // insert because it's a new object
// modify the entry
db::Player Ineptus(db, "Ineptus");
Ineptus.name = "Veterano";
Ineptus.save(); // update because this is an existing object
// delete
db::Player lorangrym(db, "Lorangrym");
if (lorangrym.num)
{
Query q(db);
char sql[1000];
sprintf(sql, "delete from playerresource where player=%ld", lorangrym.num);
q.execute(sql);
lorangrym.erase();
}
}
example9_mysql.cppexample9_sqlite3.cpp
PreviousNext