I'm trying to import a M$ Access database *.mdb into a SQLite.
My database is made by Extreme Movie Manager, a windows software to manage a Movie collection.

I read it on my Linux with MDB Tools Library.

In Qt I can read the table that I made whit this peace of code:

Qt Code:
  1. QSqlQuery query;
  2. query.exec("select MovieID, Title from Movies");
  3.  
  4. while(query.next())
  5. qDebug() << query.value(0).toString() << " - " << query.value(1).toString();
To copy to clipboard, switch view to plain text mode 

But if I try to show the database on a QTableView I obtain this error message: "The program has unexpectedly finished."
Qt Code:
  1. model->setQuery("select MovieID, Title from Movies");
  2. model->setHeaderData(0, Qt::Horizontal, QObject::tr("MovieID"));
  3. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Title"));
  4.  
  5. QTableView *view = new QTableView;
  6. view->setModel(model);
  7. view->setWindowTitle("Prova");
  8. view->show();
To copy to clipboard, switch view to plain text mode 

The program fail on the second line.

Below the second question the complete code.

What type of data should I specify in the SQLite table create command? I saw this page: http://www.sqlite.org/datatype3.html. But here there aren't int o varchar that I can see in the Qt Examples.

Main
Qt Code:
  1. #include <QtGui/QApplication>
  2. //#include <QTextCodec>
  3.  
  4. //#include <QtGui>
  5. #include <QSqlQueryModel>
  6. #include <QTableView>
  7.  
  8. #include "connection.h"
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. QApplication app(argc, argv);
  13. //QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  14. //QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  15.  
  16. if (!createConnection("example.mdb"))
  17. return 1;
  18.  
  19. QSqlQuery query;
  20. query.exec("select MovieID, Title from Movies");
  21.  
  22. while(query.next())
  23. qDebug() << query.value(0).toString() << " - " << query.value(1).toString();
  24.  
  25. model->setQuery("select MovieID, Title from Movies");
  26. model->setHeaderData(0, Qt::Horizontal, QObject::tr("MovieID"));
  27. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Title"));
  28.  
  29. QTableView *view = new QTableView;
  30. view->setModel(model);
  31. view->setWindowTitle("Prova");
  32. view->show();
  33.  
  34. return app.exec();
  35.  
  36. delete view;
  37. }
To copy to clipboard, switch view to plain text mode 

Connection.h
Qt Code:
  1. #ifndef CONNECTION_H
  2. #define CONNECTION_H
  3.  
  4. #include <QtDebug>
  5. #include <QMessageBox>
  6. #include <QSqlDatabase>
  7. #include <QSqlError>
  8. #include <QSqlQuery>
  9.  
  10. // MDB Tools
  11. #include <mdbtools.h>
  12. #include <mdbsql.h>
  13.  
  14. const QString strCreateTable = "CREATE TABLE Movies ("
  15. "MovieID INTEGER PRIMARY KEY" // 1
  16. ", Title TEXT"
  17. ")";
  18.  
  19. const QString strAddData = "INSERT INTO Movies ("
  20. "MovieID, Title"
  21. ") VALUES ("
  22. ":MovieID, :Title"
  23. ")";
  24.  
  25. static void addData(QSqlQuery *qry, MdbSQL *sql)
  26. {
  27. qry->prepare( strAddData );
  28. int i=0;
  29. qry->bindValue(":MovieID", sql->bound_values[i++]); // TODO Long
  30. qry->bindValue(":Title", sql->bound_values[i++]);
  31. }
  32.  
  33. static bool createConnection(const QString &fileName)
  34. {
  35. qDebug() << "MDBSQL: Engine initialize";
  36. MdbSQL *sql = mdb_sql_init ();
  37.  
  38. qDebug() << "MDBSQL: Open database: " << fileName;
  39. if (!(sql->mdb = mdb_open (fileName.toLatin1() , MDB_NOFLAGS))) {
  40. qDebug() << "MDBSQL: Failed to open database";
  41. QMessageBox::warning(0, qApp->tr("Cannot open database"),
  42. qApp->tr("Unable to establish a connection"
  43. "to the database %1").arg(fileName));
  44. mdb_sql_exit(sql);
  45. mdb_exit();
  46. return false;
  47. }
  48.  
  49. qDebug() << "SQLite: Create database on memory";
  50. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  51. db.setDatabaseName(":memory:");
  52. if (!db.open()) {
  53. qDebug() << "SQlite: Failed to create database";
  54. QMessageBox::warning(0, qApp->tr("Cannot open database"),
  55. qApp->tr("Unable to establish a connection"
  56. "to the database %1").arg(fileName));
  57. mdb_sql_exit(sql);
  58. mdb_exit();
  59. return false;
  60. }
  61.  
  62. QSqlQuery query;
  63. query.prepare( strCreateTable );
  64. if (!query.exec()) {
  65. qDebug() << "SQLITE: Failed to create the table to store data";
  66. QMessageBox::warning(0, qApp->tr("Cannot open database"),
  67. qApp->tr("Unable to establish a connection"
  68. "to the database %1").arg(fileName));
  69. mdb_sql_close(sql);
  70. mdb_sql_exit(sql);
  71. mdb_exit();
  72. return false;
  73. }
  74.  
  75. qDebug() << "SQLite: Start convertion database from MDB Tool Library";
  76. sql = mdb_sql_run_query (sql, "SELECT * FROM Movies");
  77. mdb_sql_bind_all(sql);
  78. while (mdb_fetch_row(sql->cur_table)) {
  79. addData(&query, sql);
  80. if (!query.exec()) {
  81. qDebug() << "SQLITE: Failed to add a record on the database";
  82. QMessageBox::warning(0, qApp->tr("Cannot open database"),
  83. qApp->tr("Unable to establish a connection"
  84. "to the database %1").arg(fileName));
  85. mdb_sql_close (sql);
  86. mdb_sql_exit(sql);
  87. mdb_exit();
  88. return false;
  89. }
  90. }
  91. qDebug() << "SQLite: Load data complete!";
  92.  
  93. qDebug() << "Terminate MDB SQL Engine";
  94. mdb_sql_reset(sql);
  95. mdb_sql_close(sql);
  96. mdb_sql_exit(sql);
  97. mdb_exit();
  98.  
  99.  
  100. return true;
  101. }
  102.  
  103. #endif // CONNECTION_H
To copy to clipboard, switch view to plain text mode 

Project
Qt Code:
  1. # -------------------------------------------------
  2. # Project created by QtCreator 2009-03-26T12:16:36
  3. # -------------------------------------------------
  4. QT += sql \
  5. core
  6. TARGET = mdb2qt
  7. TEMPLATE = app
  8. SOURCES += main.cpp
  9. HEADERS += connection.h
  10. FORMS +=
  11.  
  12. # Mdb Tools
  13. LIBS += -lmdb \
  14. -lmdbsql \
  15. -lglib-2.0
  16. INCPATH += /usr/include/glib-2.0 \
  17. /usr/lib/glib-2.0/include
To copy to clipboard, switch view to plain text mode