Results 1 to 9 of 9

Thread: Database program exit when function called

  1. #1
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Database program exit when function called

    I have a program like this
    Qt Code:
    1. QList <S_AIS_DB_SHIP> cAISDataBase::addCurrentShipDataWithFilter(AIS_DB_FILTER filterType, QString searchKey)
    2. //void cAISDataBase::addCurrentShipDataWithFilter(AIS_DB_FILTER filterType,QString searchKey)
    3. {
    4. QList<S_AIS_DB_SHIP>aisList;
    5. QString type;
    6. if (filterType==ADF_MMSI)
    7. type=QString("mmsi");
    8. else if(filterType==ADF_AREAFILTER)
    9. type=QString("areaFilter");
    10. else if (filterType==ADF_STATUS)
    11. type=QString("status");
    12. else if (filterType==ADF_COG)
    13. type=QString("cog");
    14. else if (filterType==ADF_SOG)
    15. type=QString("sog");
    16. else if (filterType==ADF_IMO)
    17. type=QString("imo");
    18. else if (filterType==ADF_CALLSIGN)
    19. type=QString("callsign");
    20. else if (filterType==ADF_STERN)
    21. type=QString("dim_stern");
    22. else if (filterType==ADF_BOW)
    23. type=QString("dim_bow");
    24. else if (filterType==ADF_STARBOARD)
    25. type=QString("dim_starboard");
    26. else if (filterType==ADF_PORT)
    27. type=QString("dim_port");
    28. else if (filterType==ADF_WIDTH)
    29. type=QString("width");
    30. else if (filterType==ADF_LENGTH)
    31. type=QString("length");
    32. else if (filterType==ADF_NAME)
    33. type=QString("name");
    34.  
    35. cMYSQLDB *mysqldb=createDbInstance();
    36. if(mysqldb->open()){
    37. MYSQL_RES *result;
    38. MYSQL_ROW row;
    39. QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";
    40.  
    41. result=mysqldb->query(query);
    42. if (result){
    43. S_AIS_DB_SHIP dbShip;
    44. while ((row=mysql_fetch_row(result))!=NULL){
    45. S_AIS_DB_STATUSDATA statusDataTemp;
    46. statusDataTemp.mmsi = QString(row[0]);
    47. statusDataTemp.lat = QString(row[1]).toDouble();
    48. statusDataTemp.lon = QString(row[2]).toDouble();
    49.  
    50. statusDataTemp.sog = QString(row[3]).toDouble();
    51. statusDataTemp.cog = QString(row[4]).toDouble();
    52. statusDataTemp.lastUpdate=QString(row[5]);
    53. statusDataTemp.status=QString(row[6]);
    54.  
    55. S_AIS_DB_SHIPINFO shipInfoDataTemp;
    56.  
    57. shipInfoDataTemp.mmsi=QString(row[7]);
    58. shipInfoDataTemp.IMO = QString(row[8]);
    59. shipInfoDataTemp.callsign=QString(row[9]);
    60. shipInfoDataTemp.name = QString(row[10]);
    61. shipInfoDataTemp.width=QString(row[11]).toDouble();
    62. shipInfoDataTemp.length=QString(row[12]).toDouble();
    63. shipInfoDataTemp.dim_port=QString(row[13]).toInt();
    64. shipInfoDataTemp.dim_starboard=QString(row[14]).toInt();
    65. shipInfoDataTemp.dim_bow=QString(row[15]).toInt();
    66. shipInfoDataTemp.dim_stern=QString(row[16]).toInt();
    67. aisList.append(dbShip);
    68. }
    69. }
    70. mysqldb->closeRslt(result);
    71. mysqldb->close();
    72. }
    73. delete mysqldb;
    74. }
    To copy to clipboard, switch view to plain text mode 
    this program to filter tableWidget items

    Qt Code:
    1. void AIS::on_lineEdit_textChanged(QString )
    2. {
    3. QList<S_AIS_DB_SHIP>aislist;
    4. aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey);
    5. for (int i=0;i<aislist.count();i++){
    6. int curRow =ui->tableWidget->rowCount();
    7. ui->tableWidget->insertRow(curRow);
    8. ui->tableWidget->setItem(curRow,0, new QTableWidgetItem (aislist.at(i).statusData.mmsi));
    9. ui->tableWidget->setItem(curRow,1, new QTableWidgetItem (aislist.at(i).shipInfo.IMO));
    10. ui->tableWidget->setItem(curRow,2, new QTableWidgetItem (aislist.at(i).shipInfo.name));
    11. ui->tableWidget->setItem(curRow,4, new QTableWidgetItem (QString::number(aislist.at(i).statusData.sog)));
    12. ui->tableWidget->setItem(curRow,5, new QTableWidgetItem (QString::number(aislist.at(i).statusData.cog)));
    13. ui->tableWidget->setItem(curRow,6, new QTableWidgetItem (QString::number(aislist.at(i).statusData.lat)));
    14. ui->tableWidget->setItem(curRow,7, new QTableWidgetItem (QString::number(aislist.at(i).statusData.lon)));
    15. }
    16. searchKey = ui->lineEdit->text();
    17.  
    18. if(ui->comboBox->currentText() == "MMSI"){
    19. filterType = ADF_MMSI;
    20. }
    21. else if(ui->comboBox->currentText() == "IMO"){
    22. filterType = ADF_IMO;
    23. }
    24. else if(ui->comboBox->currentText() == "Name"){
    25. filterType = ADF_NAME;
    26. }
    27. m_db->addCurrentShipDataWithFilter(filterType, searchKey);
    28. }
    To copy to clipboard, switch view to plain text mode 
    And this is a function that call the function above

    My program exit when I type something in lineEdit...
    I don't know what's wrong with my program??
    If i try to debug it, it displays that the data is not synchronous...
    Is my function not called properly???\
    Really need help...

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    the textChanged signal is called each time the text "change".
    For example, if you type "hello" it will be called 5 times.

    you call addCurrentShipDataWithFilter at begginnig and at end of the slot, pay attention to performance
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Database program exit when function called

    When it crashes read the stack backtrace until you find which line of your code is triggering the crash then share that with us. Otherwise we are going to guess.

    Here's my first guess: the pointer at line 35 of the first listing is NULL.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    at line 39 you wrote

    Qt Code:
    1. QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";
    To copy to clipboard, switch view to plain text mode 

    a space is missing after "where" and after "seachKey" a <'> is missing too.
    What happens if searchKey is Empty??
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    my program stop from crashing when I add return aisList; after delete mysqldb;
    and now the problem is the function didn't receive the parameter I send (the parameter are AIS_DB_FILTER filterType,QString searchKey)
    when I try to look what inside the parameter the searchKey display the right value but when I look at filterType value it displays unknown symbol...
    I convert AIS_DB_FILTER into QString but it displays unknown symbol.
    I don't know what to do?

    Quote Originally Posted by mcosta View Post
    at line 39 you wrote

    Qt Code:
    1. QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";
    To copy to clipboard, switch view to plain text mode 

    a space is missing after "where" and after "seachKey" a <'> is missing too.
    What happens if searchKey is Empty??
    if searchKey is empty the table displays all the value.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    A question before: "Why you don't use QtSql??"

    my program stop from crashing when I add return aisList; after delete mysqldb;
    ok the problem was here
    Qt Code:
    1. aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey);
    To copy to clipboard, switch view to plain text mode 

    You wrote
    Qt Code:
    1. if(ui->comboBox->currentText() == "MMSI"){
    2. filterType = ADF_MMSI;
    3. }
    4. else if(ui->comboBox->currentText() == "IMO"){
    5. filterType = ADF_IMO;
    6. }
    7. else if(ui->comboBox->currentText() == "Name"){
    8. filterType = ADF_NAME;
    9. }
    To copy to clipboard, switch view to plain text mode 
    What happens if "ui->comboBox->currentText()" isn't ("MMSI", "IMO" or "Name")?
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    Quote Originally Posted by mcosta View Post
    A question before: "Why you don't use QtSql??"


    ok the problem was here
    Qt Code:
    1. aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey);
    To copy to clipboard, switch view to plain text mode 

    You wrote
    Qt Code:
    1. if(ui->comboBox->currentText() == "MMSI"){
    2. filterType = ADF_MMSI;
    3. }
    4. else if(ui->comboBox->currentText() == "IMO"){
    5. filterType = ADF_IMO;
    6. }
    7. else if(ui->comboBox->currentText() == "Name"){
    8. filterType = ADF_NAME;
    9. }
    To copy to clipboard, switch view to plain text mode 
    What happens if "ui->comboBox->currentText()" isn't ("MMSI", "IMO" or "Name")?
    I guess QtSQL is for item view based.

    Qt Code:
    1. aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey);
    To copy to clipboard, switch view to plain text mode 

    about this I'm suspicious about this function. Is it send the parameter to SQL function or not? If not how should I write the function??

    Qt Code:
    1. if(ui->comboBox->currentText() == "MMSI"){
    2. filterType = ADF_MMSI;
    3. }
    4. else if(ui->comboBox->currentText() == "IMO"){
    5. filterType = ADF_IMO;
    6. }
    7. else if(ui->comboBox->currentText() == "Name"){
    8. filterType = ADF_NAME;
    9. }
    To copy to clipboard, switch view to plain text mode 
    I set 3 items for combobox MMSI,IMO and name so if other it does nothing

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database program exit when function called

    You can use QtSql also without ItemView.

    Qt Code:
    1. QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";
    To copy to clipboard, switch view to plain text mode 

    print the value of query;
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Database program exit when function called

    Quote Originally Posted by mcosta View Post
    at line 39 you wrote

    Qt Code:
    1. QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";
    To copy to clipboard, switch view to plain text mode 

    a space is missing after "where" and after "seachKey" a <'> is missing too.
    What happens if searchKey is Empty??
    I totally agree regarding the missing items. Basic SQL syntax and not a Qt issue btw.

    I always try more complicated queries in MySql Workbench rather than debugging them in an application.

Similar Threads

  1. Replies: 3
    Last Post: 25th April 2011, 03:40
  2. Replies: 3
    Last Post: 7th April 2011, 11:09
  3. program exit itself when create dialog with QSystemTrayIcon
    By jim2212001 in forum Qt Programming
    Replies: 2
    Last Post: 27th August 2010, 11:35
  4. Quit, Exit qApp (program) if error?
    By Arsenic in forum Newbie
    Replies: 13
    Last Post: 30th September 2008, 12:59
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16:11

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
  •  
Qt is a trademark of The Qt Company.