PDA

View Full Version : Select query using mysql for searching



sinha.ashish
9th April 2008, 05:28
void VistaMedia::search()
{
QString searchText=searchTextLineEdit->text();
QSqlQuery query;
query.prepare("SELECT * FROM mediadetails WHERE title LIKE %:title% ");
query.bindValue(":title",searchText);
query.exec();
while(query.next())
listWidget->addItem(query.value(1).toString());
QSqlError e=query.lastError () ;
QMessageBox::information(this,"problem",e.text());
}

above function shows error message box as "syntax error near %" after calling. Help me to write the correct syntax or anyclues for searching using wildcards!!!!!!!!

marcel
9th April 2008, 08:00
The query should be:
SELECT * FROM mediadetails WHERE title LIKE '%:title%'

Lykurg
9th April 2008, 22:26
...and better don't use the asterisk in conjunction with query.value(1). If You later change your table layout, the code will behave incorrect. Use "SELECT `myColumnNameWhichIWant`..." and query.value(0) in your case.

Lykurg

sinha.ashish
10th April 2008, 05:21
void VistaMedia::search()
{
QString searchText=searchTextLineEdit->text();
QSqlQuery query;
query.prepare("SELECT title FROM mediadetails WHERE title LIKE ' %:title% '"); query.bindValue(":title",searchText);
query.exec();
while(query.next())
listWidget->addItem(query.value(0).toString());
QSqlError e=query.lastError () ;
QMessageBox::information(this,"problem",e.text());
}



table details are as :
mediadetails
----------------
|title|

F_1.avi
F_2.avi
11.dat
music.dat
jazz.avi

i am entering in the search text " f ".

thanks for the suggesstions ....but even after doing so....there is a new error message shows "unable to fetch data". what to do????

sinha.ashish
10th April 2008, 06:14
void VistaMedia::search()
{
QString serachText=searchTextLineEdit->text().trimmed();
int noOfItems=listWidget->count();
while(noOfItems>0)
{
if(!(listWidget->item(noOfItems-1)->text().contains(mname, Qt::CaseInsensitive)))
{
listWidget->removeItemWidget ( listWidget->item(noOfItems-1));
delete listWidget->item(noOfItems-1);
}
l--;
}
}


I have solved my problem in another way ,just using listwidget's functions,without using query.its fast and better.Thanks to all replies ....