Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: How to dynamically load data from a mysql database?  (Read 5404 times)

Michaelszh

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to dynamically load data from a mysql database?
« on: December 07, 2016, 04:00:15 pm »
Hello all,
I would like to dynamically load data from the mysql database to the report, the following code correct?

void CShowLimeReport::PreviewReport()
{
   LimeReport::ReportEngine *report = new LimeReport::ReportEngine(m_pWnd);
   QSqlDatabase db = QSqlDatabase::addDatabase(m_strDbType);
   db.setHostName(m_strHostName);
   db.setDatabaseName(m_strDbName);
   db.setUserName(m_strUserName);
   db.setPassword(m_strPassword);
   if (db.open())
   {
      m_customers = new QSqlQuery(m_strQuerySql, db);
      LimeReport::ICallbackDatasource * callbackDatasource = report->dataManager()->createCallbackDatasouce(m_strDatasourceName);
      QObject::connect(callbackDatasource, SIGNAL(getCallbackData(LimeReport::CallbackInfo, QVariant&)), m_pWnd, SLOT(slotGetCallbackData(LimeReport::CallbackInfo info, QVariant &data)));
      QObject::connect(callbackDatasource, SIGNAL(changePos(const LimeReport::CallbackInfo::ChangePosType&, bool&)), m_pWnd, SLOT(slotChangePos(const LimeReport::CallbackInfo::ChangePosType &type, bool &result)));
      report->loadFromFile(m_strReprotName);
      report->previewReport();
   };
}


void CShowLimeReport::slotGetCallbackData(LimeReport::CallbackInfo info, QVariant &data)
{
   if (!m_customers) return;
   prepareData(m_customers, info, data);
}

void CShowLimeReport::prepareData(QSqlQuery* ds, LimeReport::CallbackInfo info, QVariant &data)
{
   switch (info.dataType) {
   case LimeReport::CallbackInfo::ColumnCount:
      data = ds->record().count();
      break;
   case LimeReport::CallbackInfo::IsEmpty:
      data = !ds->first();
      break;
   case LimeReport::CallbackInfo::HasNext:
      data = ds->next();
      ds->previous();
      break;
   case LimeReport::CallbackInfo::ColumnHeaderData:
      if (info.index < ds->record().count())
         data = ds->record().fieldName(info.index);
      break;
   case LimeReport::CallbackInfo::ColumnData:
      data = ds->value(ds->record().indexOf(info.columnName));
      break;
   default: break;
   }
}

void CShowLimeReport::slotChangePos(const LimeReport::CallbackInfo::ChangePosType &type, bool &result)
{
   QSqlQuery* ds = m_customers;
   if (!ds) return;
   if (type == LimeReport::CallbackInfo::First) result = ds->first();
   else result = ds->next();
}

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: How to dynamically load data from a mysql database?
« Reply #1 on: December 07, 2016, 08:25:01 pm »
Hi
It's hard to say :) Your compiler knows it better :)
What I see at once and it's alarming me LimeReport::ReportEngine instance is created but not deleted  - it is memory leak.
Another part of the code, as I understand it, you've got from the demo and it is correct
Can I ask you why you use callbackDatasource instead sending a model to LimeReport ?
« Last Edit: December 08, 2016, 03:56:45 am by Arin Alex »

Michaelszh

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to dynamically load data from a mysql database?
« Reply #2 on: December 08, 2016, 05:04:13 am »
Hi
It's hard to say :) Your compiler knows it better :)
What I see at once and it's alarming me LimeReport::ReportEngine instance is created but not deleted  - it is memory leak.
Another part of the code, as I understand it, you've got from the demo and it is correct
Can I ask you why you use callbackDatasource instead sending a model to LimeReport ?

Hi, Alex

Because I don't know how to sending a model to LimeReport,can you help me?
thank you.

Is it like the following code?The code compiler is no problem, but run error, I was using limereprot in the MFC.

LimeReport::ReportEngine *report = new LimeReport::ReportEngine(m_pWnd);
   QSqlDatabase db = QSqlDatabase::addDatabase(m_strDbType);
   db.setHostName(m_strHostName);
   db.setDatabaseName(m_strDbName);
   db.setUserName(m_strUserName);
   db.setPassword(m_strPassword);
   if (db.open())
   {
      m_customers = new QSqlQuery(m_strQuerySql, db);
      QSqlQueryModel* customersModel = new QSqlQueryModel();
      customersModel->setQuery(m_strQuerySql);
      report->dataManager()->addModel("ds1", customersModel, true);

      report->loadFromFile(m_strReprotName);
      report->previewReport();
                delete customersModel;
   };
   delete report;

The following code works fine

   QWinWidget *pWidget = new QWinWidget(this);
   LimeReport::ReportEngine *report = new LimeReport::ReportEngine(pWidget); //to create reportengine
   report->loadFromFile("myfirstrpt.lrxml");// to load report template file
   report->previewReport();// to generate report and preview
« Last Edit: December 08, 2016, 05:58:04 am by Michaelszh »

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: How to dynamically load data from a mysql database?
« Reply #3 on: December 08, 2016, 09:32:39 pm »
Quote
if (db.open())
   {
      m_customers = new QSqlQuery(m_strQuerySql, db);
      QSqlQueryModel* customersModel = new QSqlQueryModel();
      customersModel->setQuery(m_strQuerySql);
      report->dataManager()->addModel("ds1", customersModel, true);
      report->loadFromFile(m_strReprotName);
      report->previewReport();
      delete customersModel;
   };
   delete report;

If you have added a model to the report and made the report an owner of this model then you must not to delete the model by yourself