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. Qt + SQLite Tutorial
Forum Updated to NodeBB v4.3 + New Features

Qt + SQLite Tutorial

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 20.6k Views 2 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.
  • I Offline
    I Offline
    iyustlop
    wrote on 6 Jan 2017, 11:56 last edited by iyustlop 1 Jun 2017, 11:59
    #1

    Hi all:

    I need to create an app to manage data of a study. I have created the UI using Qt but I need a tutorial to connect my UI to a database (SQLite).

    Can anyone provide me a Tutorial of how to connect both?

    Thank you very much in advance.

    P.D: I have tried some tutorial but no one give me enough information.

    I am using Windows 10

    A 1 Reply Last reply 17 Jun 2024, 21:24
    1
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 6 Jan 2017, 12:31 last edited by
      #2

      http://doc.qt.io/qt-5/qtsql-cachedtable-example.html
      Look at createconnection method and change do filename like follows
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("tom.db");

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      N 1 Reply Last reply 6 Apr 2021, 20:54
      4
      • I Offline
        I Offline
        iyustlop
        wrote on 7 Jan 2017, 11:37 last edited by
        #3

        Thanks.

        How could I split the way to insert data?? I want separate the connection to the database from the insert.

        Thank you very much in advance.

        K 1 Reply Last reply 7 Jan 2017, 11:53
        0
        • I iyustlop
          7 Jan 2017, 11:37

          Thanks.

          How could I split the way to insert data?? I want separate the connection to the database from the insert.

          Thank you very much in advance.

          K Offline
          K Offline
          koahnig
          wrote on 7 Jan 2017, 11:53 last edited by
          #4

          @iyustlop

          You can basically split the routine everywhere after each step. The first step is the DB connection and its error handling. Afterwards you can put every query step into a separate routine e.g.

          bool routine1 ()
          {
              QSqlQuery query;
              query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
              return true;
          }
          

          and

          bool routine2 ()
          {
              QSqlQuery query;
              query.exec("insert into person values(101, 'Danny', 'Young')");
              return true;
          }
          

          The db conenction kept internally somewhere.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          1
          • I Offline
            I Offline
            iyustlop
            wrote on 7 Jan 2017, 20:19 last edited by
            #5

            Thank very much indeed.

            Last question about database. tom.db have been created in the root directory. Is it possible create it in a directory(Not including the path in the String)??

            K 1 Reply Last reply 7 Jan 2017, 20:24
            0
            • I iyustlop
              7 Jan 2017, 20:19

              Thank very much indeed.

              Last question about database. tom.db have been created in the root directory. Is it possible create it in a directory(Not including the path in the String)??

              K Offline
              K Offline
              koahnig
              wrote on 7 Jan 2017, 20:24 last edited by
              #6

              @iyustlop

              You can add a path to your db name.

              E.g. with

              db.setDatabaseName("c:/data/testing/databases/tom.db");
              

              This is an example for windows. Similar you can do it for linux or any other system.
              Note: you need to create the path to your DB manually outside or with Qt in your program.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • I Offline
                I Offline
                iyustlop
                wrote on 7 Jan 2017, 20:40 last edited by
                #7

                Thanks.

                Amazing!!! The database is created automatically.

                1 Reply Last reply
                0
                • D dheerendra
                  6 Jan 2017, 12:31

                  http://doc.qt.io/qt-5/qtsql-cachedtable-example.html
                  Look at createconnection method and change do filename like follows
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setDatabaseName("tom.db");

                  N Offline
                  N Offline
                  nitya
                  wrote on 6 Apr 2021, 20:54 last edited by
                  #8

                  @dheerendra I have tried this but keep getting loads of errors , i think i need to configure my QT to work with SQLite but dont know how i can do this

                  P 1 Reply Last reply 6 Apr 2021, 21:50
                  0
                  • N nitya
                    6 Apr 2021, 20:54

                    @dheerendra I have tried this but keep getting loads of errors , i think i need to configure my QT to work with SQLite but dont know how i can do this

                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 6 Apr 2021, 21:50 last edited by
                    #9

                    @nitya

                    What errors? QMake or CMake?
                    If you have a pro file, have you added QT += sql?


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    2
                    • I iyustlop
                      6 Jan 2017, 11:56

                      Hi all:

                      I need to create an app to manage data of a study. I have created the UI using Qt but I need a tutorial to connect my UI to a database (SQLite).

                      Can anyone provide me a Tutorial of how to connect both?

                      Thank you very much in advance.

                      P.D: I have tried some tutorial but no one give me enough information.

                      I am using Windows 10

                      A Offline
                      A Offline
                      Amirreza Taghizadeh
                      wrote on 17 Jun 2024, 21:24 last edited by
                      #10

                      @iyustlop Thank you. Your suggestion was awesome! cheers

                      1 Reply Last reply
                      0

                      • Login

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