sqlite override data
-
Hi,
I'm very new in qt and c++ and have the following problem.
I have a database and want to refresh all 5 minutes the data.
The database is displayed by a tableview.
I tried it like this, but it doesn't work..cpp
db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:"); if (!db.open()) { QMessageBox::critical(nullptr, QObject::tr("Cannot open database"), QObject::tr("Unable to establish a database connection.\n" "This example needs SQLite support. Please read " "the Qt SQL driver documentation for information how " "to build it.\n\n" "Click Cancel to exit."), QMessageBox::Cancel); return; } query = new QSqlQuery; query->exec("CREATE TABLE CustomiseToolChange (COLOR varchar(20)," "TOOLNR, TIME int, POCKETSTATUS varchar(20))"); query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) " "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)"); query->bindValue(":COLOR", ""); timer = new QTimer(this); timer->setInterval(5000); timer->start(); connect(timer, SIGNAL(timeout()), this, SLOT(fillDatabaseTableCustomiseToolChange_1())); /////////////////////////////////////// //method: /////////////////////////////////////// void detailScreen::fillDatabaseTableCustomiseToolChange_1() { qint32 testwerteFuerArray_1[30]; int lengthTableCustomiseToolChange = 30; for (int i = 0; i < lengthTableCustomiseToolChange; i++) { int randInt = rand() % (1010000 - 0) + 1000000; testwerteFuerArray_1[i] = randInt; } query->first(); for (int i = 0; i < 10; i++) { query->bindValue(":TOOLNR", testwerteFuerArray_1[i * 3 + 0]); query->bindValue(":TIME", testwerteFuerArray_1[i * 3 + 1]); query->bindValue(":POCKETSTATUS", testwerteFuerArray_1[i * 3 + 2]); query->exec(); } qDebug()<<"query->first() ="<<query->first(); //return always "false" }
Is that the right way?
(I have to do it with a database, I can't insert it directly in the tableview)
Why isquery->first();
always false?
Will my table be updated too, if that works?table.cpp
myTableCustomiseToolChange = new TableCustomise(this); myTableCustomiseToolChange->setTable("CustomiseToolChange"); myTableCustomiseToolChange->select(); myTableview = new QTableView(this); myTableview->setModel(myTableCustomiseToolChange);
The following is only a understanding question:
Why does the model in the file table.cpp know which database should be used?
My modle get only the tablename.myTableCustomiseToolChange->setTable("CustomiseToolChange");
no database name.Sorry for the many questions.
-
@Mogli123 said in sqlite override data:
Why is query->first(); always false?
I guess because it is an insert query, not a select.
You can use the SQLite command line tool to check whether data was inserted or not. -
Thank you for your help.
How can I make a select query, or is that nonsens in my case?
With that tool I can see the database?
I looked at the database like this to show a value from the table.
(only the last row)int queryValue = query->boundValue(1).toInt(); qDebug()<<"queryValue ="<<queryValue;
-
@Mogli123 said in sqlite override data:
How can I make a select query,
Like any other and like shown in the documentation http://doc.qt.io/qt-5/qsqlquery.html
"With that tool I can see the database?" - yes:
sqlite3 PATH_TO_YOUR_DB_FILE
-
My database has no path.
Is my query because of that line "insert"?
query->prepare("INSERT INTO CustomiseToolChange (COLOR, TOOLNR, TIME, POCKETSTATUS) " "VALUES (:COLOR, :TOOLNR, :TIME, :POCKETSTATUS)");
If I use SELECT instead of INSERT the table doesn't show the data any more.
-
@Mogli123 said in sqlite override data:
My database has no path.
Do you use a in-memory database? If not then it has a path. A SQLite database is either in-memory or a file.
"If I use SELECT instead of INSERT the table doesn't show the data any more." - I don't get it? You use INSERT to insert data and you use SELECT to query data (to check whether something was inserted or not in your case). Because if you call query.frist() after executing an INSERT query it will not return anything as there is nothing to return.