简体   繁体   中英

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

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:-

https://i.stack.imgur.com/fEkRg.png

https://i.stack.imgur.com/VxDll.png

I tried to change requests, selectors, file names, rummaged through the SQL forums, did not find a solution to my particular problem.


Screenshots with error and SQL-request:

https://i.stack.imgur.com/fEkRg.png

https://i.stack.imgur.com/VxDll.png

I believe that you have removed the member ID from Notes (or possibly removed the Class entirely) and without then compiling the project looked at the MainDAO_Impl.

For example consider:-

@Entity
class Notes {
    @PrimaryKey(autoGenerate = true)
    long ID;
    String title;
    String notes;
    String date;
    boolean pinned;
}

After compiling then MainDAO_Impl is:-

在此处输入图像描述

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:-

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM