Hello Claes,
I have this code:
#include <iostream>
#include "pwr.h"
#include "rt_gdh.h"
#include <mysql/my_global.h>
#include <mysql/mysql.h>
using namespace std;
class ra_myappl {
pwr_tInt32 *field1;
pwr_tInt32 *field2;
pwr_tInt32 *field3;
pwr_tInt32 *field4;
pwr_tSubid subid;
public:
ra_myappl() {}
void init();
void scan();
void close();
};
void ra_myappl::init()
{
pwr_tStatus sts;
pwr_tOName name_field1 = "PLANT-DB-field1.ActualValue";
pwr_tOName name_field2 = "PLANT-DB-field2.ActualValue";
pwr_tOName name_field3 = "PLANT-DB-field3.ActualValue";
pwr_tOName name_field4 = "PLANT-DB-field4.ActualValue";
// Connect to database
sts = gdh_Init("ra_myappl"); // nu conteaza numele
if (EVEN(sts)) exit(0);
// Connect to object
sts = gdh_RefObjectInfo(name_field1, (void**)&field1, &subid, sizeof(*field1));
sts = gdh_RefObjectInfo(name_field2, (void**)&field2, &subid, sizeof(*field2));
sts = gdh_RefObjectInfo(name_field3, (void**)&field3, &subid, sizeof(*field3));
sts = gdh_RefObjectInfo(name_field4, (void**)&field4, &subid, sizeof(*field4));
if (EVEN(sts)) exit(0);
}
void finish_with_error(MYSQL *con)
{
cout << mysql_error(con) << endl;
mysql_close(con);
exit(0);
}
void ra_myappl::scan()
{
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
cout << mysql_error(con) << endl;
exit(0);
}
if (mysql_real_connect(con, "localhost", "pwrp", "", "ovidiu", 0, NULL, 0) == NULL) {
finish_with_error(con);
}
if (mysql_query(con, "SELECT id, nr_sarja, data_start, data_stop FROM brazda1 ORDER BY id DESC LIMIT 1")) {
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
finish_with_error(con);
}
MYSQL_ROW row = mysql_fetch_row(result);
cout << row[0] << " - ";
cout << row[1] << " - ";
cout << row[2] << " - ";
cout << row[3] << endl;
*field1 = atol(row[0]);
*field2 = atol(row[1]);
*field3 = atol(row[2]);
*field4 = atol(row[3]);
mysql_free_result(result);
mysql_close(con);
exit(0);
}
void ra_myappl::close()
{
gdh_UnrefObjectInfo(subid);
}
int main()
{
ra_myappl myappl;
myappl.init();
myappl.scan();
myappl.close();
}
The code it's working. I am not a C programmer. Please tell me if it's a better way to write that code.
I read 4 field from mysql database and I write those fields into proview. Documentation said that gdh_RefObjectInfoList() it's better than gdh_RefObjectInfo(). I don't know how to convert the code.
Regards,
Daniel