Advice about the application I will be making? (I'm a beginner)


  • Lifetime Qt Champion

    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.



  • @dvlpr.bernard

    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 via mydb.open(). A database may contain many tables, each with their own name. It is the table name you mention in SQL select (and most other) statements. You previously created your table(s) via some kind of CREATE TABLE name ... statement, and that name is what you need in your query. It would be "unusual" for you to have named a table mydatabase.

    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.

    0_1556721420905_3aa67a8d-a82e-4456-9548-1500489ec844-image.png

    And here is the same query that I copy paste to qt but suddenly didn't work.

    0_1556721664803_4baa7258-307d-4a59-9dbb-68db56cb80f8-image.png



  • @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):

    0_1556685583290_a11aff6b-ff9a-4e92-828b-3de115afe4c9-image.png

    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");

    0_1556722834541_bdf923ef-e973-41f0-ae89-f8e84f224c65-image.png



  • @dvlpr.bernard

    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 the CODEANALL 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?



  • @dvlpr.bernard

    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 at open() 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 a QFile::exists() check on the filepath prior to the open() 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'"
    0_1556809488139_40bef343-e9e0-41ad-bb2e-070a5789455e-image.png



  • @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 to bindValue().

    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:
    0_1556893610379_c08bd5fe-6b99-41af-86c7-7baba4f004f8-image.png

    After I executed the query
    0_1556893684717_cfe98f8b-b909-4343-b680-0b3131f0226a-image.png

    Sir @mrjj you mentioned earlier that I can make it look more like a code in a text editor. Like so
    0_1556894107853_0cffa870-6b69-4959-9ffe-7b936a3595ab-image.png
    How is that?



  • @dvlpr.bernard

    I got strange spacing whenever I get a question from my database.
    Before I paste to the database:
    0_1556893610379_c08bd5fe-6b99-41af-86c7-7baba4f004f8-image.png

    After I executed the query
    0_1556893684717_cfe98f8b-b909-4343-b680-0b3131f0226a-image.png

    Are you using "raw" or tabulation spaces to indent your example codes ?

    @mrjj has mentioned the QSyntaxHighlighter



  • @KillerSmath
    I was using the tabulation spaces to indent. I thought that was the problem so I tried the raw tabulation or using the spacebar but I still got the same result.



  • @dvlpr.bernard
    I am not sure if you can directly change the tab stop size in QLabel.
    A possible way, since you are using tab spaces to indent your code, would be replace all tabs by raw spaces.

    QString string = "#include <iostream>\n#include <stdlib>\n\nint main(){\n\tbool x;\n}";
    string.replace('\t', QLatin1String("    ")); // 4 raw spaces
    label->setText(string);
    

    However, QTextEdit has the feature to change the TabSize and it is the default widget used by QSyntaxHighlighter.


  • Lifetime Qt Champion

    Hi
    You can take the highlighter from here
    https://doc.qt.io/qt-5/qtwidgets-richtext-syntaxhighlighter-example.html
    (the sample is in creator already )
    and instead of having the question code in a QLabel, use TextEdit as @KillerSmath says.


Log in to reply