Results 1 to 3 of 3

Thread: Import PostgreSQL Values to SQLITE

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Import PostgreSQL Values to SQLITE

    Hi everybody,

    I am trying to make a function that import data from a table from PostgreSQL and insert the data on a sqlite3 database.

    But i dont know if my idea will work, can somebody help me:
    Qt Code:
    1. bool MainWindow::connectToDB()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    4. db.setHostName("localhost");
    5. db.setDatabaseName("lernit");
    6. db.setUserName("admin");
    7. db.setPassword("admin");
    8.  
    9. if(!db.open())
    10. {
    11. QMessageBox::information(this,"LenrIT",db.lastError().text());
    12. }
    13. else
    14. {
    15. QSqlQuery select("SELECT language1, language2, plural, description FROM words_tbl");
    16. while(select.next())
    17. {
    18. QString language1 = select.value(0).toString();
    19. QString language2 = select.value(1).toString();
    20. QString plural = select.value(2).toString();
    21. QString description = select.value(3).toString();
    22.  
    23. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE");
    24. db2.setDatabaseName("LernIT.db");
    25. if(!db2.open())
    26. QMessageBox::information(this,"LenrIT",db.lastError().text());
    27.  
    28. QSqlQuery insert;
    29. insert.prepare("insert into words_tbl (language1, language2, plural, description) values ('"
    30. + language1 + "', '" + language2 + "', '" + plural + "', '" + description + "')");
    31. if( ! insert.exec() )
    32. QMessageBox::information(this,"LernIT",insert.lastError().text());
    33.  
    34.  
    35. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    36. db.setHostName("localhost");
    37. db.setDatabaseName("lernit");
    38. db.setUserName("admin");
    39. db.setPassword("admin");
    40. if(!db.open())
    41. QMessageBox::information(this,"LenrIT",db.lastError().text());
    42.  
    43. }
    44.  
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 24th May 2007 at 17:46. Reason: wrapped too long line
    Think DigitalGasoline

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Import PostgreSQL Values to SQLITE

    You dont know if it will work ? Run it

    Why you create second handler to posgre database ?
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Import PostgreSQL Values to SQLITE

    Quote Originally Posted by raphaelf View Post
    QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE");
    It should be:
    Qt Code:
    1. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE", "something");
    To copy to clipboard, switch view to plain text mode 
    This way you will create a second connection, without closing the first one, so you won't have to connect to the database every time you want to insert something.

    Quote Originally Posted by raphaelf View Post
    QSqlQuery insert;
    This should be:
    Qt Code:
    1. QSqlQuery insert( db2 );
    To copy to clipboard, switch view to plain text mode 
    because you want to use that other connection for this statement.

    Quote Originally Posted by raphaelf View Post
    insert.prepare("insert into words_tbl (language1, language2, plural, description) values ('"
    + language1 + "', '" + language2 + "', '" + plural + "', '" + description + "')");
    This is dangerous, better use bindValue():
    Qt Code:
    1. insert.prepare("insert into words_tbl (language1, language2, plural, description) values ( :lang1, :lang2, :plural, :desc )" );
    2. insert.bindValue( ":lang1", language1 );
    3. ...
    To copy to clipboard, switch view to plain text mode 
    This way you can also optimise your code a bit by invoking insert.prepare() only once.

    Your code should look like this:
    • open connection to PostgreSQL
    • open connection to SQLite
    • prepare the insert statement
    • prepare the select statement
    • execute the select statement
    • for each selected row:
      • bind values to the insert statement
      • execute the insert statement

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.