sorted it out with some help from the kexi source code...
heres my .h
Qt Code:
  1. #ifndef BACKEND_H
  2. #define BACKEND_H
  3.  
  4. #include <QObject>
  5. #include <QSqlDatabase>
  6. #include <QSqlTableModel>
  7. #include <QSqlQuery>
  8. #include <QSqlRecord>
  9. #include <QDebug>
  10. #include <QStringList>
  11. #include <QList>
  12.  
  13. struct field {
  14. QString type;
  15. QString name;
  16.  
  17. bool isKey;
  18. bool isNotNull;
  19. bool isAutoIncrement;
  20.  
  21. field() : isKey(false), isNotNull(false), isAutoIncrement(false){}
  22.  
  23. };
  24.  
  25. Q_DECLARE_METATYPE(field);
  26.  
  27. class backend : public QObject
  28. {
  29. Q_OBJECT
  30. public:
  31. explicit backend(QObject *parent = 0);
  32. ~backend();
  33.  
  34. QSqlDatabase * database() { return ptr_db; }
  35. bool sqlExec(QString sql);
  36. bool addTable(QString name, const QList<field>& fields);
  37. bool dropTable(QString name);
  38. void addRecord(QString table, QSqlRecord record, int pos = -1);
  39. bool isConnected() { return connected; }
  40. bool connectDatabase(QString connection_name, QString db_name);
  41. QString connectionName() { return ptr_db->connectionName(); }
  42.  
  43.  
  44. signals:
  45.  
  46. public slots:
  47.  
  48. private:
  49. QSqlDatabase *ptr_db;
  50. bool connected;
  51. };
  52.  
  53. #endif // BACKEND_H
To copy to clipboard, switch view to plain text mode 

and the .cpp
Qt Code:
  1. #include "backend.h"
  2.  
  3. backend::backend(QObject *parent) :
  4. QObject(parent)
  5. {
  6. connected = false;
  7. }
  8.  
  9. backend::~backend()
  10. {
  11. ptr_db->close();
  12. ptr_db->removeDatabase(ptr_db->connectionName());
  13. }
  14.  
  15. bool backend::connectDatabase(QString connection_name, QString db_name)
  16. {
  17. ptr_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", connection_name));
  18. ptr_db->setDatabaseName(db_name);
  19. if (ptr_db->open()){
  20. connected = true;
  21. qDebug() << "database connection established...";
  22. return true;
  23. }
  24. connected = false;
  25. qDebug() << "unable to connect to database...";
  26. return false;
  27. }
  28.  
  29. bool backend::dropTable(QString name)
  30. {
  31. QSqlQuery query(*ptr_db);
  32. if (!query.exec("DROP TABLE IF EXISTS " + name)){
  33. return false;
  34. }
  35. qDebug() << "table" << name << "dropped from database...";
  36. return true;
  37. }
  38.  
  39. void backend::addRecord(QString table, QSqlRecord record, int pos)
  40. {
  41. model.setTable(table);
  42. model.database().transaction();
  43. model.insertRecord(pos, record);
  44. model.database().commit();
  45. }
  46.  
  47. bool backend::sqlExec(QString sql)
  48. {
  49. QSqlQuery query(sql, *ptr_db);
  50. if (!query.isActive()){
  51. return false;
  52. }
  53. return true;
  54. }
  55.  
  56. bool backend::addTable(QString name, const QList<field> &fields)
  57. {
  58. bool first = true;
  59.  
  60. s = "CREATE TABLE ";
  61. s.append(name.toLatin1() + " ( ");
  62.  
  63. foreach (field f , fields){
  64. if (first){
  65. first = false;
  66. } else {
  67. s.append(", ");
  68. }
  69.  
  70. s.append(f.name.toLatin1() + " " + f.type.toLatin1());
  71.  
  72. if (f.isKey){
  73. s.append(" PRIMARY KEY");
  74. }
  75.  
  76. if (f.isAutoIncrement){
  77. s.append(" AUTOINCREMENT");
  78. }
  79.  
  80. if (f.isNotNull){
  81. s.append(" NOT NULL");
  82. }
  83. }
  84.  
  85. s.append(")");
  86.  
  87. QSqlQuery q(*ptr_db);
  88.  
  89. if (q.exec(s)){
  90. return true;
  91. }
  92.  
  93. return false;
  94. }
To copy to clipboard, switch view to plain text mode