-1

I need help I am developing an app using SQLite, here is my scenario I want to make favorite feature using SQLLITE, if the value does exist then fill the heart icon with red color otherwise remove the color from the heart icon.

here is my code

  //this table where i am fetching all data
    
    //fetch all verse
       static Future<List<Map<String, dynamic>>> fetchallverse(String database, int booknumber,int chapternumber) async {
         final db = await Dbhelper().db;
         return db!.rawQuery('SELECT  DISTINCT id,v,t from ${database} WHERE b=${booknumber} AND c=${chapternumber}');
    
    
       }
    
    //this feature used add favourite
    //add favourite
      static favourite(var bookname,var chapternumber,var versenumber,var verse,var bookid) async {
    final db = await Dbhelper().db;
      //  db!.rawInsert('INSERT INTO favourite(bookname, chapternumber,versenumber,verse,bookid) VALUES("${bookname},${chapternumber},${versenumber},${verse},${bookid}")');
    
    db!.rawInsert('INSERT INTO favourite(bookname, chapternumber,versenumber,verse,bookid) VALUES("${bookname}",${chapternumber},${versenumber},"${verse}",$bookid)');
    print(db);
    
      }
    //now fethcing data from favourite successfully
    
    //data coming from favourite
      static Future<List<Map<String, dynamic>>> getfavourite() async {
        final db = await Dbhelper().db;
        return db!.rawQuery('SELECT * from  favourite');
    
        }
    
    
    
    
    
//here is my listview favourite button make code shorter for easier understing

FavoriteButton(

                 iconDisabledColor: Colors.grey,
                 iconSize:30,
                 valueChanged: (_isFavorite) {


                    Dbhelper.favourite(widget.bookname,widget.chapternumber,allverse[index]['v'],allverse[index]['t'],allverse[index]['id']);
                 },
               ),
aneel ilyas
  • 31
  • 10

1 Answers1

1

To check if a value exists in the SQLite database, you can use the SELECT statement with a WHERE clause to check if the desired value exists in the table. If a value is returned from the query, it means that the value exists in the table, and you can fill the heart icon with red color. If no value is returned, it means that the value does not exist in the table, and you can remove the color from the heart icon.

Here is an example of how you can check if a value exists in the "favourite" table:

static Future<List<Map<String, dynamic>>> checkFavourite(var bookname,var chapternumber,var versenumber) async {
  final db = await Dbhelper().db;
  return db!.rawQuery('SELECT * FROM favourite WHERE bookname = "$bookname" AND chapternumber = $chapternumber AND versenumber = $versenumber');
}

You can then call this method with the desired values, and check the length of the returned list to see if the value exists in the table.

List<Map<String, dynamic>> isFavourite = await Dbhelper.checkFavourite(widget.bookname,widget.chapternumber,allverse[index]['v']);
if(isFavourite.length > 0) {
  // fill heart icon with red color
} else {
  // remove color from the heart icon
}

Note: You should use parameterized queries instead of string concatenation to avoid SQL injection vulnerabilities in your code.

Carlos
  • 21
  • 3