Results 1 to 16 of 16

Thread: Qt to Sqlite data insertion doubt.

  1. #1
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Qt to Sqlite data insertion doubt.

    Hello every one.

    i have a doubt , how can to insert data which is in a QList<QString> into a database.
    i have created the table and the columns for it in connection.h file.. i need to insert the continuous data in the string for 500 columns with a time stamp in the end.. but it is not working what i am doing wrong here. pls help me out with it.

    Qt Code:
    1. for(int p = 0; p < rdata.count();p++) // rdata is QList<QString>
    2. {
    3. lstdata = rdata.at(p); // lstdata is a QString
    4. }
    5. QString sp("INSERT INTO thdata values (%1)");
    6. sp = sp.arg(datacount)
    7. .arg("'" + lstdata + "'");
    8. m_query.exec(sp);
    9. datacount++;
    10. lstdata.clear();
    To copy to clipboard, switch view to plain text mode 

    thank you

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    Qt Code:
    1. for(int p = 0; p < rdata.count();p++) // rdata is QList<QString>
    2. {
    3. lstdata = rdata.at(p); // lstdata is a QString
    4. }
    To copy to clipboard, switch view to plain text mode 
    This code does nothing more than assign values from rdata list into lstdata. In the end you are inserting only the last item from list. You have to put the database insertion code into the loop as well

    Also use the QSqlQuery::lastError() method after executing query to check status.

  3. #3
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    hello thanks for the reply, i am actually calling this function every sec using a QTimer.
    this is what i have done in connection.h to create 500 columns for the table "thdata" i just need to insert the continuous data which is present in lstdata into these columns.
    Qt Code:
    1. QString dbtot("create table if not exists thdata(Time_InterVal INTEGER, Thermo varchar(250), Thermo varchar(250),Thermo varchar(250),Thermo4 varchar(250),Thermo5 varchar(250),Thermo6 varchar(250) . . . . . . Thermo500 varchar(250)
    2. query.exec(dbtot);
    To copy to clipboard, switch view to plain text mode 

    this is how i am trying to insert the data base into the sqlite table columns now, but the data is not been inserted into columns.
    Qt Code:
    1. for(int p = 0; p < rdata.count();p++)
    2. {
    3. lstdata = rawdata.at(p);
    4. if(lstdata.count()!=0)
    5. {
    6. QString sp("INSERT INTO thermodata values (%1)");
    7. sp = sp.arg(datacount)
    8. .arg("'" + lstdata + "'");
    9. m_query.exec(sp);
    10. datacount++;
    11. }
    12. }
    13. listdata.clear();
    To copy to clipboard, switch view to plain text mode 
    i did put it in the loop but i have no data in the columns. ..
    thank you sir
    Last edited by rex; 14th December 2010 at 09:19.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    Qt Code:
    1. QString dbtot("create table if not exists thdata(Time_InterVal INTEGER, Thermo varchar(250), Thermo varchar(250),Thermo varchar(250),Thermo4 varchar(250),Thermo5 varchar(250),Thermo6 varchar(250) . . . . . . Thermo500 varchar(250)
    To copy to clipboard, switch view to plain text mode 
    Did you hand-coded all of this ? You could create statement like this using a loop.
    Qt Code:
    1. QString thermoDbData;
    2. for( int i=0 ; i<500 ; ++i ){
    3. thermoDbData += QString("Thermo%1 varchar(250) ").arg(i) + (i==499 ? "" : ",");
    4. }
    5. QString dbtot = QString("create table if not exists thdata(Time_InterVal INTEGER, %1 );").arg(thermoDbData);
    To copy to clipboard, switch view to plain text mode 

    Now to insert a data into 500 columns you need to create an insertion statement that looks like
    Qt Code:
    1. insert into thdata values(/*and here goes your string: value1, value2, ..., value500*/)
    To copy to clipboard, switch view to plain text mode 

    Just use a loop to join all the values from list into one string ( or use QStringList::join() method ) and then insert it into database.

  5. The following user says thank you to stampede for this useful post:

    rex (14th December 2010)

  6. #5
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    thanks a lot for the help sir. Yes i actually hand- coded every thing to create 500 columns. i app the code you gave in the main program and it is working fine all the columns are created. I am actually taking four char's from a QString and appending them to a List that is on channels data , tat is the reason i stored all the values in a QList<QString>
    Qt Code:
    1. for(int k=0; k<newdat.size(); k+=4)
    2. {
    3. nudata.append(newdat.mid(k, 4)); // QList<QString> nudata;
    4. }
    5. //timest = QTime::currentTime().toString("hh:mm:ss");
    6. for(int p = 0; p < nudata.count();p++)
    7. {
    8. lstdata = nudata.at(p); // QString lstdata;
    9. }
    10. if(lstdata.count()!=0)
    11. {
    12. //QMessageBox::information(this, "Test","Loop Position reached");
    13. QString sq("insert into thdata values(lstdata)");
    14. m_query.exec(sq);
    15. }
    To copy to clipboard, switch view to plain text mode 
    but i still don't have the data in the columns. .. is this syntax right. I am calling the function every 2 sec's with a timer.

    thanks you.

  7. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    but i still don't have the data in the columns. .. is this syntax right.
    Try to debug, print the value of "lstdata" variable before creating query, see what QSqlQuerry::lastError() says after executing the query ect...

    Qt Code:
    1. QString sq("insert into thdata values(lstdata)");
    To copy to clipboard, switch view to plain text mode 
    This code will try to insert a string value "lstdata" rather than the value of "lstdata" variable. Try:
    Qt Code:
    1. QString sq = QString("insert into thdata values(%1);").arg(lstdata);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    Hello sir thanks for your time and help, This is the function i am calling every 2 seconds. The data is present in the QString just before the data insertion but its not saved into the data base.

    Qt Code:
    1. void datareceiver::aligndata()
    2. {
    3. for(int k=0; k<newdat.size(); k+=4)
    4. {
    5. nudata.append(newdat.mid(k, 4));
    6. }
    7.  
    8. for(int p = 0; p < nudata.count();p++)
    9. {
    10. lstdata = nudata.at(p);
    11. }
    12. if(lstdata!=0)
    13. {
    14. textBrowser_2->append(lstdata); \\ The data is displayed in the text browser but not in the data base.
    15. QString sq = QString("insert into thdata values(%1);").arg(lstdata);
    16. m_query.exec(sq);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    the data is present in the String but its not updated into the db. what do you think can be going wrong.

    thank you
    Last edited by rex; 14th December 2010 at 11:51.

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    Hello sir thanks for your time and help
    no problem

    textBrowser_2->append(lstdata); \\ The data is displayed in the text browser but not in the data base.
    And does the data looks like you expect ?
    You have created your table as
    Qt Code:
    1. thdata(Time_InterVal INTEGER, /* ... and 500 varchars*/ )
    To copy to clipboard, switch view to plain text mode 
    so in order to insert data into it you need to provide value for each column in your statement. Valid statements for this table looks like this:
    Qt Code:
    1. insert into thdata values(x,'str1','str2',...,'str500');
    To copy to clipboard, switch view to plain text mode 
    where x is an integer value, and str1, ..., str500 are "varchars" (strings).
    Note that each string is wrapped around the extra quotes.

    So the value of "lstdata" should look like
    Qt Code:
    1. x,'str1','str2',...,'str500'
    To copy to clipboard, switch view to plain text mode 
    in order to give you a valid statement.

    I'm gonna repeat myself, please check the QSqlQuerry::lastError(), this could really give you a clue about what you are doing wrong.

  10. #9
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qt to Sqlite data insertion doubt.

    Hey!

    Generally we follow this process,

    1) prepare a query
    2) Exec a query
    3) commit it to the database

    Are you doing everything ?

  11. #10
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    hello, yes this is what i am doing right now to insert where lstdata is QString with all the data i need to insert into the columns. but it is not working , i mean the data is not updated. I am calling this function every 2 seconds.

    Qt Code:
    1. QString sq = QString("insert into thdata values(%1);").arg(lstdata);
    2. m_query.exec(sq);
    To copy to clipboard, switch view to plain text mode 


    Added after 10 minutes:


    Hello stampede sir , Yes i am getting all the data as i expected in textBrowser.. but its not updated into the data base. . what do i have to do to insert all the data stored in a String
    id on't think this approch will work for me sir cause, i am receiving data from rs 232 terminal after aligning the data the way i wanted i am finally appending tat list into the QString lstdata; each string in the list is of 4 char's. .. I just need to insert data from the QString from first position to 500th and again start from 1st column and go on till 500th column with the time stamp.
    insert into thdata values(x,'str1','str2',...,'str500');
    since the data is appended into the string continiously , i have to insert every consequtive string into the columns from 1 to 500 and come back to 1st column and insert to 500 and so on with the time stamp. ..?! pls help me out with this problem..
    I did read about lastError() but i am using Qt integrated with Visual Studio IDE so i am not able to use QDebug here.
    thanks a lot.
    Last edited by rex; 14th December 2010 at 12:47.

  12. #11
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Qt to Sqlite data insertion doubt.

    Quote Originally Posted by rex View Post
    hello, yes this is what i am doing right now to insert where lstdata is QString with all the data i need to insert into the columns. but it is not working , i mean the data is not updated. I am calling this function every 2 seconds.

    Qt Code:
    1. QString sq = QString("insert into thdata values(%1);").arg(lstdata);
    To copy to clipboard, switch view to plain text mode 
    .
    I do not see commit statement after your exec. And also prepare statement is also missing, which ideally tells whether your query is valid or not?

    I did read about lastError() but i am using Qt integrated with Visual Studio IDE so i am not able to use QDebug here.
    Include the header QDebug, you can able to use. It does not matter which IDE you are using. To put it simple. Get lastError() inside the string and check its value. I assume that you are opening the database before calling all these statements.

  13. #12
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    Hello this is what i am doing to insert the data in QString temperaturedata; into the sqlite db which has 500 columns..
    Qt Code:
    1. if(temperaturedata.count()!=0)
    2. {
    3. QString sq = QString("insert into thdata values(%1);").arg("'"+ temperaturedata +"'");
    4. m_query.exec(sq);
    5. // qDebug("lastError()");
    6. temperaturedata.clear();
    7. if(!m_query.exec(sq))
    8. {
    9. qDebug() << m_query.lastError().text();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    i used the lastError() method to find whats wrong with the query.. and this is message i got in debug, but not able to figure out what is wrong.
    Qt Code:
    1. lastError()
    2. "table thdata has 500 columns but 1 values were supplied Unable to execute statement"
    To copy to clipboard, switch view to plain text mode 
    pls help me out with this problem.

    thank you
    Last edited by rex; 15th December 2010 at 12:06.

  14. #13
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    "table thdata has 500 columns but 1 values were supplied Unable to execute statement"
    Your query statement is wrong.
    Qt Code:
    1. QString("insert into thdata values(%1);").arg("'"+ temperaturedata +"'");
    To copy to clipboard, switch view to plain text mode 
    You need to remove the extra quotes from around the "temperatureData", this is causing the error, because the value like this
    Qt Code:
    1. 'tempData'
    To copy to clipboard, switch view to plain text mode 
    is just one string. As I've posted before, string that goes into arg() must be like this
    x,'str1','str2',...,'str500'
    Can you give us an example of how "temperatureData" looks like ? But not how you want it to look like, just print the value of the string from your program and show us.

  15. #14
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    Hello sir thanks a lot for your time and patience in helping me out with my problem, This is where i am appending strings in a list to a QString which i am converting to hex and appending to temperatureData String. This is the data which is exactly been showed in the textBrowser at that instance the data which i received from RS232 terminal was displayed like this.
    every string is displayed in a new line in the textBrowser.
    497
    497
    497
    497
    497
    497
    497
    497
    497
    497
    497
    525
    525
    525
    it is a part of the simulated data which i am receiving.

    This is the code where i am appending the data from the list to the string.
    Qt Code:
    1. for(int p = 0; p < rawdata.count();p++)
    2. {
    3. listdata = rawdata.at(p); // QList<QString> rawdata;
    4. }
    5. for(int j=0; j< listdata.size();j++) // QString listdata, str;
    6. {
    7. temperatureData = QString::number(listdata.toInt(0,16));
    8. }textBrowser_2->append(temperatureData);
    To copy to clipboard, switch view to plain text mode 

    sir i removed the extra quotes also from the code i compiled and checked it again but it does't fill any data to the columns at all.
    Qt Code:
    1. QString sq = QString("insert into thdata values(%1);").arg(temperaturedata );
    2. m_query.exec(sq);
    3. temperaturedata .clear();
    To copy to clipboard, switch view to plain text mode 
    i have attached the complete .cpp file sir the function where i am trying to insert data into db is aligndata().
    thank you
    Attached Files Attached Files

  16. #15
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt to Sqlite data insertion doubt.

    Ok, but what I meant was something like:
    Qt Code:
    1. if(temperaturedata.count()!=0)
    2. {
    3. qDebug() << temperaturedata; //!< print the value and see if its ok
    4. QString sq = QString("insert into thdata values(%1);").arg(temperaturedata);
    5. m_query.exec(sq);
    6. temperaturedata.clear();
    7. }
    To copy to clipboard, switch view to plain text mode 
    I see that you've displayed strings before processing them into query statement.
    Print the data just before inserting it into database.

  17. #16
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: Qt to Sqlite data insertion doubt.

    hello sir i checked using the debug the data is present it shows in the debug window.. The right data is present in the QString temperaturedata; but its not getting inserted into the data base.

    "10"
    "10"
    "10"
    "10"
    "10"
    "10"
    The thread 'Win32 Thread' (0xef0) has exited with code 0 (0x0).
    The thread 'Win32 Thread' (0x848) has exited with code 0 (0x0).
    and i have two textBrowser in the program to check the incoming data and the processed data which is supposed to go into the db..
    No data at all is updated not even a single row..

Similar Threads

  1. Replies: 13
    Last Post: 6th December 2010, 04:41
  2. Replies: 0
    Last Post: 21st April 2010, 16:25
  3. Sqlite and UTF8 data
    By kroenecker in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2009, 14:49
  4. Replies: 0
    Last Post: 4th December 2008, 05:48
  5. data not being retained in sqlite DB
    By sticcino in forum Qt Programming
    Replies: 2
    Last Post: 2nd July 2008, 10:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.