PDA

View Full Version : Trouble with "INSERT" by QSqlTableModel



AD
7th November 2008, 11:05
/// Добавление серийного номера
bool AddSerialNumber::add()
{
/// Иметь в виду, что длина строки = 9 байт. 9-й байт - конец строки. При генерации файла ключа выкидывать 9-й байт
while(element -> serialName.size() <= 8)
element -> serialName += (QChar)32;

int model = selectModelID();

QSqlTableModel model;
model.setTable("DeviceList");
//model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.select();
int row = model.rowCount();
bool k = model.insertRow(row);
model.setData(model.index(row, 0), element -> serialName);
model.setData(model.index(row, 1), element -> secret);
model.setData(model.index(row, 2), true);
model.setData(model.index(row, 3), modelID);
bool ins = model.submitAll();
string err = model.lastError().text().toStdString();

return ins;
}

This code does not insert record in table. I don't understand - why? That code is executed:


// Добавление серийного номера
bool AddSerialNumber::add()
{
while(element -> serialName.size() <= 8)
element -> serialName += (QChar)32;

int modelID = selectModelID();
QSqlQuery query;
QString insert_query1 = QString("insert into DeviceList(serialNumber, secretKey, security, modelID) "
"values('%1', '%2', %3, %4)").arg(element -> serialName).arg(element -> secret).arg(true).arg(modelID);
bool ins = query.exec(insert_query1);

return ins;
}


What is wrong? Why do I can't use QSqlTableModel?

caduel
7th November 2008, 12:02
there is a bug in Qt that might be related to this: http://trolltech.com/developer/task-tracker/index_html?method=entry&id=227468
(check your debug output for "No fields to update.")

Try if your code works with Qt 4.4.0

HTH

AD
7th November 2008, 12:15
there is a bug in Qt that might be related to this: http://trolltech.com/developer/task-tracker/index_html?method=entry&id=227468
(check your debug output for "No fields to update.")

Try if your code works with Qt 4.4.0

HTH

My code works with Qt 4.3.2. Can I use QSqlTableModel for INSERT, UPDATE? :(

caduel
7th November 2008, 12:46
My code works with Qt 4.3.2. Can I use QSqlTableModel for INSERT, UPDATE?
sure.
(of course, only with a Qt version that is not buggy here. i.e. <= 4.4.0 and hopefully >= 4.4.4)

AD
7th November 2008, 12:50
sure.
(of course, only with a Qt version that is not buggy here. i.e. <= 4.4.0 and hopefully >= 4.4.4)

Where are errors? :) Why my code doesn't work i.e. submitAll return false for INSERT. Why?

caduel
7th November 2008, 12:57
possibilites
i) the bug i mentioned (or some other bug)
ii) did you check the columns match the order matches your inserts?

Is there any debug output that you get?
What error is returned (if any)?

AD
7th November 2008, 13:26
Error

[Microsoft][SQL Native Client][SQL Server]Operand type clash: text is incompatible with bit [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared. QODBC3: Unable to execute statement
which geted by
model.lastError()

How can I check the order? I saw mentioned order by Microsoft Sql Express Manager

caduel
7th November 2008, 14:51
looks like the order of the fields is not as you expected (seems like column 1 is "security"; you can check by displaying the QSqlTableModel in a QTableView).

How about this approach:
populate (access columns by name) a QSqlRecord (you get an empty one with record()) and insert it with QSqlTableModel::insertRecord()

This way your code does not depend on the order of the columns in the table.

HTH

AD
7th November 2008, 15:07
looks like the order of the fields is not as you expected (seems like column 1 is "security"; you can check by displaying the QSqlTableModel in a QTableView).

How about this approach:
populate (access columns by name) a QSqlRecord (you get an empty one with record()) and insert it with QSqlTableModel::insertRecord()

This way your code does not depend on the order of the columns in the table.

HTH


// Добавление серийного номера
bool AddSerialNumber::add()
{
while(element -> serialName.size() <= 8)
element -> serialName += (QChar)32;

int modelID = selectModelID();
QSqlTableModel model;model.setTable("DeviceList");
model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.select();
QSqlRecord record = model.record( );
record.setValue("serialNumber", element -> serialName);
record.setValue("secretKey", element -> secret );
record.setValue("security", true);
record.setValue( "modelID", modelID);
model.insertRecord(-1, record);
bool ins = model.submitAll( );

return ins;
}


This code doesn't works too. submitAll return false

AD
8th November 2008, 12:01
Au! Help, please!

caduel
8th November 2008, 12:56
any error message / debug output that could help us?

AD
10th November 2008, 07:31
This error is:


[Microsoft][SQL Native Client][SQL Server]Operand type clash: text is incompatible with bit [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared. QODBC3: Unable to execute statement


ODBC driver work right. Intructions UPDATE, SELECT works, INSERT with QSqlQuery works!

caduel
10th November 2008, 08:21
i) dump the QSqlRecord to stderr, check esp. the QVariant::Types of the fields
ii) if those do not match your db: fix the setValue calls to the record to be inserted
iii) assuming those are correct: file a bug report to the Trolls