Skip to content

storage_t::remove

Yevgeniy Zakharov edited this page Jun 15, 2017 · 3 revisions
template<class O, class I>
void remove(I id);

DELETE FROM ... by primary key function. Calls DELETE FROM %table_name% WHERE %pk% = %id% with table_name equal table name mapped to O class, pk - column name with PRIMARY KEY constraint in the table and id - first argument passed to a function. Column with pk is defined by column passed to a storage in make_storage not in real sqlite database. If table has no column with PRIMARY KEY constraint std::runtime_error will be thrown. Function returns nothing.

If there are no rows with specified id nothing happens cause this is not an error.

If you want to know whether row deleted please use this function with changes function within a transaction.

If you want to DELETE rows from a table with custom WHERE conditions or without them (delete all rows) you have to use remove_all function instead.

Parameters

class O is an object type. Must be specified explicitly.

class I id object. Type of the first argument. Can be omitted cause can be deduced by argument. Actually I must be the same as primary key column type from O but it's not necessary cause any value can be bound to sqlite statement. E.g. you can pass long if you have primary key int and sqlite engine will 'understand' you.

(1) id id of object that must be deleted

Example

struct Object {
    int id;
    std::string name;
};

using namespace sqlite_orm;
auto storage = make_storage("remove_example.sqlite",
                            make_table("objects",
                                       make_column("id",
                                                   &Object::id,
                                                   primary_key()),
                                       make_column("name",
                                                   &Object::name)));
storage.sync_schema();

auto id1 = storage.insert(Object{ 0, "Skillet"});
storage.remove<Object>(id1);