Results 1 to 5 of 5

Thread: MYSQL insert

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default MYSQL insert

    I am wanting to insert into my database STL vector data types. My first question is: Is this possible??

    Test code looks like this:

    bool createConnection()
    {
    sVector3<double> pos(0.0,0.0,0.0);
    sVector3<double> orient(0.0,0.0,0.0);
    sVector3<double> vel(0.0,0.0,0.0);
    ...
    QSqlTableModel * model = new QSqlTableModel;
    model->setTable("Missile1");

    QSqlRecord dataRecord;

    for(int i=0; i <= 180; ++i)
    {
    //made up values
    pos[0] = i+100;
    pos[1] = i+10;
    pos[2] = i;
    dataRecord.setValue(QString("Position"),pos);

    bool insert = model->insertRecord(-1,dataRecord);
    bool submit = model->submit();
    }
    }


    The dataRecord.setValue(QString("Position"),pos); statement is the one I know is wrong but don't know how to fix it. I eventually will have a combination of several vectors like this and scalars that I will be placing into this table at a high rate.

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MYSQL insert

    you can't save STL vectors directly using the setValue-method, as it accepts only QVairant-values.
    what mysql-type has the Position-field?
    if it is a string you have to convert the vectors into a QString.
    afaik there is no vector-field-type in mysql...

    niko

  3. #3
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: MYSQL insert

    Ok, thanks.

    I have "fixed" my error. Next question, is there a better/more efficient method of placing this data in (i.e. 1) insertRow() ,2) setData() 3) submitAll())?? Code below.

    Also, is there a maximum number of columns?? I see where there is a max limit on the length of the column but not on the number of columns.

    Qt Code:
    1. bool createConnection()
    2. {
    3. sVector3<double> pos(0.0,0.0,0.0);
    4. sVector3<double> orient(0.0,0.0,0.0);
    5. sVector3<double> vel(0.0,0.0,0.0);
    6.  
    7. ...
    8. QSqlQuery query;
    9.  
    10. //Delete old table if it exists, if not no harm no foul
    11. query.exec("drop table Missile1");
    12. query.exec("create table Missile1 ( "
    13. "PositionX double, PositionY double, PositionZ double,"
    14. "OrientationX double, OrientationY double, OrientationZ double,"
    15. "VelocityX double, VelocityY double, VelocityZ double)");
    16.  
    17. model->setTable("Missile1");
    18.  
    19. QSqlRecord dataRecord = model->record();
    20.  
    21. int row = 0;
    22. for(int i=0; i <= 150; ++i)
    23. {
    24. //made up values
    25. pos[0] = i+100;
    26. pos[1] = i+10;
    27. pos[2] = i;
    28. orient[0] = i+50;
    29. orient[1] = i+5;
    30. orient[2] = i;
    31. vel[0] += 25.123;
    32. vel[1] += 2.456;
    33. vel[2] += 0.7890123;
    34.  
    35. row = model->rowCount();
    36. model->insertRow(row);
    37. model->setData(model->index(row,0),pos[0]);
    38. model->setData(model->index(row,1),pos[1]);
    39. model->setData(model->index(row,2),pos[2]);
    40. model->setData(model->index(row,3),orient[0]);
    41. model->setData(model->index(row,4),orient[1]);
    42. model->setData(model->index(row,5),orient[2]);
    43. model->setData(model->index(row,6),vel[0]);
    44. model->setData(model->index(row,7),vel[1]);
    45. model->setData(model->index(row,8),vel[2]);
    46.  
    47. bool submit = model->submitAll();
    48.  
    49. if(!submit)
    50. cerr << "Record not submited " << endl;
    51. }
    52. ...
    53. return true;
    54. }
    To copy to clipboard, switch view to plain text mode 
    Thanks.

  4. #4
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MYSQL insert

    Also, is there a maximum number of columns?? I see where there is a max limit on the length of the column but not on the number of columns.
    i guess the only limitation is by MySQL - and here it is normally the datatype of the primary key. if the primary key is an INT the limit will be at 2.147.483.647 rows - but in your case you don't have a primary key anyway (i would suggest you add one...)

    QSqlTAbleModel is maily useful to provide a data-class for eg. QTableView. In your case you could use QSqlQuery to execute the INSERT-query.
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO person (id, forename, surname) "
    3. "VALUES (:id, :forename, :surname)");
    4. query.bindValue(":id", 1001);
    5. query.bindValue(":forename", "Bart");
    6. query.bindValue(":surname", "Simpson");
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 

    niko

  5. #5
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: MYSQL insert

    Thanks again, niko. Using your technique for insertion is MUCH MUCH faster!! To put this issue to bed, here is what I ended up doing:

    Qt Code:
    1. bool createConnection()
    2. {
    3. sVector3<double> pos(0.0,0.0,0.0);
    4. sVector3<double> orient(0.0,0.0,0.0);
    5. sVector3<double> vel(0.0,0.0,0.0);
    6. double time(0.0),sltRngToGo(0.0),fuel(0.0),seekerEl(0.0),seekerAz(0.0);
    7. ...
    8. //Set up MySQL template query for insertion
    9. QSqlQuery query;
    10. //Delete old table if it exists, if not no harm no foul
    11. query.exec("drop table Missile2");
    12. query.exec("create table Missile2 ( "
    13. "Time double,"
    14. "PositionX double, PositionY double, PositionZ double,"
    15. "OrientationX double, OrientationY double, OrientationZ double,"
    16. "VelocityX double, VelocityY double, VelocityZ double,"
    17. "SltRngToGo double, Fuel double, SeekerAcq smallint, SeekerEl double,"
    18. "SeekerAz double)"
    19. "ENGINE = MyISAM ");
    20.  
    21. query.prepare("insert into missile2 (Time,PositionX, PositionY, PositionZ,"
    22. "OrientationX, OrientationY, OrientationZ,"
    23. "VelocityX, VelocityY, VelocityZ, SltRngToGo, Fuel, "
    24. "SeekerAcq, SeekerEl, SeekerAz)"
    25. "Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    26.  
    27. for(int i=0; i <= 10000; ++i)
    28. {
    29. //made up values
    30. time = i;
    31. pos[0] = i+100;
    32. pos[1] = i+10;
    33. pos[2] = i;
    34. orient[0] = i+50;
    35. orient[1] = i+5;
    36. orient[2] = i;
    37. vel[0] += 25.123;
    38. vel[1] += 2.456;
    39. vel[2] += 0.7890123;
    40. sltRngToGo = sltRngToGo+1.2345;
    41. fuel = fuel+0.567;
    42. seekerAcq = true;
    43. seekerEl = 2.2;
    44. seekerAz = 3.45;
    45.  
    46. query.addBindValue(time);
    47. query.addBindValue(pos[0]);
    48. query.addBindValue(pos[1]);
    49. query.addBindValue(pos[2]);
    50. query.addBindValue(orient[0]);
    51. query.addBindValue(orient[1]);
    52. query.addBindValue(orient[2]);
    53. query.addBindValue(vel[0]);
    54. query.addBindValue(vel[1]);
    55. query.addBindValue(vel[2]);
    56. query.addBindValue(sltRngToGo);
    57. query.addBindValue(fuel);
    58. query.addBindValue(seekerAcq);
    59. query.addBindValue(seekerEl);
    60. query.addBindValue(seekerAz);
    61. query.exec();
    62. }
    63. return true;
    64. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [Qt4.1] How to insert an image inside a Form?
    By Gonzalez in forum Qt Tools
    Replies: 5
    Last Post: 23rd September 2008, 11:20
  2. connecting to MySQL through it's API
    By Philip_Anselmo in forum Installation and Deployment
    Replies: 20
    Last Post: 30th May 2006, 23:22
  3. issues compiling Qt4.1 with mysql
    By patcito in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:41
  4. Qt 4.1 OS on Windows & mysql
    By neeko in forum Installation and Deployment
    Replies: 10
    Last Post: 31st January 2006, 20:22
  5. insert a widget into a group "on-the-fly"
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 15th January 2006, 00:29

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.