PDA

View Full Version : Help Sqlite + QT



vinny gracindo
30th November 2009, 21:36
listaSqlModel->setQuery("SELECT codigo, COD, nome, cpf, rg, endereco, bairro, cidade, uf, cnh, validade, telefone, data, hora, status FROM Motorista WHERE '"+m_ui->comboDados->currentText()+"' = '"+m_ui->editNome->text().toUpper()+"'");

What Error?

Lykurg
1st December 2009, 14:44
What Error?
I don't know. Tell me! Maybe you want debug your created string and see if it is filled with values? And by the way use QSqlQuery::prepare().

Tanuki-no Torigava
4th December 2009, 08:28
There is a good technique to solve that problem:

1. Obtain sqliteman (www.sqliteman.com) and verify your queries manually.
2. Code like that:




QString sql = QString("ALTER TABLE '%1' ADD COLUMN '%2' %3;")
.arg(s_db_alter_table_list[i].table)
.arg(s_db_alter_table_list[i].column)
.arg(s_db_alter_table_list[i].settings);

QSqlQuery query(m_db);
query.exec(sql);

if (query.lastError().isValid())
{
if (m_useLogging)
emit log(BRIDGE_COMPONENT_ID, priHigh, QString("Alter table %1 error %2 during SQL statement: %3")
.arg(s_db_alter_table_list[i].table).arg(query.lastError().text()).arg(query.la stQuery()));

emit error(QString("Alter table error %1 during SQL statement: %2")
.arg(query.lastError().text())
.arg(query.lastQuery()));
return false;
}

wysota
4th December 2009, 09:02
I'd say prepare() as Lykurg suggested is a much better approach than inserting pieces of data using .arg().

ChrisW67
5th December 2009, 07:33
listaSqlModel->setQuery("SELECT codigo, COD, nome, cpf, rg, endereco, bairro, cidade, uf, cnh, validade, telefone, data, hora, status FROM Motorista WHERE '"+m_ui->comboDados->currentText()+"' = '"+m_ui->editNome->text().toUpper()+"'");

What Error?
You really need to explain what is happening and what you expected to happen. Otherwise, we have to guess and are less likely to help.

Wild guess... the value of m_ui->comboDados->currentText() does not equal the value of m_ui->editNome->text().toUpper() so no rows are returned. I suspect that you are expecting m_ui->comboDados->currentText() to identify a column in the table but, because you are quoting it in the SQL, you are comparing its value as a string to another value as a string.

The prepare() approach will not allow you to substitute the name of a table, so if that's what you are trying to do then you might need a two stage approach:

Build an SQL string with your column name from the program controlled list of column names (Do NOT trust user input for this) and a placeholder for the user edit box value.
Prepare() it.
Bind the user edit box value and execute allowing Qt to handle escaping the user input to avoid SQL injection problems.