Home > database >  Android Room Database: stmt.bindLong(1, value.ID);
Android Room Database: stmt.bindLong(1, value.ID);

Time:01-22

I can not fix the error when creating Room Database in Android Studio. Error: stmt.bindLong(1, value.ID); Also in the database sql queries: "notes", "title", "id" are highlighted in red, as if there is an error here. When trying to compile, it transfers to a file - MainDAO_Impl.java. Shown in the screenshotRoom code:

package com.example.applicationnotes.DataBase;

import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.applicationnotes.Models.Notes;

@Database(entities = Notes.class, version = 1, exportSchema = false)
public abstract class RoomDB extends RoomDatabase {

    private static RoomDB database;
    private static String DATABASE_NAME = "NoteApp";
    
    public synchronized static RoomDB getInstance(Context context){
        if (database == null) {
            database = Room.databaseBuilder(context.getApplicationContext(),
                            RoomDB.class, DATABASE_NAME)
                    .allowMainThreadQueries()
                    .fallbackToDestructiveMigration()
                    .build();
        }
        return database;
    }
    
    public abstract com.example.applicationnotes.DataBase.MainDAO mainDao();

}

MainDAO:

package com.example.applicationnotes.DataBase;

import static androidx.room.OnConflictStrategy.REPLACE;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

import com.example.applicationnotes.Models.Notes;

@Dao
public interface MainDAO {

    @Insert (onConflict = REPLACE)
    void  insert (Notes notes);
    
    @Query ("SELECT * FROM notes ORDER BY id DESC")
    List<Notes> getAll();
    
    @Query("UPDATE notes SET title = :title, notes = :notes WHERE ID = :id")
    void update (int id, String title, String notes);
    
    @Delete
    void  delete (Notes notes);
    
    @Query("UPDATE notes SET pinned = :pin WHERE ID = :id")
    void pin (int id, boolean pin);

}

More in screenshot:-

enter image description here

Now if Notes is changed to (long ID; commented out):-

@Entity
class Notes {
    @PrimaryKey(autoGenerate = true)
    //long ID; /*<<<<<<<<<< COMMENTED OUT >>>>>>>>>>*/
    String title;
    String notes;
    String date;
    boolean pinned;
}

Then without compiling MainDAO_Impl is:-

enter image description here

  • Related