1

I have a standart long field

 @PrimaryKey(autoGenerate = true)
 val id:Long

But it is filled in the process and has a random value. If I try to add it to the database through the DAO, then the id is not overwritten by autoGenerate sqlite engine

 @Insert(onConflict = OnConflictStrategy.REPLACE)
 fun insert(i:MyTypeObject)

If I do this type field nullable its does not work too

 @PrimaryKey(autoGenerate = true)
 val id:Long?

There is a way that any value that would be in the id field is forcibly converted to autoincrement value in db?

I recive data from repository livedata. In viewmodel

private val db = CurrencyDatabase.getInstance(getApplication())
private val dao = db!!.nalDao()
private val service = Executors.newFixedThreadPool(1)

  fun addBook(book: CurrencyItem)
{
    service.submit {
        dao.insert(book)
    }
}

1 Answers1

1

Thanks all for the help. By official documentaion "If the field type is long or int (or its TypeConverter converts it to a long or int), Insert methods treat 0 as not-set while inserting the item. " You must add 0 to id field with autoincrement.

Also my problem was incorrect access to database data file. I use "Device file explorer" on Android stuidiо and "Save as" on right click menu. Its wrong way for SQL data debugging. Correct access to data its adb terminal shell. For example my OS Win10 and database file name = book.db, and datatable = nal

Open terminal

cd %USERPROFILE%\AppData\Local\Android\sdk\platform-tools
adb root
adb devices

My device was "emulator-5554" then

adb -s emulator-5554 shell
sqlite3

Inside sqlite shell

.open /data/data/com.example.scotland/databases/book.db
select * from nal;

If you cant find you database file. Try open

View->ToolWindows->Device File explorer.

There tree you ADB filesystem. You must open

data->data->YouProjectName->databases