-1

I am trying to add a row to my "exercises" table for my gym_tracker app , the weird thing is that whenever I add the exercise not all the items are inserted , I am new to sqflite so probably there is something wrong in my code , pls tell me if this looks correct : this is my exercise create method:

 Future<Database> initialdatabase2() async {
    String databasepath = await getDatabasesPath();
    var path = join(databasepath, "exercises.db");
    Database mydb = await openDatabase(path,
        onCreate: _oncreate2, version: 1, onUpgrade: _onupgrade);
    return mydb;
  }

//exercise db
  _oncreate2(Database db2, int version) async {
    await db2.execute('''
CREATE TABLE "exercises" 
(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT , workoutname Text , exercisename Text , sets INTEGER , reps INTEGER , weight REAL
) 
    ''');

this is my insert method :

 insertData2(String sql) async {
    Database? db = await get2();
    int response = await db!.rawInsert(sql);
    return response;
  }

this is where I am using it in my cubit (I am using cubit as my state management) :

 void addexercise(
      {required String name,
      required String workoutname,
      required int reps,
      required int sets,
      required double weight}) async {
    print(name);
    print(workoutname);
    print(reps);
    print(sets);
    print(weight);
    emit(ExercisesListLoadingState());
    await casheHelper
        .insertData2(
            "INSERT INTO 'exercises' (workoutname ,exercisename , sets ,reps ,weight) VALUES ('$workoutname','$name',$sets ,$reps ,$weight )")
        .then((value) {
      exercisesname = [];
      loadexercises();
      print("exercise added");
      emit(ExercisesListAddSuccessState());
    });
  }

note that when i am printing the data in my cubit method i have them all , but whenever i call them from the table only two of them (workoutname and weight) are being null . thanks all .

hamz
  • 47
  • 8

1 Answers1

0

Maybe that it not the reason but you should first use parameters instead of putting the arguments in the sql statement, i.e. instead of

rawInsert("INSERT INTO 'exercises' (workoutname ,exercisename , sets ,reps ,weight) 
  VALUES ('$workoutname','$name',$sets ,$reps ,$weight )");

try something like:

rawInsert("INSERT INTO 'exercises' (workoutname ,exercisename , sets ,reps ,weight) 
  VALUES (?, ?, ?, ?, ?)", [workoutname,name, sets, reps, weight]);

More info here: https://github.com/tekartik/sqflite/blob/master/sqflite/doc/sql.md#parameters

alextk
  • 5,713
  • 21
  • 34