tags:

views:

322

answers:

1

Newbie having trouble creating a database here. Following code compiles ok, but no database is created and I can't find any sqlite file.

#include "makeDB.h"
#include <iostream>
#include <QString>
#include <QtSql>

void makeDB(QString dbName) {
   QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
   db.setHostName("localHost");
   db.setDatabaseName(dbName);
   db.setUserName("logger");
   db.setPassword("logger");
}

#include <QtGui/QApplication>
#include <QString>
#include <QtSql>
#include "mainwindow.h"
#include "makeDB.h"
#include "createTable.h"
#include "ui_mainwindow.h"


int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  makeDB("log1");

  createTable();

  return a.exec();
}

What am I doing wrong here? Thanks! Waynew Edit/Delete Message

A: 

We can't see what you createTable() does, the code that you have never calls db.open() most of the QT database and SQL calls return bool for success and there is a lastError() function for both the QSqlDatabase and the QSqlQuery calls. Check those if appropriate calls, i.e. db.open() and query.exec() return false.

e.g.

bool makeDB(QString dbName) {
  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  db.setHostName("localHost");
  db.setDatabaseName(dbName);
  bool result = db.open();
  if (result)
  {
    //do more processing
  }
  else
  {
    qDebug() << db.lastError().text()
  }
  return result;
}

Also I don't think that SqlLite supports any kind of authentication this seems to indicate that you can secure your db by licensing an extension from the creators of SqlLite.

As to the actual table creation there should be some SQL that is executed that looks like this:

 CREATE TABLE (x int, y varchar);

depending on the columns that you actually want.

This is the respective documentation.

Harald Scheirich