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. SQLite select and update statements
QtWS25 Last Chance

SQLite select and update statements

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlite datebasesqlite queries
6 Posts 4 Posters 2.4k Views
  • 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.
  • deleted511D Offline
    deleted511D Offline
    deleted511
    wrote on last edited by
    #1

    Good afternoon, I am having a problem with selecting and updating queries in sqlite.
    Basically I use the select and update commands in my sqlite command shell and it gives me
    the correct output but if I use the same statement in my QT creator it gives
    me an error about unable to fetch query row.
    The select and update function only works if I have one string picked out
    rather than a whole bunch. Let me show you.
    qry.prepare("update patientTable set lname='"+lname+"'where
    patientId='"+patientId+"'"); works and updates but when I try:

    qry.prepare("update patientTable set patientId='"+patientId+"',
    fname='"+fname+"',middle='"+middle+"',lname='"+lname+
    "',suffix='"+suffix+"',dob='"+dob+"',gender='"+g
    ender+"',physician='"+physician+"',description='"+descript
    ion+"',anatomy='"+anatomy+"',accession='"+accession+"
    ',datetime='"+datetime+"'where
    patientId='"+patientId+"'");
    it gives me an error about query fetch row. I then thought it would be an database error but then put the same
    command into my sqlite and it updated the corresponding values with both
    commands shown above. I am also having the same problem with my select
    statement where if I select one value then it works but if I try to select
    more than one then it gives me a query fetch error. I tried using :patientId
    and qry.bindValue(":patientId",patientId) method and it also didnt
    work. I can post my code for you if youd like but let me know what you think
    first.

    Here is my SQLite schema:

    create table patientTable(patientId INT NOT NULL, fname varchar(30) NOT NULL,
    midde varchar(30), lname varchar(30) NOT NULL, suffix varchar(15), dob
    varchar(10) NOT NULL, gender varchar(20) NOT NULL, physician varchar(40) NOT
    NULL, description varchar(50), anatomy varchar(20) NOT NULL, accession int
    NOT NULL, datetime varchar(20) NOT NULL, primary key(patientId));

    I also tried out a second one to make sure it wasnt the INT that was causing
    the error as shown:

    create table patientTable(patientId varchar(20) NOT NULL, fname varchar(30)
    NOT NULL, midde varchar(30), lname varchar(30) NOT NULL, suffix varchar(15),
    dob varchar(10) NOT NULL, gender varchar(20) NOT NULL, physician varchar(40)
    NOT NULL, description varchar(50), anatomy varchar(20) NOT NULL, accession
    varchar(20) NOT NULL, date varchar(12) NOT NULL, time varchar(12) NOT NULL,
    primary key(patientId));

    I can post the .h file and .cpp if needed.

    the_T 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Yes, sharing your code would be a good idea. It will allow us to better understand what you are trying to do.

      By the way, did you took a look a the request generated by QSqlQuery since it's failing ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Danielpopo said:

        y.prepare("update patientTable set patientId='"+patientId+"',
        fname='"+fname+"',middle='"+middle+"',lname='"+lname+xxxxxxx

        Just a note:
        If any of the variables is not a QString, then please wrap
        QString::number(var) around it as str+ int + str , do not always do
        as you might expect.

        deleted511D 1 Reply Last reply
        6
        • deleted511D deleted511

          Good afternoon, I am having a problem with selecting and updating queries in sqlite.
          Basically I use the select and update commands in my sqlite command shell and it gives me
          the correct output but if I use the same statement in my QT creator it gives
          me an error about unable to fetch query row.
          The select and update function only works if I have one string picked out
          rather than a whole bunch. Let me show you.
          qry.prepare("update patientTable set lname='"+lname+"'where
          patientId='"+patientId+"'"); works and updates but when I try:

          qry.prepare("update patientTable set patientId='"+patientId+"',
          fname='"+fname+"',middle='"+middle+"',lname='"+lname+
          "',suffix='"+suffix+"',dob='"+dob+"',gender='"+g
          ender+"',physician='"+physician+"',description='"+descript
          ion+"',anatomy='"+anatomy+"',accession='"+accession+"
          ',datetime='"+datetime+"'where
          patientId='"+patientId+"'");
          it gives me an error about query fetch row. I then thought it would be an database error but then put the same
          command into my sqlite and it updated the corresponding values with both
          commands shown above. I am also having the same problem with my select
          statement where if I select one value then it works but if I try to select
          more than one then it gives me a query fetch error. I tried using :patientId
          and qry.bindValue(":patientId",patientId) method and it also didnt
          work. I can post my code for you if youd like but let me know what you think
          first.

          Here is my SQLite schema:

          create table patientTable(patientId INT NOT NULL, fname varchar(30) NOT NULL,
          midde varchar(30), lname varchar(30) NOT NULL, suffix varchar(15), dob
          varchar(10) NOT NULL, gender varchar(20) NOT NULL, physician varchar(40) NOT
          NULL, description varchar(50), anatomy varchar(20) NOT NULL, accession int
          NOT NULL, datetime varchar(20) NOT NULL, primary key(patientId));

          I also tried out a second one to make sure it wasnt the INT that was causing
          the error as shown:

          create table patientTable(patientId varchar(20) NOT NULL, fname varchar(30)
          NOT NULL, midde varchar(30), lname varchar(30) NOT NULL, suffix varchar(15),
          dob varchar(10) NOT NULL, gender varchar(20) NOT NULL, physician varchar(40)
          NOT NULL, description varchar(50), anatomy varchar(20) NOT NULL, accession
          varchar(20) NOT NULL, date varchar(12) NOT NULL, time varchar(12) NOT NULL,
          primary key(patientId));

          I can post the .h file and .cpp if needed.

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @Danielpopo

          Why not use QSqlQuery::prepare with named parameters in your query you can then bind the values to the parameters with QSqlQuery::bindValue, see prepare and bindValue example.

          Just another note: if you post code here please put it between ` ` or ``` ``` so it will be more readable :)

          -- No support in PM --

          1 Reply Last reply
          0
          • mrjjM mrjj

            @Danielpopo said:

            y.prepare("update patientTable set patientId='"+patientId+"',
            fname='"+fname+"',middle='"+middle+"',lname='"+lname+xxxxxxx

            Just a note:
            If any of the variables is not a QString, then please wrap
            QString::number(var) around it as str+ int + str , do not always do
            as you might expect.

            deleted511D Offline
            deleted511D Offline
            deleted511
            wrote on last edited by
            #5

            @mrjj Thanks for the response,

            I solved my problem it was a misspelling in my schema with my sqlite. But what do you mean about wrapping it with QString::number, because I compiled it with a string and it worked but I can see what you mean that I am inputting a string to an integer slot.

            mrjjM 1 Reply Last reply
            0
            • deleted511D deleted511

              @mrjj Thanks for the response,

              I solved my problem it was a misspelling in my schema with my sqlite. But what do you mean about wrapping it with QString::number, because I compiled it with a string and it worked but I can see what you mean that I am inputting a string to an integer slot.

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

              @Danielpopo
              Hi
              super.
              What i meant was that sometimes
              integers do not convert to strings as expected with +

              int number=888;
              QString a="a";
              QString b="b";
              QString result=a+number+b;
              qDebug() <<"-------" << result;

              expected a888b
              got axb

              so i was not sure if all was strings in your statement. Since it work, you didnt have such case :)

              1 Reply Last reply
              3

              • Login

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