Results 1 to 20 of 20

Thread: QStandardItemModel to sqlite database table

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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 

Similar Threads

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