I am trying to create a main window and if a SQLite file doesn't exist, then create one. Then populate a combobox with data from a query. I am using QT Creator. I am learning and would like to know why I get the error and not just the correct code if that isn't too much to ask.
Qt Code:
  1. //database.h
  2. //Database functions will go here
  3. #ifndef DATABASE_H
  4. #define DATABASE_H
  5.  
  6. class database
  7. {
  8. public:
  9. database();
  10. };
  11.  
  12. #endif // DATABASE_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //MainWindow.h
  2. #ifndef MXMAINWINDOW_H
  3. #define MXMAINWINDOW_H
  4.  
  5. #include <QMainWindow>
  6.  
  7. namespace Ui {
  8. class MXMainWindow;
  9. }
  10.  
  11. class MXMainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. explicit MXMainWindow(QWidget *parent = 0);
  17. ~MXMainWindow();
  18.  
  19. private:
  20. Ui::MXMainWindow *ui;
  21. };
  22.  
  23. #endif // MXMAINWINDOW_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //database.cpp
  2. #include "database.h"
  3. #include <QtSql>
  4. #include <QApplication>
  5. #include <QtGui>
  6.  
  7. bool createConnection()
  8. {
  9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  10. db.setDatabaseName("rider.mt");
  11. if (!db.open()) {
  12. QMessageBox::warning(0, QObject::tr("Database Error"),
  13. db.lastError().text());
  14. return false;
  15. }
  16.  
  17.  
  18. QSqlQuery dbquery;
  19. dbquery.prepare ("CREATE TABLE Rider (firstname varchar(20))");
  20. dbquery.exec();
  21. dbquery.prepare("insert into Rider (firstname)Jeremy')");
  22. dbquery.exec("insert into Rider values('Ricky', 'Charmichael')");
  23. dbquery.exec("insert into Rider values('Chad', 'Reed')");
  24. dbquery.next();
  25. QString strTest=dbquery.value(0).toString();
  26. QString strTest2= dbquery.value(1).toString();
  27. return true;
  28. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //mainwindow.cpp
  2. #include "mxmainwindow.h"
  3. #include "ui_mxmainwindow.h"
  4. #include <QtSql>
  5. #include "database.h"
  6.  
  7. MXMainWindow::MXMainWindow(QWidget *parent) :
  8. QMainWindow(parent),
  9. ui(new Ui::MXMainWindow)
  10. {
  11. ui->setupUi(this);
  12. while (dbquery.next()) //Error here.. dbquery not declared in this scope
  13. {
  14. ui->cmbName->addItem(dbquery.value(0).toString());
  15. }
  16. }
  17.  
  18. MXMainWindow::~MXMainWindow()
  19. {
  20. delete ui;
  21. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //main.cpp
  2. #include <QtGui/QApplication>
  3. #include "mxmainwindow.h"
  4. #include <QtSql>
  5. #include <QtGui>
  6. bool createConnection()
  7. {
  8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  9. db.setDatabaseName("mxtrainer.dat");
  10. if (!db.open()) {
  11. QMessageBox::warning(0, QObject::tr("Database Error"),
  12. db.lastError().text());
  13. return false;
  14. }
  15. return true;
  16. }
  17.  
  18. void createDb()
  19. {
  20. QSqlQuery query;
  21. //query.exec("DROP TABLE scooter");
  22.  
  23. query.exec("CREATE TABLE rider ("
  24. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  25. "name VARCHAR(20) NOT NULL, "
  26. "weight INTEGER NOT NULL, ");
  27. query.exec("INSERT INTO rider (name, weight) "
  28. "VALUES ('Villapoto', 155");
  29. query.exec("INSERT INTO rider (name, weight) "
  30. "VALUES ('Carmichael', 165");
  31. query.exec("INSERT INTO rider (name, weight) "
  32. "VALUES ('McGrath', 175");
  33.  
  34. }
  35. int main(int argc, char *argv[])
  36. {
  37. QApplication a(argc, argv);
  38. bool create = !QFile::exists("mxtrainer.dat");
  39. if (!createConnection())
  40. return 1;
  41. if (create)
  42. createDb();
  43. MXMainWindow w;
  44. w.show();
  45.  
  46. return a.exec();
  47. }
To copy to clipboard, switch view to plain text mode 
I thought by including the header file of where the declaration is made it would provide the scope? What is the problem with this?
Thanks