void GenerateNewBase::createTableCourse(){ QSqlQuery *query = new QSqlQuery(QSqlDatabase::database("localConnect")); if(!query->exec("CREATE TABLE course(course_pk INTEGER PRIMARY KEY, name TEXT, number INTEGER NOT NULL," " position INTEGER NOT NULL UNIQUE)")); { QMessageBox::critical(0, QObject::tr("Cannot create table"), QObject::tr("Unable to create table course.\n" "Click Cancel to exit."), QMessageBox::Cancel); } delete query;}
bool checkAvailabilityDatabase(const QString baseName){ if (!(QFile(baseName).exists())) { QMessageBox msg; msg.setWindowTitle(QObject::tr("Error")); msg.setText(QObject::tr("База данных не была найдена! \nБудет создана навая база данных")); msg.exec(); return false; } return true;}static bool createLocalConnection(){ if(checkAvailabilityDatabase("LocalBase\\database.db")) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","localConnect"); db.setDatabaseName("LocalBase\\database.db"); if (!db.open()) { QMessageBox::critical(0, QObject::tr("Cannot open database"), QObject::tr("Unable to establish a database connection.\n" "Click Cancel to exit."), QMessageBox::Cancel); return false; } } else { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","localConnect"); db.setDatabaseName("LocalBase\\database.db"); GenerateNewBase nb; nb.create(); } return true;}
bool GenerateNewBase::create(){ ... createTableCourse(); ...}void GenerateNewBase::createTableCourse(){ QSqlQuery query(QSqlDatabase::database("localConnect")); if(!query.exec("CREATE TABLE course(course_pk INTEGER PRIMARY KEY, name TEXT, number INTEGER NOT NULL," " position INTEGER NOT NULL UNIQUE)")); { QTextBrowser *textBrowser = new QTextBrowser; textBrowser->setText(query.lastError().text()); textBrowser->show(); QMessageBox::critical(0, QObject::tr("Cannot create table"), QObject::tr("Unable to create table course.\n" "Click Cancel to exit."), QMessageBox::Cancel); }}
C++ (Qt)static bool createLocalConnection(){ if(checkAvailabilityDatabase("LocalBase\\database.db")) { ... } else { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","localConnect"); db.setDatabaseName("LocalBase\\database.db"); // <<<<< Где открытие базы ??? GenerateNewBase nb; nb.create(); } return true;}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","localConnect"); db.setDatabaseName("LocalBase\\database.db"); if (!db.open()) { QMessageBox::critical(0, QObject::tr("Cannot open database"), QObject::tr("Unable to establish a database connection.\n" "Click Cancel to exit."), QMessageBox::Cancel); return false; } GenerateNewBase nb; nb.create();