PDA

View Full Version : QSql Column-Header



reinki0013
13th May 2009, 08:44
Hello,

i have a question...
i have a qsqlite-database and i add a column in the database.
how can i get the column-headers from the database?
the problem is, there are some columns and i want to check if there exists
a column-headername?

because i don`t want to have two columns who have the same name...

hope you can help me...

reinki

wysota
13th May 2009, 15:08
Could you explain (maybe with an example) what you mean?

reinki0013
19th May 2009, 09:38
Hello,

i have a little problem...

i would like to make a database and add in the database a dynamic column...

the database have to look like this:

ID | File | Category | German

and when i click in my programm "add a new language" it have to look like this:

ID | File | Category | German | English

and so on...

the problem is, the first language is in the database and by the second language it creates only the column and put no data in the column...


for(i = m_Texte.TextMap.begin(); i != m_Texte.TextMap.end(); i++)
{
QSqlTableModel model;
model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setTable("Texte");
model.select();

QModelIndex insertIndex;

row = insertIndex.row() == -1 ? 0 : insertIndex.row();
model.insertRow(row);
QSqlRecord record = model.record(row);

// ID, File, Category, Language
record.setValue("ID", i->ID);
record.setValue("File", Datei);
record.setValue("Category", i->Kategorie);
record.setValue(Language, i->MapText[spr1]);

model.setRecord(row, record); // Record setzen
model.submitAll(); //
qDebug() << model.lastError();
}
model.select();

when i click to add the new language, by "model.lastError()" i become an error:

The record could not be fetched! "constraint failed"...

does anybody know why i get this error?

kind regards

wysota
19th May 2009, 09:49
The code you pasted is related to adding rows, not columns... Creating the model all over in the for loop looks like a bad idea as well...

And here:

record.setValue(Language, i->MapText[spr1]);
What does "Language" point to? Is it an enum?

reinki0013
19th May 2009, 10:01
Hello,

I`m sorry that I`ve made the posting code without
tags...
sorry...

sorry, Language is not a enum, it`s just a QString where the Language is contains...

so i`ve made a copy & paste mistake:

[CODE]
QSqlTableModel model;
model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setTable("Texte");
model.select();

for(i = m_Texte.TextMap.begin(); i != m_Texte.TextMap.end(); i++)
{
QModelIndex insertIndex;
row = insertIndex.row() == -1 ? 0 : insertIndex.row();
model.insertRow(row);
QSqlRecord record = model.record(row);

// ID, File, Category, Language
record.setValue("ID", i->ID);
record.setValue("File", Datei);
record.setValue("Category", i->Kategorie);
record.setValue("Language", i->MapText[spr1]);

model.setRecord(row, record); // set record
model.submitAll(); //
qDebug() << model.lastError();
}
model.select();

can anybody help me with my problem?

wysota
19th May 2009, 14:36
What is "insertIndex"? You can't create indexes on your own, you have to ask the model to provide one for you. And still I don't know how the code you pasted is related to the code you described.