Results 1 to 20 of 20

Thread: QStandardItemModel to sqlite database table

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QStandardItemModel to sqlite database table

    hi everyone,
    I am trying to insert the data present in a QStandardItemModel.I have to set that data into a table present in sqlite database.I tried like below

    Qt Code:
    1. QSqlTableModel table(0,db_local);
    2. table.setTable("menu_list");
    3. table.setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. for(i=0;i<model->rowCount();i++)
    5. {
    6. table.insertRow(i);
    7. for(j=0;j<model->columnCount();j++)
    8. ok= table.setData(table.index(i,j),model->data(model->index(i,j)));
    9. }
    To copy to clipboard, switch view to plain text mode 


    In QSqlTableModel the required value is present.
    But in database nothing is there.
    Is there any other statement required to update the table inside database?
    plz help me.

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    Get rid of line #3 in the above snippet.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    thanks for reply.
    if i remove the line 3 then table.insertRow(i) returns false.
    this things was working for QSqlQUeryModel.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    If it returns false then there has to be a reason for it. What exactly are you trying to do?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    I have a xml string whoch contain data of a table.I want to insert that data in a table.
    so first i inserted all data into QStandardItemModel .Now i have to insert the data into table.
    Is there any other way to do this?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    So why did you insert it to QStandardItemModel in the first place? Also does your sql table have a primary key defined?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    No there is no primary key.Is there any other way to extract data? this is the only way which i know.
    If there is any other easy plz suggest me.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    If there is no primary key defined then I don't see how you would want to insert rows into the database.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    I did not get you.Actually here primary key is not necessary.Is primary key necessary?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    If you wish to say "insert me a new row" and then "set data on row X" how should the database know which row is "X"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    ok then how i will do this.plz tel me.

    now i tried like below.but same thing.(means not working).

    Qt Code:
    1. for(i=0;i<model->rowCount();i++)
    2. {
    3. for(j=0;j<model->columnCount();j++)
    4. {
    5. QSqlField field;
    6. field.setValue(model->data(model->index(i,j)));
    7. field.setName(model->headerData(j,Qt::Horizontal).toString());;
    8. rec.append(field);
    9. }
    10. ok=table->insertRecord(i,rec);
    11. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    Your table needs to have a primary key defined.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    how i set one field to primary key.
    is there any function to set ?

  14. #14
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    I created menu_list table with a primary key.But it isnot working.
    Did you want to do this ?

  15. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStandardItemModel to sqlite database table

    Other reasons for inserts to fail include:
    • If you don't provide a value and the column in the table has a NOT NULL constraint and no default value.
    • Inserted values are not valid data for the column types e.g. insert word into integer field.
    • Inserted values violate check constraints, e.g. insert "X" into column accepting only "Y" or "N".
    • Inserted values violate unique index constraints, e.g. insert same value twice.
    • Inserted values violate foreign key constraints e.g. code column not in related code lookup table.
    We have no information that would help us narrow this list down.
    What is your actual table schema and what is a sample of the data you are trying to insert? Can you execute the same insert manually through the relevant database CLI?

    Wysota's question regarding why you used a QStandardItemModel was intended to make you wonder if there was another way. You have all the pieces. Your current process goes:
    • Read XML file
    • Insert into QStandardItemModel
    • User fiddles with the data (I assume this) through a view on the QStandardItemModel
    • Copy the QStandardItemModel into a QSqlTableModel


    Have you considered?
    • Read XML file
    • Insert into QSqlTableModel
    • User fiddles with the data (I assume this) through a view on the QSqlTableModel
    • No copying to do


    Other ways to get data into a table involve writing your own SQL INSERT queries, binding values and executing once per row (QSqlQuery).

  16. #16
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    hi ChrisW67,
    when i am trying to insert row in qsqltable model it returns false.If i will try with
    table.setEditStrategy(QSqlTableModel::OnManualSubm it);
    then there is no problem with insertion.But in database there is no change in table.

    is there any soln ?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QStandardItemModel to sqlite database table

    Quote Originally Posted by sattu View Post
    If i will try with
    table.setEditStrategy(QSqlTableModel::OnManualSubm it);
    then there is no problem with insertion.
    Because there is no insertion then until you submit the model to the database. Which will fail because you fail to acknowledge that your database schema is incorrect. It seems you do not have sufficient knowledge about relational databases, maybe it's time to learn a bit about them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStandardItemModel to sqlite database table

    Quote Originally Posted by sattu View Post
    is there any soln ?
    Yes. You need to look at what Qt's SQL components are going to do with the data you are providing, the actual data you are providing, and then work out why it isn't working. Wysota and I have already given you a list of possible causes and some things to look at that would help you work out your problem. Repeatedly coming back with "It doesn't work, fix it for me" responses and no specifics is not going to help.

    Here is a repeat hint: Ultimately your code causes an SQL INSERT with the data you provide. If you cannot manually perform the equivalent insert then there is no way that Qt will be able to either. Manually performing the equivalent insert will tell you what the SQL engine thinks is the problem. This activity has nothing to do with Qt-based code.

  19. #19
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QStandardItemModel to sqlite database table

    Thanks both for help me.
    Now it is working,Actually i did not mention the field type and field name .So i am not able to insert.

    Can any one told me is there any better way to extract data from xml string ?
    I am using QXmlStreamReader for reading data.For this i use a loop to read data.Is there any way by which i can directly convert the xml string to tablemodel or querymodel or any equivalent model ?
    thank.

  20. #20
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStandardItemModel to sqlite database table

    You could write a custom model wrapped around the XML document loaded into a QDomDocument. This is only going to work if the XML document is small enough to fit in RAM and will not be particularly efficient.

    However, since your requirement seems to be that the data ends up in a relational table then you might as well read the XML and insert it straight into the table. Then your users view and edit the data from the table.

Similar Threads

  1. Replies: 2
    Last Post: 4th August 2010, 14:06
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 13:22
  3. SQLITE ATTACH database
    By drescherjm in forum Qt Programming
    Replies: 8
    Last Post: 9th December 2009, 08:25
  4. Replies: 2
    Last Post: 7th December 2009, 15:15
  5. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 22:38

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.