-
I've been trying to get a working example of inserting records with a field which has a Here's my short example test code and would love some insight on what I'm doing wrong as when I inject the new record, the But when manualy running a SQL Tried also with #include <fmt/core.h>
#include <memory>
#include <optional>
#include <spdlog/spdlog.h>
#include <sqlite_orm/sqlite_orm.h>
#include <string>
// #define OPTIONAL
// #define CURRENT_TIMESTAMP
struct Personnel {
public:
std::int32_t id;
std::string name;
std::int32_t age;
std::string address;
std::string designation;
std::uint64_t bigNumber;
#ifdef OPTIONAL
std::optional<std::string> createdAt;
#else
std::string createdAt;
#endif
};
int main(int argc, char **argv)
{
Personnel person{-1, "Test", 21, "Somewhere", "Mr.", 7903197353};
const auto personnelTable = sqlite_orm::make_table(
"personnel", //
sqlite_orm::make_column("id", &Personnel::id, sqlite_orm::primary_key().autoincrement()), //
sqlite_orm::make_column("name", &Personnel::name), //
sqlite_orm::make_column("age", &Personnel::age), //
sqlite_orm::make_column("address", &Personnel::address), //
sqlite_orm::make_column("designation", &Personnel::designation), //
#ifdef CURRENT_TIMESTAMP
sqlite_orm::make_column("createdAt", &Personnel::createdAt, sqlite_orm::default_value("CURRENT_TIMESTAMP")), //
#else
sqlite_orm::make_column("createdAt", &Personnel::createdAt, sqlite_orm::default_value(sqlite_orm::datetime("now", "localtime"))), //
#endif
sqlite_orm::make_column("bigNumber", &Personnel::bigNumber) //
);
const std::string dbFile{"db.sqlite"};
try {
auto storage = sqlite_orm::make_storage(dbFile, personnelTable);
storage.sync_schema(true);
spdlog::info("Database opened: {0}", dbFile);
auto insertedId = storage.insert(person);
spdlog::info("insert: {0}", insertedId);
auto newPerson = storage.get<Personnel>(insertedId);
#ifdef OPTIONAL
spdlog::info("read: \"{0}\", \"{1}\", \"{2}\"", newPerson.id, newPerson.name,
newPerson.createdAt.has_value() ? *newPerson.createdAt : "NULL");
#else
spdlog::info("get: \"{0}\", \"{1}\", \"{2}\"", newPerson.id, newPerson.name, newPerson.createdAt);
#endif
newPerson.name = fmt::format("{0}-{0}", person.name);
storage.update(newPerson);
auto personP = storage.get_pointer<Personnel>(insertedId);
if (personP) {
#ifdef OPTIONAL
spdlog::info("read: \"{0}\", \"{1}\", \"{2}\"", personP->id, personP->name,
personP->createdAt.has_value() ? *personP->createdAt : "NULL");
#else
spdlog::info("get: \"{0}\", \"{1}\", \"{2}\"", personP->id, personP->name, personP->createdAt);
#endif
}
} catch (std::system_error error) {
spdlog::error(error.what());
}
return 0;
} $ rm db.sqlite
$ build/sqlite-test
[2024-06-12 11:15:13.697] [info] Database opened: db.sqlite
[2024-06-12 11:15:13.701] [info] insert: 1
[2024-06-12 11:15:13.702] [info] get: "1", "Test", ""
[2024-06-12 11:15:13.704] [info] get: "1", "Test-Test", ""
$ sqlite3 db.sqlite .schema
CREATE TABLE IF NOT EXISTS "personnel" ( "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "name" TEXT NOT NULL , "age" INTEGER NOT NULL , "address" TEXT NOT NULL , "designation" TEXT NOT NULL , "createdAt" TEXT DEFAULT ((DATETIME('now', 'localtime'))) NOT NULL , "bigNumber" INTEGER NOT NULL );
CREATE TABLE sqlite_sequence(name,seq); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
|
Beta Was this translation helpful? Give feedback.
-
sqlite_orm has explicit |
Beta Was this translation helpful? Give feedback.
Complete solution FFR