Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The easiest way for a very simple database with tables?
Forum Updated to NodeBB v4.3 + New Features

The easiest way for a very simple database with tables?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hi!
    I need to store (and load) some attributes in different tables (in one database) in my program.
    With this data, I want to recreate some objects with the stored attributes in the database.
    I thought that a normal SQL database (SQLite) would do the job (it surely does somehow), but I noticed, that it will get a bit complicated (to evaluate the stored data)... I there an easier way in QT to achieve all that?
    Thanks for answers!

    J.HilkJ jsulmJ 2 Replies Last reply
    0
    • NiagarerN Niagarer

      Hi!
      I need to store (and load) some attributes in different tables (in one database) in my program.
      With this data, I want to recreate some objects with the stored attributes in the database.
      I thought that a normal SQL database (SQLite) would do the job (it surely does somehow), but I noticed, that it will get a bit complicated (to evaluate the stored data)... I there an easier way in QT to achieve all that?
      Thanks for answers!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @Niagarer ,
      do you know of the SQL-Browser- Example? It does pretty much what you describe.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • NiagarerN Niagarer

        Hi!
        I need to store (and load) some attributes in different tables (in one database) in my program.
        With this data, I want to recreate some objects with the stored attributes in the database.
        I thought that a normal SQL database (SQLite) would do the job (it surely does somehow), but I noticed, that it will get a bit complicated (to evaluate the stored data)... I there an easier way in QT to achieve all that?
        Thanks for answers!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Niagarer What about SQLite? You can easily use it in Qt. And you do not need a SQL server.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        NiagarerN 1 Reply Last reply
        2
        • jsulmJ jsulm

          @Niagarer What about SQLite? You can easily use it in Qt. And you do not need a SQL server.

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by
          #4

          @jsulm
          Well, I can't find any useful docs or tutorials or examples about SQLite in C++ that is complete and does not use any third party plugins and that is portable.

          @J-Hilk
          Thanks, I will take a look.

          mrjjM 1 Reply Last reply
          0
          • NiagarerN Niagarer

            @jsulm
            Well, I can't find any useful docs or tutorials or examples about SQLite in C++ that is complete and does not use any third party plugins and that is portable.

            @J-Hilk
            Thanks, I will take a look.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Niagarer
            What you mean ?
            Both Qt + sqlite has many docs and the sqllite itself have good docs also.
            If its the actual SQL syntax, its not included as
            thats out of scope in same way as Qt docs dont try to teach you c++.

            The minimal example is

            bool createConnection() {
              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.setDatabaseName(":memory:");
              if (!db.open()) {
                QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel);
                return false;
              }
              QSqlQuery query;
              qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                                   "firstname varchar(20), lastname varchar(20), num int )");
              query.exec("insert into person values(101, 'Dennis', 'Young','1')");
              query.exec("insert into person values(102, 'Christine', 'Holand','2')");
              query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
              query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
              query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
              return true;
            }
            
            ..
            MainWindow::MainWindow(QWidget* parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow) {
              ui->setupUi(this);
              
              createConnection();
            
              QSqlQuery query;
              int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)"));
              query.bindValue(":id", 4);
              query.bindValue(":first", "Lars junior");
              query.bindValue(":last", "Gordon");
              query.exec();
            
            
            NiagarerN 1 Reply Last reply
            4
            • mrjjM mrjj

              @Niagarer
              What you mean ?
              Both Qt + sqlite has many docs and the sqllite itself have good docs also.
              If its the actual SQL syntax, its not included as
              thats out of scope in same way as Qt docs dont try to teach you c++.

              The minimal example is

              bool createConnection() {
                QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                db.setDatabaseName(":memory:");
                if (!db.open()) {
                  QMessageBox::critical(0, qApp->tr("Cannot open database"), "Click Cancel to exit.", QMessageBox::Cancel);
                  return false;
                }
                QSqlQuery query;
                qDebug() << "table:" <<   query.exec("create table person (id int primary key, "
                                                     "firstname varchar(20), lastname varchar(20), num int )");
                query.exec("insert into person values(101, 'Dennis', 'Young','1')");
                query.exec("insert into person values(102, 'Christine', 'Holand','2')");
                query.exec("insert into person values(103, 'Lars junior', 'Gordon','4')");
                query.exec("insert into person values(104, 'Roberto', 'Robitaille','5')");
                query.exec("insert into person values(105, 'Maria', 'Papadopoulos','3')");
                return true;
              }
              
              ..
              MainWindow::MainWindow(QWidget* parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow) {
                ui->setupUi(this);
                
                createConnection();
              
                QSqlQuery query;
                int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)"));
                query.bindValue(":id", 4);
                query.bindValue(":first", "Lars junior");
                query.bindValue(":last", "Gordon");
                query.exec();
              
              
              NiagarerN Offline
              NiagarerN Offline
              Niagarer
              wrote on last edited by Niagarer
              #6

              @mrjj
              Yes, well in the mean time I found some more useful docs on the sqlite site. Anyway, the docs of SQLite, I can do something with them.... once I know everything else about SQLite that's not explained by them.
              I just wondered, wheather there is an easier way of storing and loading minimal static data in tables in a database or at least in a sql-like db style but with easier tools to evaluate it. Doesn't seem to be the case. But with QSQLite it seems to be feasible.
              Thanks for the example, it really helps!

              mrjjM 1 Reply Last reply
              0
              • NiagarerN Niagarer

                @mrjj
                Yes, well in the mean time I found some more useful docs on the sqlite site. Anyway, the docs of SQLite, I can do something with them.... once I know everything else about SQLite that's not explained by them.
                I just wondered, wheather there is an easier way of storing and loading minimal static data in tables in a database or at least in a sql-like db style but with easier tools to evaluate it. Doesn't seem to be the case. But with QSQLite it seems to be feasible.
                Thanks for the example, it really helps!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Niagarer
                Hi
                Most use of database is somewhat the same as its SQL that does it.
                Also if you need sql-like db, so you can easy select subsets of the data, its
                most likely the most easy way using sqllite.

                1 Reply Last reply
                1
                • NiagarerN Offline
                  NiagarerN Offline
                  Niagarer
                  wrote on last edited by Niagarer
                  #8

                  update
                  Well, there does not seem to be another popular and easy way to go than using SQL.
                  SQLite turned out as an easy and fun way. The SQL-Browser- Example shows good, how it works.
                  To sum it up (for SQLite):

                  • To open an existing database (or create a new one, if it doesn't exist yet)
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLIITE");
                  db.setDatabaseName("G:/databases/myDB.db"); // path to your database
                  if(!db.open())
                      // error
                  // now it's opened
                  
                  • To read or write data, we need a query. To execute SQLite-commands, call query.exec("commands")
                  QSqlQuery query;
                  query.exec("SELECT * FROM Student");
                  while(query.next()){
                      QString name = query.value("Name").toString;
                      int age = query.value("Age").toInt();
                      // and so on
                  }
                  
                  • Creating table
                  query.exec("CREATE TABLE 'Student' ( \
                                 'ID'	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \
                                 'Name'	TEXT, \
                                 'Age'	INTEGER ); \
                               ");
                  
                  • inserting new data into a table
                  query.exec("INSERT INTO Person(Name, Age) VALUES ('"+name+"', '"+age+"');");
                  
                  

                  please also look at the minimal example by @mrjj above and SQL-Browser- Example

                  • to see what was happening, I found the DB Browser for SQLite very useful.

                  Hope it helps :)

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved