Part example:

Qt Code:
  1. #define DBHOST "127.0.0.1" //for localhost loop
  2. #define DBDRIVER "QSQLITE" //if use other DB need the driver with dll files
  3. #define DBNAME "DB.db3" //name used for the database file
  4. //#define
  5.  
  6. //constructor
  7. Database::Database() {
  8. createConnectiontoSqlite();
  9. }
  10. ....
  11.  
  12. bool Database::createConnectiontoSqlite() {
  13. QSqlDatabase db = QSqlDatabase::addDatabase(DBDRIVER);// changed "QSQLITE" with define DBDRIVER
  14. db.setHostName(DBHOST);
  15. db.setDatabaseName(DBNAME); //if the dababase does not exist he will create it
  16. //the database name is also the name of the file in the working directory
  17. // db.setUserName("user");//username and password does not affect SQLITE db
  18. // db.setPassword("pass");
  19.  
  20. if (!db.open()) {
  21. QMessageBox::critical(0, QObject::tr("DB Error - CreateConnection()"),
  22. db.lastError().text());
  23. return false;
  24. }
  25. return true;
  26.  
  27. }
  28.  
  29. void Database::createTables() {
  30. QSqlQuery query;
  31. //main Tables
  32. // table
  33. query.exec(
  34. "create table table1(id int primary key, name varchar(20), variable int, company int)");
  35. query.exec("insert into table1 values(1, 'Default', 1, 1)");
  36. query.exec("insert into table1 values(2, 'blabla', 2, 1)");
  37. .....
  38.  
  39.  
  40. qDebug() << "dB created "; //need to set up debug in the .pro file
To copy to clipboard, switch view to plain text mode 

hope this help