Project

General

Profile

Dbo: how to add column?

Added by Vladimir Zibarov over 11 years ago

Hello.

In short, how to add new fields to mapped tables in database

I have a database using Wt::Dbo and Sqlite3 backend. First time db created with:

Wt::Dbo::backend::Sqlite3 sqlite3_(WApplication::instance()->appRoot() + "my.db")

Wt::Dbo::Session session;

session.setConnection(sqlite3);

session.mapClass("MyClass");

session.createTables();

Wt::Dbo::Transaction transaction(session);

try {

session.createTables();

Wt::log("info") << "Database created";

} catch (...) {

Wt::log("info") << "Using existing database";

}

I filled up database with many items of MyClass.

Now I added some new fields to MyClass - dateAdded and lastSeen, and error occured:

[error] "Wt: fatal error: Sqlite3: update ""MyClass"\" set ""version"" = ?, ""role"" = ?, ""name"" = ?, ""dateAdded"" = ?, ""lastSeen"" = ? where ""id"" = ? and ""version"" = ?: no such column: dateAdded\"

How to update database file with this new fields?


Replies (2)

RE: Dbo: how to add column? - Added by Saif Rehman over 11 years ago

You'll have to alter the table using the "ALTER TABLE" SQL Statement and run it by calling Wt::Dbo::Session::execute() once.

Or you can alter the table manually.

RE: Dbo: how to add column? - Added by Vladimir Zibarov over 11 years ago

Thanks a lot!

I added lines:

session.execute("alter table MyClass add column 'dateAdded' 'text'");

session.execute("alter table MyClass add column 'lastSeen' 'text'");

Then executed my programm once and after that commented those lines.

    (1-2/2)