Unsolved Advice about the application I will be making? (I'm a beginner)
-
-
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.
-
@KillerSmath
Then the question is: can you specify something atopen()
time to override that behaviour, without changing the driver? Or, can you temporarily change that driver flag from code before opening? Then the OP would have found out/use it in future.Meanwhile, the OP needs to understand that the two paths (one with space, one with backslash) refer to quite different files!
-
I see so that's the reason why there is a newly created database in my folder. Also backslash "\" specifies the parent folder. I have encountered that on web dev. Thank you!
-
@JonB
Your answer is on sqlite source code.Unhapply, that behaviour is only avoided if you are using SQLITE_OPEN_READONLY flag
-
@KillerSmath
Then @dvlpr-bernard might want to do aQFile::exists()
check on the filepath prior to theopen()
if he wants to ensure he is opening a correctly-spelled existing database rather than accidentally creating a blank new one.... -
I tried different code but it didn't work. What should I do to replace query variable value with my variable in qt.
"select Answer from mydatabase where QQuantity = 5 and Topic = 'FOR' and Difficulty_Level = 'ADVANCED'"
-
@dvlpr.bernard This should work:
query.prepare("select answer from mydatabase where QQuantitiy=:qquantity AND Topic =:topic and Difficulty_Level =:difficulty_level") query.bindValue(":qquantity", current_num); query.bindValue(":topic", ch_topic); query.bindValue(":difficulty_level", ch_div_lvl);
binValue will add necessary escape sequencies for string values.
you only have to ensure placeholder in prepare string is the same as which given tobindValue()
.Read QSqlQuery Class for more details.
-
@KroMignon
It is working now there is just a misspelled word QQuantitiy to QQuantity. Thank you sir :) -
Good day!
I got strange spacing whenever I get a question from my database.
Before I paste to the database:
After I executed the query
Sir @mrjj you mentioned earlier that I can make it look more like a code in a text editor. Like so
How is that? -
I got strange spacing whenever I get a question from my database.
Before I paste to the database:
After I executed the query
Are you using "raw" or tabulation spaces to indent your example codes ?
@mrjj has mentioned the QSyntaxHighlighter