PDA

View Full Version : Database program exit when function called



micamica
13th July 2011, 07:20
I have a program like this


QList <S_AIS_DB_SHIP> cAISDataBase::addCurrentShipDataWithFilter(AIS_DB_ FILTER filterType, QString searchKey)
//void cAISDataBase::addCurrentShipDataWithFilter(AIS_DB_ FILTER filterType,QString searchKey)
{
QList<S_AIS_DB_SHIP>aisList;
QString type;
if (filterType==ADF_MMSI)
type=QString("mmsi");
else if(filterType==ADF_AREAFILTER)
type=QString("areaFilter");
else if (filterType==ADF_STATUS)
type=QString("status");
else if (filterType==ADF_COG)
type=QString("cog");
else if (filterType==ADF_SOG)
type=QString("sog");
else if (filterType==ADF_IMO)
type=QString("imo");
else if (filterType==ADF_CALLSIGN)
type=QString("callsign");
else if (filterType==ADF_STERN)
type=QString("dim_stern");
else if (filterType==ADF_BOW)
type=QString("dim_bow");
else if (filterType==ADF_STARBOARD)
type=QString("dim_starboard");
else if (filterType==ADF_PORT)
type=QString("dim_port");
else if (filterType==ADF_WIDTH)
type=QString("width");
else if (filterType==ADF_LENGTH)
type=QString("length");
else if (filterType==ADF_NAME)
type=QString("name");

cMYSQLDB *mysqldb=createDbInstance();
if(mysqldb->open()){
MYSQL_RES *result;
MYSQL_ROW row;
QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";

result=mysqldb->query(query);
if (result){
S_AIS_DB_SHIP dbShip;
while ((row=mysql_fetch_row(result))!=NULL){
S_AIS_DB_STATUSDATA statusDataTemp;
statusDataTemp.mmsi = QString(row[0]);
statusDataTemp.lat = QString(row[1]).toDouble();
statusDataTemp.lon = QString(row[2]).toDouble();

statusDataTemp.sog = QString(row[3]).toDouble();
statusDataTemp.cog = QString(row[4]).toDouble();
statusDataTemp.lastUpdate=QString(row[5]);
statusDataTemp.status=QString(row[6]);

S_AIS_DB_SHIPINFO shipInfoDataTemp;

shipInfoDataTemp.mmsi=QString(row[7]);
shipInfoDataTemp.IMO = QString(row[8]);
shipInfoDataTemp.callsign=QString(row[9]);
shipInfoDataTemp.name = QString(row[10]);
shipInfoDataTemp.width=QString(row[11]).toDouble();
shipInfoDataTemp.length=QString(row[12]).toDouble();
shipInfoDataTemp.dim_port=QString(row[13]).toInt();
shipInfoDataTemp.dim_starboard=QString(row[14]).toInt();
shipInfoDataTemp.dim_bow=QString(row[15]).toInt();
shipInfoDataTemp.dim_stern=QString(row[16]).toInt();
aisList.append(dbShip);
}
}
mysqldb->closeRslt(result);
mysqldb->close();
}
delete mysqldb;
}

this program to filter tableWidget items



void AIS::on_lineEdit_textChanged(QString )
{
QList<S_AIS_DB_SHIP>aislist;
aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey) ;
for (int i=0;i<aislist.count();i++){
int curRow =ui->tableWidget->rowCount();
ui->tableWidget->insertRow(curRow);
ui->tableWidget->setItem(curRow,0, new QTableWidgetItem (aislist.at(i).statusData.mmsi));
ui->tableWidget->setItem(curRow,1, new QTableWidgetItem (aislist.at(i).shipInfo.IMO));
ui->tableWidget->setItem(curRow,2, new QTableWidgetItem (aislist.at(i).shipInfo.name));
ui->tableWidget->setItem(curRow,4, new QTableWidgetItem (QString::number(aislist.at(i).statusData.sog)));
ui->tableWidget->setItem(curRow,5, new QTableWidgetItem (QString::number(aislist.at(i).statusData.cog)));
ui->tableWidget->setItem(curRow,6, new QTableWidgetItem (QString::number(aislist.at(i).statusData.lat)));
ui->tableWidget->setItem(curRow,7, new QTableWidgetItem (QString::number(aislist.at(i).statusData.lon)));
}
searchKey = ui->lineEdit->text();

if(ui->comboBox->currentText() == "MMSI"){
filterType = ADF_MMSI;
}
else if(ui->comboBox->currentText() == "IMO"){
filterType = ADF_IMO;
}
else if(ui->comboBox->currentText() == "Name"){
filterType = ADF_NAME;
}
m_db->addCurrentShipDataWithFilter(filterType, searchKey);
}

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...

mcosta
13th July 2011, 08:01
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

ChrisW67
13th July 2011, 08:14
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.

mcosta
13th July 2011, 08:22
at line 39 you wrote



QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";


a space is missing after "where" and after "seachKey" a <'> is missing too.
What happens if searchKey is Empty??

micamica
13th July 2011, 08:26
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? :confused:


at line 39 you wrote



QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";


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.

mcosta
13th July 2011, 08:34
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

aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey) ;

You wrote


if(ui->comboBox->currentText() == "MMSI"){
filterType = ADF_MMSI;
}
else if(ui->comboBox->currentText() == "IMO"){
filterType = ADF_IMO;
}
else if(ui->comboBox->currentText() == "Name"){
filterType = ADF_NAME;
}

What happens if "ui->comboBox->currentText()" isn't ("MMSI", "IMO" or "Name")?

micamica
13th July 2011, 08:41
A question before: "Why you don't use QtSql??"


ok the problem was here

aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey) ;

You wrote


if(ui->comboBox->currentText() == "MMSI"){
filterType = ADF_MMSI;
}
else if(ui->comboBox->currentText() == "IMO"){
filterType = ADF_IMO;
}
else if(ui->comboBox->currentText() == "Name"){
filterType = ADF_NAME;
}

What happens if "ui->comboBox->currentText()" isn't ("MMSI", "IMO" or "Name")?

I guess QtSQL is for item view based.


aislist=m_db->addCurrentShipDataWithFilter(filterType,searchKey) ;

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??



if(ui->comboBox->currentText() == "MMSI"){
filterType = ADF_MMSI;
}
else if(ui->comboBox->currentText() == "IMO"){
filterType = ADF_IMO;
}
else if(ui->comboBox->currentText() == "Name"){
filterType = ADF_NAME;
}

I set 3 items for combobox MMSI,IMO and name so if other it does nothing

mcosta
13th July 2011, 09:23
You can use QtSql also without ItemView.


QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";

print the value of query;

schnitzel
13th July 2011, 17:07
at line 39 you wrote



QString query="SELECT *FROM SKM_T_AIS_CURRENTSHIPLIST where" +type+ " ='" +searchKey +";";


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.