Results 1 to 20 of 57

Thread: Record update windowd entered data saving

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    Does insertRecord() return true?
    Last edited by wysota; 10th January 2006 at 10:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    Yes, I've tried it several times. submitAll also returns true, there is no visible error (in debugger), just empy field is added to QSqlRelationDataModel (not to databse, becasue if I reopen window, the empty field dissapears). I thought it was something wrong with relations, so I commented relation setup, still same result. Do you need whole code listing?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    Try this:

    Qt Code:
    1. rec.append(QSqlField("id", QVariant::Int));
    2. rec.append(QSqlField("name", QVariant::String));
    3. rec.append(QSqlField("address", QVariant::String));
    4. rec.setValue("id", lineedit1->text());
    5. rec.setValue("name", lineedit2->text());
    6. rec.setValue("address", lineedit3->text());
    7. //...
    8. model->insertRecord(-1, rec);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    God damn, it works, but still there is no data in the table after reopening the window! Is this problem maybe connected with my databse schematic, because I have an ID field in customer table autonumbered, but I cannot get its value from SQL to Qt?
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    Quote Originally Posted by wysota
    Try this:

    Qt Code:
    1. rec.append(QSqlField("id", QVariant::Int));
    2. rec.append(QSqlField("name", QVariant::String));
    3. rec.append(QSqlField("address", QVariant::String));
    To copy to clipboard, switch view to plain text mode 
    I just wonder why didn't we thought about this earlier

    A shorter version:
    Qt Code:
    1. QSqlRecord rec( m_pTableModel->record() );
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    Quote Originally Posted by jacek
    I just wonder why didn't we thought about this earlier

    A shorter version:
    Qt Code:
    1. QSqlRecord rec( m_pTableModel->record() );
    To copy to clipboard, switch view to plain text mode 
    But isn't there a QSqlDatabase connection or some other mechanism, which fetches fields names automaticly?
    Qt 5.3 Opensource & Creator 3.1.2

  7. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    Quote Originally Posted by MarkoSan
    But isn't there a QSqlDatabase connection or some other mechanism, which fetches fields names automaticly?
    Now it works, thank you very very much! But can you please tell me how to fetch ID of CUSTOMER, which is set to AUTONUMBER in SQL Administrator, but it does not work, since If I add record manualy, the ID does not grow!!
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    Quote Originally Posted by MarkoSan
    But isn't there a QSqlDatabase connection or some other mechanism, which fetches fields names automaticly?
    That's what QSqlTableModel::record() does.

    Quote Originally Posted by MarkoSan
    can you please tell me how to fetch ID of CUSTOMER, which is set to AUTONUMBER in SQL Administrator, but it does not work, since If I add record manualy, the ID does not grow!!
    Try:
    Qt Code:
    1. rec.setGenerated( "id", false );
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    Ok, I have another question: Why do I get now table filled with new data correctly, but If i reopen windows, the data dissapears, that means, record is still not being written to database?
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    Did you try QSqlTableModel::submit() or QSqlTableModel::submitAll()?

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    I checked it out, just wait, now returns false, I'll post the error soon.
    Qt 5.3 Opensource & Creator 3.1.2

  12. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    Quote Originally Posted by MarkoSan
    I checked it out, just wait, now returns false, I'll post the error soon.
    submitAll returns false, further diagnostic:
    Qt Code:
    1. QSqlError::type returned StatementError
    2. QSqlQueryModel::query returned {...}
    3. QSqlQuery::lastQuery returned {"SELECT stranka.SIFRA,stranka.DS,stranka.PRIIMEK,stranka.IME,ulica.naziv,stranka.STEVILKA_ULICE,kraj.naziv_kraja FROM stranka,u" size=217}
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    Quote Originally Posted by MarkoSan
    Ok, I have another question: Why do I get now table filled with new data correctly, but If i reopen windows, the data dissapears, that means, record is still not being written to database?
    Is it not written to the database or not displayed in the tableview?


    Oh, and guys, try not to use UPPERCASE column names in SQL tables, it's a risk while porting between platforms.

  14. #14
    Join Date
    Jan 2006
    Location
    Iasi, Romania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    Thanks.
    1. I have tried
    Qt Code:
    1. rec.setValue("city", "London");
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. rec.setValue("city", 1) ;
    To copy to clipboard, switch view to plain text mode 
    with the same result -- record added to database but 'city' field value == 0.

    2 - 3. I'll go for them later, I do not have Qt4 right now.

    Is my problem similar to MarkoSan's or am I wrong?

  15. #15
    Join Date
    Jan 2006
    Location
    Iasi, Romania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    I have done some more "research" and this is what I have:
    rec.field("city").type() returns QVariant::Invalid - that's because rec does not contain a field named "city".
    rec.field("city_name").type() returns QVariant::String which is correct as long the database field city_name is a VARCHAR.

    Using rec.setValue("city", "London") apparently works because QSqlRecord::setValue(const QString & name, const QVariant & val) does nothing if the field ("city" in this case) does not exist. So the record is inserted in the database using the default value (0) for the field "city_name" which is contained by the record rec. In this case why rec.setValue("city_name", "London") doesn't work and model->insertRecord(-1, rec) returns FALSE?
    I have also found something that looks strange, at least for me. I run in MySQL QueryBrowser the query upon which the QSqlRelationalTableModel model is filled with data and a saw that I can't insert or update records in the resultset returned.

    I think I have to dig deeper into this model/view architecture. For now I will try a different approach and use a QSqlQuery("INSERT INTO ...") to make things going on, but the topic still remains opened.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    You might want to ask Trolltech about it, it might be some kind of bug or lack of documentation.

  17. #17
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Record update windowd entered data saving

    I know why submitAll is not working for me. I've set up foreign keys, but I had not done the JOIN statement, I'll try it now and report the result.
    Qt 5.3 Opensource & Creator 3.1.2

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    Quote Originally Posted by jacek
    I just wonder why didn't we thought about this earlier
    Because we didn't read QSqlRecord docs carefully enough (I didn't read it at all, just looked at it).

  19. #19
    Join Date
    Jan 2006
    Location
    Iasi, Romania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Record update windowd entered data saving

    I have been watching this thread since its beginnings because I am stuck with the same problem. Look at this scenario:
    Two database tables

    CUSTOMERS
    customer_id INTEGER AUTOINCREMENT NOT NULL
    name VARCHAR
    city INTEGER NOT NULL DEFAULT 0

    CITIES
    city_id INTEGER AUTOINCREMENT NOT NULL
    city_name VARCHAR

    A model:
    Qt Code:
    1. model = new QSqlRelationalTableModel(this);
    2. model->setTable("customers");
    3. model->setRelation(2, QSqlRelation("cities", "city_id", "city_name"));
    4. model->select();
    To copy to clipboard, switch view to plain text mode 

    "this" beeing the form on which there is a QTableView:

    Qt Code:
    1. ui.tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    A QPushButton insertButton and a custom slot:

    Qt Code:
    1. void MainWindow::on_insertButton_clicked(bool checked)
    2. {
    3. QSqlRecord rec = model->record();
    4.  
    5. rec.setValue("name", "Test Customer");
    6. rec.setValue("city_name", "London");
    7.  
    8. bool insert = model->insertRecord(-1, rec);
    9. bool submit = model->submit();
    10. }
    To copy to clipboard, switch view to plain text mode 

    The select statement upon which the model is created is like this:
    "SELECT customers.customer_id, customers.name, cities.city_name
    FROM customers, cities
    WHERE customers.city = cities.city_id"
    This is the return of model->query().lastQuery().

    The QSqlRecord rec from above have the same fields: customer_id, name, city_name, which is obvious.
    BUT if you run the code like this both insertRecord() and submit() return FALSE and the last error reported by model->lastError().text() is:
    "Unknown column 'city_name' in 'field list' QMYSQL3 : QMYSQLResult". Surprisingly in the tableView there is a new row which contain the values used by the setRecord() calls.
    You get the same result if you use a numerical value with rec.setValue() in line 6:
    Qt Code:
    1. rec.setValue("city_name", 1);
    To copy to clipboard, switch view to plain text mode 

    QSqlTableModel::insertRecord() calls internally QSqlTableModel::insertRows() and QSqlTableModel::setRecord(). The new row is shown in the tableView and that means that only insertRows() worked but not setRecord().

    Now change this line (6) to be like this:
    Qt Code:
    1. rec.setValue("city", 1);
    To copy to clipboard, switch view to plain text mode 
    Both insertRecord() and submit() return TRUE, the record is added in the database (I have checked with MySQL Query Browser) but the field "city" is set to 0, which is the default value for the field and model->lastError().text() is empty. The record isn't shown in the tableView because there is no corresponding record in the related table "cities" to match the WHERE clause 'customers.city = cities.city_id'.

    So the first way is not good (it generates SQL errors and no record added) the second one seems also not to be working (no SQL error, record added, but with a useless value in the field upon the relation is set). Until now I couldn't narrow it more than this. Which way??

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Record update windowd entered data saving

    The behaviour is (probably) because you can't insert into two tables with a single insert statement. Using this model you can only insert into the table which you set to be the model table ("customers" in your case). That's why you have to reference "city" and not "city_name".

    Now we come to the second part -- the Problem(R).

    I would try two things:

    1.
    Qt Code:
    1. rec.setValue("city", "London");
    To copy to clipboard, switch view to plain text mode 
    2.
    Qt Code:
    1. rec.setGenerated("city", true);
    To copy to clipboard, switch view to plain text mode 

    A third thing to do could be to check what is the type of QVariant associated with "city" field (using rec.field().type()).

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.