I am having problem with my database application. I am using a postgreSQL and QT4. I have created a connection in the main.cpp and it works fine.
There are other forms too, but none of them is using an inline function to automaticaly generate the id number to some of the tables.
Here is the structure of the table:
CREATE TABLE "Blood"
(
"GroupID" int4 NOT NULL,
"Group" varchar(3),
CONSTRAINT "Blood_pkey" PRIMARY KEY ("GroupID")
)
WITHOUT OIDS;
ALTER TABLE "Blood" OWNER TO postgres;
Here are the functions I use to insert a new data in the table:
inline int generateId
(const QString &table
) {
query.exec(" SELECT MAX(GroupID) FROM " + table);
if (!query.isActive())
cerr << "Error" << endl;
int id = 0;
if (query.next())
id = query.value(0).toInt() + 1;
return id;
}
void Krv::AddRec()
{
int row = model->rowCount();
model->insertRow(row);
tableView->setCurrentIndex(index);
tableView->edit(index);
}
{
record.setValue("GroupID", generateId(table));
}
inline int generateId(const QString &table)
{
QSqlQuery query;
query.exec(" SELECT MAX(GroupID) FROM " + table);
if (!query.isActive())
cerr << "Error" << endl;
int id = 0;
if (query.next())
id = query.value(0).toInt() + 1;
return id;
}
void Krv::AddRec()
{
int row = model->rowCount();
model->insertRow(row);
QModelIndex index = model->index(row, Group);
tableView->setCurrentIndex(index);
tableView->edit(index);
}
void Krv::beforeInsertRec(QSqlRecord &record)
{
record.setValue("GroupID", generateId(table));
}
To copy to clipboard, switch view to plain text mode
I am constantly getting an error message as the query is not active.
I can delete from the same form, I can insert a first record, but the id wont iterate.
It always got the id=0 value? Can someone help???
Bookmarks