0

Hello I am new to flutter and I am tryin to fetch data from MYSQL database based on keyword entered by the user. But the result is empty even though the data is present. I am using msqljocky5 package for MYSQL connection.

 Future<Results> getProducts()async{
   if(searchValue==""){
   ConnectionSettings settings = new ConnectionSettings(
       host: '10.0.2.2',
       port: 3306,
       user: 'root',
       password:'',
       db: 'master'
   );
   dynamic db =  await MySqlConnection.connect(settings);
  try{
    Results results =   await (await db
        .execute('SELECT * from product'))
        .deStream();
    await db.close();
    return (results);
  }catch(e){
    print("Error01"+e.toString());
  }
   }else{
     ConnectionSettings settings = new ConnectionSettings(
         host: '10.0.2.2',
         port: 3306,
         user: 'root',
         password:'',
         db: 'master'
     );
     dynamic db =  await MySqlConnection.connect(settings);
     try{
       print("Serach valuse is "+searchValue);
       Results results =   await (await db
           .prepared('SELECT * from product WHERE productName LIKE "%?%"',[searchValue]))
           .deStream();
       await db.close();
       print("AffectedRows"+results.affectedRows.toString());
       print(results);
       results.forEach((rw.Row rw) {print(rw[0]); });
       return (results);
     }catch(e){
       print("Error01"+e.toString());
     }
   }}

2 Answers2

0

Editing code helped me fix the issue.

Results results =   await (await db
       .execute('SELECT * from product WHERE productName LIKE "$searchValue%"'))
       .deStream();
0

Ran into the same problem. The issue is with .affectedRows.

I imagine that this value changes with SQL UPDATE/DELETE statements, use .lenght as it will return 0 or a value equal or greater than 1 to validate the results.

HumbertoS
  • 11
  • 2