A
A
AlexAlex0002021-03-04 16:29:35
Java
AlexAlex000, 2021-03-04 16:29:35

How to overwrite data from a row in SQlite?

Good afternoon.
Help me to understand.
I have an SQlite table with a column "musicSwitch" in my application.
In this column, I need only one line, which will store either 0 or 1, so that when loading, the application determines whether background music should be turned on or not.
Accordingly, when the button is clicked, the following is done:

switch (buttonpress)// определяет включен или выключен звук сейчас
{
                            case 1: // если включен
                               buttonpress=0; 
                               contentValues.put("musicSwitch", 0);//пишет в таблицу строку со значением 0
                                stopPlayer();//останавливает плеер
                                break;
                            case 0: // если выключен
                                buttonpress=1;
                                contentValues.put("musicSwitch", 1);//пишет в таблицу строку со значением 1
                                play();//  включает плеер
                                break;
                        }
database.insert(DBHelper.TABLE_SETTING, null, contentValues);


The problem is that each click on the button creates a new row in the table.
and I have a question, how to do it correctly so that when you press it multiple times, the line remains one.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-03-04
@zagayevskiy

What for it in general to write to a DB? Especially since you're obviously doing it on the mainstream. Do not do it this way. Write in shared preferences.

V
VitalyChaikin, 2021-03-05
@VitalyChaikin

see Update
https://www.sqlitetutorial.net/sqlite-update/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question