Results 1 to 5 of 5

Thread: Help Sqlite + QT

  1. #1

    Default Help Sqlite + QT

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Help Sqlite + QT

    Quote Originally Posted by vinny gracindo View Post
    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().

  3. #3
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Angry Re: Help Sqlite + QT

    There is a good technique to solve that problem:

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


    Qt Code:
    1. QString sql = QString("ALTER TABLE '%1' ADD COLUMN '%2' %3;")
    2. .arg(s_db_alter_table_list[i].table)
    3. .arg(s_db_alter_table_list[i].column)
    4. .arg(s_db_alter_table_list[i].settings);
    5.  
    6. QSqlQuery query(m_db);
    7. query.exec(sql);
    8.  
    9. if (query.lastError().isValid())
    10. {
    11. if (m_useLogging)
    12. emit log(BRIDGE_COMPONENT_ID, priHigh, QString("Alter table %1 error %2 during SQL statement: %3")
    13. .arg(s_db_alter_table_list[i].table).arg(query.lastError().text()).arg(query.lastQuery()));
    14.  
    15. emit error(QString("Alter table error %1 during SQL statement: %2")
    16. .arg(query.lastError().text())
    17. .arg(query.lastQuery()));
    18. return false;
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help Sqlite + QT

    I'd say prepare() as Lykurg suggested is a much better approach than inserting pieces of data using .arg().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help Sqlite + QT

    Quote Originally Posted by vinny gracindo View Post
    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.

Similar Threads

  1. access SQlite from Qt (Windows & Linux)
    By Qt Coder in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 10:07
  2. steps to connect to SQLite from Q
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 8th July 2009, 12:12
  3. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 19:43
  4. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 14:18
  5. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 14:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.