Results 1 to 4 of 4

Thread: Qt SQLite

  1. #1
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Qt SQLite

    Hi, i'm doing a little aplication in qt that insert data in a SQLite, in my pc it's ok but in other pc's the aplication don't insert the data :s

    The aplication: http://dl.dropbox.com/u/6416035/release.zip

    The code:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QSqlDatabase>
    3. #include <QDebug>
    4. #include <QSqlQuery>
    5. #include <QSqlError>
    6. #include <QSqlTableModel>
    7. #include <QTableView>
    8. #include <QString>
    9. #include <QDir>
    10. #include <QFileDialog>
    11. #include <QMessageBox>
    12. #include <QSqlRecord>
    13. #include <QLabel>
    14. #include <QProgressBar>
    15. #include <QLayout>
    16. #include <QVBoxLayout>
    17. #include <QTimer>
    18.  
    19. MainWindow::MainWindow(QWidget *parent)
    20. : QMainWindow(parent)
    21. {
    22.  
    23. QString dir = QFileDialog::getOpenFileName(this, tr("Open Directory"),"");//,tr("SQLite (*.*)"));
    24. //qDebug()<<dir;
    25. QString path(dir);
    26. //path.append(QDir::separator()).append("dbpass");
    27. path = QDir::toNativeSeparators(path);
    28. //qDebug()<<path;
    29. //db.setDatabaseName(path);
    30.  
    31. db = QSqlDatabase::addDatabase("QSQLITE");
    32. //db.setDatabaseName("thomsonkeysDB");
    33. db.setDatabaseName(path);
    34.  
    35. if (db.open()) {
    36. // Try to locate the contacts database.
    37. // If it is not available create it.
    38. if (db.tables().indexOf("thomsonkeys") == -1) {
    39. QSqlQuery query(db);
    40. query.prepare("CREATE TABLE thomsonkeys (ssid VARCHAR(6), pass VARCHAR(10))");
    41. if (!query.exec())
    42. qCritical() << query.lastError();
    43. }
    44. } else {
    45. qCritical() << db.lastError();
    46. }
    47.  
    48. QVBoxLayout *layout = new QVBoxLayout;
    49. progresso = new QProgressBar;
    50. progresso->setMaximum(174680046);
    51. layout->addWidget(progresso);
    52.  
    53. QWidget *w=new QWidget();
    54. w->setLayout(layout);
    55. this->setCentralWidget(w);
    56.  
    57.  
    58. QTimer::singleShot(100, this, SLOT(teste()));
    59. }
    60.  
    61. MainWindow::~MainWindow()
    62. {
    63.  
    64. }
    65.  
    66. void MainWindow::teste(){
    67. QString dir = QFileDialog::getOpenFileName(this, tr("Open Directory"),"",tr("Texto (*.txt)"));
    68. QFile f(dir);
    69. f.open(QIODevice::ReadOnly);
    70. if(f.isOpen())
    71. {
    72. QSqlQuery query(db);
    73.  
    74. int step=174680046/100;
    75. int bytesProcessed=0;
    76. QString linha;
    77. progresso->setValue(bytesProcessed);
    78. while (not f.atEnd()){
    79. linha = f.readLine().data();
    80. bytesProcessed+= linha.size();
    81. list = linha.split(":");
    82. query.exec("insert into thomsonkeys values('"+list.at(0)+"', '"+list.at(1)+"')");
    83. if (bytesProcessed%step<=10){
    84. qDebug()<<"bytesProcessed:"<<bytesProcessed;
    85. progresso->setValue(bytesProcessed);
    86. }
    87. }
    88. progresso->setValue(bytesProcessed);
    89.  
    90. }
    91. else
    92. {
    93. QMessageBox::warning(this,"Erro","Sem ficheiro com as passwords!");
    94. }
    95. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 23rd October 2010 at 13:32.

  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: Qt SQLite

    What is the error you get from your query? Also you should consider using prepare() to avoid a potential security leak because of SQL injection.

  3. #3
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt SQLite

    I discover that the problem is that the sqlite file wasn't created :S

  4. #4
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Cool Re: Qt SQLite

    Thankyou. I used your database creation/open technique to create the SQLite file on the harddisk and it is working very fine.

Similar Threads

  1. [Qt][SQLite] Two problems with SQLite.
    By Xandareva in forum Newbie
    Replies: 6
    Last Post: 7th April 2010, 00:06
  2. Help Sqlite + QT
    By vinny gracindo in forum Newbie
    Replies: 4
    Last Post: 5th December 2009, 08:33
  3. Sqlite e QT4
    By vinny gracindo in forum Newbie
    Replies: 1
    Last Post: 27th October 2009, 16:44
  4. SQLite in QT
    By sophister in forum Qt Programming
    Replies: 11
    Last Post: 8th April 2009, 17:09
  5. sqlite
    By spx2 in forum Qt Programming
    Replies: 9
    Last Post: 19th December 2006, 23:01

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.