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. Saving to SQLite from text edit (help pwetty pwease)
Forum Updated to NodeBB v4.3 + New Features

Saving to SQLite from text edit (help pwetty pwease)

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 4 Posters 9.3k 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.
  • S Scottish_Jason

    @clochydd Hi

    I have actually already tried this approach also without success.
    I just tried again with your code and it still never worked.

    I'm really scratching my head. qDebug show's the appropriate values that I have typed into the table cells but it just simply does not split them out to the database. I am running DB Browser for SQLite on my 2nd monitor to monitor for any changes and none are happening. It's a mystery!

    [if (db4.open())
    {
    QSqlQuery query(db4);
    int rowcount1 = ui->physicaltable->rowCount();
    int rowcount2 = ui->logicaltable->rowCount() - 1;
    if (rowcount1 > 0)
    {
    QString dp1 = ui->physicaltable->item(0,0)->text();
    QString cp1 = ui->physicaltable->item(0,1)->text();
    query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = 'FC-28';");
    query.bindValue(":dp", dp1);
    query.bindValue(":cp", cp1 );
    query.exec();
    qDebug() << dp1 << " " << cp1;
    }
    }

    p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #11

    @Scottish_Jason Use QSqlQuery::lastError to check for errors if any.

    157

    S 1 Reply Last reply
    1
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #12

      @Scottish_Jason You can open your database with another connection:

      db4 = QSqlDatabase::addDatabase("QSQLITE","xsql"); // adds connection xsql
      db4.setDatabaseName (fullpath + "sensors.sqlite");
      if(!db4.open ()){
          qDebug() << db4.lastError().text();
      }
      

      Provide the full path to your .sqlite to make sure it's accessable

      1 Reply Last reply
      1
      • p3c0P p3c0

        @Scottish_Jason Use QSqlQuery::lastError to check for errors if any.

        S Offline
        S Offline
        Scottish_Jason
        wrote on last edited by Scottish_Jason
        #13

        @p3c0 - p3c0 the error returns "true" ??? The application also locks up and / or crashes

        if (db4.open())
        {
            QSqlQuery query(db4);
            int rowcount1 = ui->physicaltable->rowCount();
            int rowcount2 = ui->logicaltable->rowCount() - 1;
            if (rowcount1 > 0)
            {
            QString dp1 = ui->physicaltable->item(0,0)->text();
            QString cp1 = ui->physicaltable->item(0,1)->text();
        
            query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = 'FC-28';");
            query.bindValue(":dp", dp1);
            query.bindValue(":cp", cp1 );
            if (query.exec())
                 {
                qDebug() << "inserted";
                 }
                 else
                {
                qDebug() << QSqlQuery::lastError;
                }
        
            }
        

        }

        p3c0P 1 Reply Last reply
        0
        • S Scottish_Jason

          @p3c0 - p3c0 the error returns "true" ??? The application also locks up and / or crashes

          if (db4.open())
          {
              QSqlQuery query(db4);
              int rowcount1 = ui->physicaltable->rowCount();
              int rowcount2 = ui->logicaltable->rowCount() - 1;
              if (rowcount1 > 0)
              {
              QString dp1 = ui->physicaltable->item(0,0)->text();
              QString cp1 = ui->physicaltable->item(0,1)->text();
          
              query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = 'FC-28';");
              query.bindValue(":dp", dp1);
              query.bindValue(":cp", cp1 );
              if (query.exec())
                   {
                  qDebug() << "inserted";
                   }
                   else
                  {
                  qDebug() << QSqlQuery::lastError;
                  }
          
              }
          

          }

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #14

          @Scottish_Jason No. You didn't use it properly. I posted it like that just for the sake of that link. Try :

          query.exec()
          ...
          qDebug() << query.lastError() << query.lastError().text();
          //Also some other useful methods
          qDebug() << query.lastQuery();
          qDebug() << query.executedQuery();
          

          157

          S 1 Reply Last reply
          1
          • p3c0P p3c0

            @Scottish_Jason No. You didn't use it properly. I posted it like that just for the sake of that link. Try :

            query.exec()
            ...
            qDebug() << query.lastError() << query.lastError().text();
            //Also some other useful methods
            qDebug() << query.lastQuery();
            qDebug() << query.executedQuery();
            
            S Offline
            S Offline
            Scottish_Jason
            wrote on last edited by Scottish_Jason
            #15

            @p3c0

            QSqlError("5", "Unable to fetch row", "database is locked") "database is locked Unable to fetch row"

            I am unsure how to handle databases... so far I have been opening and closing them on each function since I can't call db.whatever if I have not initialized it, or the lack of it being a global variable. Is this my issue?

            p3c0P 1 Reply Last reply
            0
            • S Scottish_Jason

              @p3c0

              QSqlError("5", "Unable to fetch row", "database is locked") "database is locked Unable to fetch row"

              I am unsure how to handle databases... so far I have been opening and closing them on each function since I can't call db.whatever if I have not initialized it, or the lack of it being a global variable. Is this my issue?

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #16

              @Scottish_Jason Don't do that. Instead open the connection only once. QSqlQuery will always use this default connection.

              157

              S 1 Reply Last reply
              1
              • p3c0P p3c0

                @Scottish_Jason Don't do that. Instead open the connection only once. QSqlQuery will always use this default connection.

                S Offline
                S Offline
                Scottish_Jason
                wrote on last edited by
                #17

                @p3c0

                When I take out the secondary QSqlQuery query(db) lines I always end up with query was not declared. How do I initialize the database per function? it has always confused me until now

                p3c0P 1 Reply Last reply
                0
                • S Scottish_Jason

                  @p3c0

                  When I take out the secondary QSqlQuery query(db) lines I always end up with query was not declared. How do I initialize the database per function? it has always confused me until now

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #18

                  @Scottish_Jason Check this example specifically the createConnection method. Modify it as per your need.

                  157

                  S 1 Reply Last reply
                  1
                  • p3c0P p3c0

                    @Scottish_Jason Check this example specifically the createConnection method. Modify it as per your need.

                    S Offline
                    S Offline
                    Scottish_Jason
                    wrote on last edited by
                    #19

                    @p3c0 Thanks, but what if I want to use the same database throughout many functions?
                    how do I pass the db variable to open?

                    p3c0P 1 Reply Last reply
                    0
                    • S Scottish_Jason

                      @p3c0 Thanks, but what if I want to use the same database throughout many functions?
                      how do I pass the db variable to open?

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #20

                      @Scottish_Jason That is what is default connection. It is available across all functions or classes. Just use QSqlQuery directly or you can also try

                      QSqlQuery query(QSqlDatabase::database())
                      

                      in this case it will return the default one.

                      157

                      S 1 Reply Last reply
                      1
                      • p3c0P p3c0

                        @Scottish_Jason That is what is default connection. It is available across all functions or classes. Just use QSqlQuery directly or you can also try

                        QSqlQuery query(QSqlDatabase::database())
                        

                        in this case it will return the default one.

                        S Offline
                        S Offline
                        Scottish_Jason
                        wrote on last edited by Scottish_Jason
                        #21

                        @p3c0

                        well guys, thanks for the help but I think I really need to hang up the hat on this one... no matter how I try to initialize the database on a second function ( or lack of initilization) I either get database not open or duplicate database open that causes instability and crashes.

                        I have been up all night trying to get it working so maybe I'm just tired but I'm starting to think this might be a bit over my head. Thanks for the help guys

                        p3c0P 1 Reply Last reply
                        0
                        • S Scottish_Jason

                          @p3c0

                          well guys, thanks for the help but I think I really need to hang up the hat on this one... no matter how I try to initialize the database on a second function ( or lack of initilization) I either get database not open or duplicate database open that causes instability and crashes.

                          I have been up all night trying to get it working so maybe I'm just tired but I'm starting to think this might be a bit over my head. Thanks for the help guys

                          p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #22

                          @Scottish_Jason I would suggest you to start from some simple examples keeping the earlier example code in mind ofcourse after some sleep. It works :)

                          157

                          S 1 Reply Last reply
                          1
                          • p3c0P p3c0

                            @Scottish_Jason I would suggest you to start from some simple examples keeping the earlier example code in mind ofcourse after some sleep. It works :)

                            S Offline
                            S Offline
                            Scottish_Jason
                            wrote on last edited by Scottish_Jason
                            #23

                            @p3c0

                            The strange thing is I am only implementing this block once in my loadsensors() function

                            QString dbname = "sensors";
                            QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                            db.setDatabaseName("sensors.sqlite");
                            QSqlQuery query(QSqlDatabase::database());
                            

                            and then just QSqlQuery query; in the following functions but it still insists that the database is locked.
                            ( I need query defined so I can do the prepare's etc on each function )

                            I probably sound awfully silly right now but I'm kinda struggling coming from a php mainly background to this. Any hints down the correct path would be appreciated

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              clochydd
                              wrote on last edited by clochydd
                              #24

                              @Scottish_Jason
                              Hi, what happens, if you declare your query that way:

                              QSqlQuery query;
                              ...
                              query = QSqlQuery(db);
                              
                              1 Reply Last reply
                              1
                              • C Offline
                                C Offline
                                clochydd
                                wrote on last edited by
                                #25

                                @Scottish_Jason

                                You are using DB Browser for SQLite parallel - is there a chance that it locks your database?

                                S 1 Reply Last reply
                                1
                                • C clochydd

                                  @Scottish_Jason

                                  You are using DB Browser for SQLite parallel - is there a chance that it locks your database?

                                  S Offline
                                  S Offline
                                  Scottish_Jason
                                  wrote on last edited by Scottish_Jason
                                  #26

                                  @clochydd said:

                                  @Scottish_Jason

                                  You are using DB Browser for SQLite parallel - is there a chance that it locks your database?

                                  YAYYY thanks so much.
                                  the browser itself was the culprit. Values are being updated now.

                                  Thank you all +reps

                                  one last question if I may:

                                  instead of dropping the data into the 'FC-28' column I would like it to drop it under the name of whatever is selected on another table, any help in that direction?

                                  query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = "selected item on sensor_table");
                                  query.bindValue(":dp", dp1);
                                  query.bindValue(":cp", cp1 );

                                  p3c0P 1 Reply Last reply
                                  0
                                  • S Scottish_Jason

                                    @clochydd said:

                                    @Scottish_Jason

                                    You are using DB Browser for SQLite parallel - is there a chance that it locks your database?

                                    YAYYY thanks so much.
                                    the browser itself was the culprit. Values are being updated now.

                                    Thank you all +reps

                                    one last question if I may:

                                    instead of dropping the data into the 'FC-28' column I would like it to drop it under the name of whatever is selected on another table, any help in that direction?

                                    query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = "selected item on sensor_table");
                                    query.bindValue(":dp", dp1);
                                    query.bindValue(":cp", cp1 );

                                    p3c0P Offline
                                    p3c0P Offline
                                    p3c0
                                    Moderators
                                    wrote on last edited by
                                    #27

                                    @Scottish_Jason Do it in the same way which you did for the other placeholders viz. for eg. :dp and :cp in your example.

                                    157

                                    1 Reply Last reply
                                    1
                                    • C Offline
                                      C Offline
                                      clochydd
                                      wrote on last edited by
                                      #28

                                      @Scottish_Jason
                                      Hi Jason, may look like:

                                      QString mySearch;
                                      ...
                                      mySearch = "FC-28";
                                      query.prepare("UPDATE sensors SET pin1 = :dp, attached_gpio = :cp WHERE sensor_name = :search;");
                                      query.bindValue(":dp", dp1);
                                      query.bindValue(":cp", cp1 );
                                      query.bindValue(":search", mySearch );
                                      
                                      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