Example #1
0
	void Database::createDatabase() {

		qDebug() << "Creating new database:" << path();

		try {
			if (not scriptPath().isEmpty())
				runSqlScript(scriptPath());
		} catch (ErrorOpeningDatabase *e) {
			throw new ErrorCreatingDatabase(e->path());
		} catch (SqlQueryError *e) {
			qDebug() << e->what();
			throw e;
		}
	}
Example #2
0
/**
 * @brief DbCoordinator::openDatabase Open a database or create a new
 * one if non existent.
 * @param path the path to the folder that contains the database
 * @param dbName the name of the database to open. Relative or Absolute path.
 */
void DbCoordinator::openDatabase(QString path, QString dbName)
{
    if (isOpened()) {
        qDebug() << "Database is already opened!";
        return;
    }

    QString dbFilePath(QString(path + "/" + dbName));

    QFile dbFile(dbFilePath);
    bool needsGenerateSchema = false;

    if(!dbFile.exists()) {
        needsGenerateSchema = true;
    }

    m_db = QSqlDatabase::addDatabase("QSQLITE");
    m_db.setDatabaseName(dbFilePath);
    bool db_opened = m_db.open();

    if (db_opened) {
        qDebug() << "Opened Database for access.";

        opened = true;

        if (needsGenerateSchema) {
            QString schemaFilePath(QString(path + "TAEval.sql"));
            qDebug() << "Generating Schema..." << schemaFilePath;

            QFile* file = new QFile(schemaFilePath);
            runSqlScript(file);

            delete file;

            qDebug() << "Generated DB " << dbName;
        }
    }
}