Unsolved Advice about the application I will be making? (I'm a beginner)
-
This post is deleted! -
@jsulm
Hello sir. So that I can access the string that I declared in other functions.
How could I do it the right way?0_1556355591803_f0274585-e5f9-4e11-8428-9341f8853a75-image.png
-
Hi
Put all the variables in the class!class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
...
private:
Ui::MainWindow *ui;
QString ch_Dif_Lvl; ///// HERE !
QString fc_Topic;
};then you can just use them
void MainWindow::on_pushButton_clicked() { ch_Dif_Lvl = "BEGINNER"; fc_Topic = "Topic"; }
-
@mrjj
Thank you. God bless your kindness to help other people. -
@dvlpr.bernard
Thank you :)
I hope the project is progressing fine. -
@mrjj
Good day sir.I have been trying to get the data from my database it is already connected but whenever the query executes qDebug() returns "QSqlQuery::exec: database not open".
Also I tried to replace this query
qry.exec("select Question from mydatabase where QQuantity = 4 and Topic = 'FOR' and Difficulty_Level = 'ADVANCED'")
with variable and string name
qry.exec("select Question from mydatabase where QQuantity = '"+quantity+"'and Topic = '"ch_topic"' and Difficulty_Level = '"ch_dif_lvl"'")
but it didn't work also. -
Hi
I cant see anything wrong.
Show how code is before the if ( mydb.open ) -
The code above is inside of this button function
Here's the declaration of the database
-
-
I notice that your Output window shows two occurrences of your message
Connected...
. Why is that? Are you connecting more than once?? [In a separate issue, in the long run you absolutely will not want toopen()
and thenclose()
the database for each query. The whole point is to connect once, keep the connection open while you execute all your queries, and close when you are completely done with the database.] -
[...] but it didn't work also.
What does that mean? Error message? Behaviour? What?? Please help us to help you by supplying useful information.
-
-
-
I played around with the code and this error return.
-
-
hi
are you sure the table name is mydatabase ?
seems wrong. unless the table is called taht.
But it seems you used the name of the database.and it even says unknown table.
so use table name instead. -
no such table: mydatabase
@mrjj wrote:
are you sure the table name is mydatabase ?
Are you getting your database vs your tables mixed up in your head? You work with one database at a time. In SQLite that corresponds to the file you pass to
setDatabaseName()
, and is what gets opened viamydb.open()
. A database may contain many tables, each with their own name. It is the table name you mention in SQLselect
(and most other) statements. You previously created your table(s) via some kind ofCREATE TABLE name ...
statement, and that name is what you need in yourquery
. It would be "unusual" for you to have named a tablemydatabase
.You might like to do yourself a favour, to help with all the SQLite queries you're trying to write. Download one of the (free) "workbenches" for SQLite. These are GUI applications which will let you see what is in your database; it will also let you construct and play with queries, till you are ready to copy them over to your Qt code. Google for
sqlite workbench
, there seem to be several, e.g. https://www.sqlite-workbench.com/, https://sqlitestudio.pl, https://alternativeto.net/software/sqlite-manager/. I suggest you take the time to pick one, in the long run I think the effort will be well worthwhile for you. -
Good day.
So here is my database named codeanaldb with a table name "mydatabase". I didn't know why qt couldn't recognize it.And here is the same query that I copy paste to qt but suddenly didn't work.
-
@dvlpr.bernard
In a word, Qt will "accept" any SQL query you can make work in a workbench. If it isn't something else is wrong, e.g. you're not connected to the database you think you are.... -
@dvlpr.bernard said in Advice about the application I will be making? (I'm a beginner):
Are you sure that your database file is Database codeanaldb ? Because in last image you have used Database codeanal.db (with .)
-
Holly cow, after more than a 100 trial and error, I got it guys. So the problem is I didn't set the path correctly but I'm pretty sure I set it correctly earlier but then other code wasn't correct so I played around and tried change the path, I don't know why it is still working even if the path is wrong. So thank you again guys :).
Correct One: - connected and query is worling
mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database\codeanaldb1.db");
Wrong One: - connected but the query isn't working dunno why
mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database codeanaldb1.db"); -
Correct One: - connected and query is worling
mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database\codeanaldb1.db");
Wrong One: - connected but the query isn't working dunno why
mydb.setDatabaseName("C:\Users\bj\Documents\CODEANALL\Database codeanaldb1.db");I suspect (never having used SQLite) that your existing database is the first path. When you specify the second path to
open()
, being non-existent SQLite decides to create that for you, as an empty database, which then does not have the table mentioned in the query/error message. Check your disk (directly in theCODEANALL
folder) to see if the second one exists, or exists so long as you are running the query?If that is the case, there "must" be some way of asking to open only an existent database/file from SQLite/Qt which you could use for safety?
-
I don't know why it is still working even if the path is wrong.
it is because, the QSqliteDriver uses by default, SQLITE_OPEN_CREATE flag to create the file if it is not exists.