[odb-users] Problem with QDate and Sqlite when it's mapped with TEXT type

علی دانش adanesh at noornet.net
Sat Sep 5 06:26:06 EDT 2015


Hi,

First, thanks for the work on this great tool.

I have a problem with QDate and sqlite database when QDate is mapped with TEXT type (default behavior). When it is mapped with INTEGER there is no problem, but when it is mapped with TEXT, it is persisted correctly, but retrieved incorrectly (it looks like an uninitialized variable!)



Test code:

Using odb-2.4.0 and Visual Studio 2013 and Qt 5.4.0



1-      odb.exe --std c++11 --profile qt --database sqlite --generate-query --generate-schema "%(Identity)" -I "$(MSBuildProjectDirectory)" -I "$(QTDIR)\\include" -I "$(QTDIR)\\include\\QtCore"

2-      test_qdate.h:


#pragma once

#include <QString>
#include <QDate>
#include "odb/core.hxx"

#pragma db object pointer(std::shared_ptr)
class ProgramModel
{
public:
#pragma db id auto
   int m_id;
   QString m_title;
#pragma db type("TEXT")
   QDate m_insertDate;
};



3-      test_qdate.cpp:


#include <memory>
#include <assert.h>
#include <odb/transaction.hxx>
#include <odb/database.hxx>
#include <odb/sqlite/database.hxx>
#include "odb/schema-catalog.hxx"
#include "test_qdate.h"
#include "test_qdate-odb.hxx"

auto DATABASE_NAME = L"test.sqlite";

void createDB()
{
   auto db = std::make_shared<odb::sqlite::database>(DATABASE_NAME, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
   odb::transaction t(db->begin());
   try
   {
      odb::schema_catalog::create_schema(*db, "", false);
   }
   catch (odb::exception&)
   {
   }
   t.commit();
}

int persist(std::shared_ptr<ProgramModel>& program)
{
   auto db = std::make_shared<odb::sqlite::database>(DATABASE_NAME, SQLITE_OPEN_READWRITE);
   odb::transaction t(db->begin());
   auto id = db->persist(program);
   t.commit();
   return id;
}

std::shared_ptr<ProgramModel> load(int id)
{
   auto db = std::make_shared<odb::sqlite::database>(DATABASE_NAME, SQLITE_OPEN_READWRITE);
   odb::transaction t(db->begin());
   auto result = db->load<ProgramModel>(id);
   t.commit();
   return result;
}

int main(int argc, char* argv[])
{
   createDB();
   auto item1 = std::make_shared<ProgramModel>();
   item1->m_title = "test";
   item1->m_insertDate = QDate::currentDate();
   //---
   auto item2 = load(persist(item1));
   assert(item1->m_insertDate == item2->m_insertDate);
   return 0;
}

Best regards,
Ali


More information about the odb-users mailing list