SQLite - no query unable to fetch row
-
Hello, I am trying to add a row into SQLite database and getting above error... First part executes well but the second one databaseQuery() not... I don't understand why it works if I leave db open in first part, and not work if I close db in first part and then reopen it in the second... Is it safe to leave database open in the first part? What if application crashes?
@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QApplication::setStyle(QStyleFactory::create("Fusion")); this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint & ~Qt::WindowMaximizeButtonHint); db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("data.db"); db.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE=1"); bool open = db.open(); if (open) qDebug() << "Success"; QSqlQuery query; query.exec("CREATE TABLE IF NOT EXISTS login(USERNAME, PASSWORD)"); query.exec("CREATE TABLE IF NOT EXISTS party(NAME)"); db.close(); // If I remove this line then query in function databaseQuery works@
@void MainWindow::databaseQuery(QString s)
{
QSqlQuery query(db);
bool open = db.open();
if (open)
qDebug() << "Success";
query.prepare("INSERT INTO party VALUES ('test')");
query.exec();
db.close();}@