D
D
Dmitry2020-08-30 15:41:05
Java
Dmitry, 2020-08-30 15:41:05

Why doesn't SQLite save data after app close on emulator?

Hello!
I just know SQLite.
Please tell me what could be the problem - while I'm in the application itself - reading and writing are successful. As soon as I left the emulator and went in, I try to read the record in the database, it gives a link that the problem is in the Cursor. So I understand that the record was not saved in the database?

markup

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="385dp"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:weightSum="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/btnAdd"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="AddDB"
            android:text="Запись"></Button>

        <Button
            android:id="@+id/btnUpdate"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="UpdateDB"
            android:text="Обновить"></Button>

        <Button
            android:id="@+id/btnRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="ReadfromDB"
            android:text="Читать"></Button>

        <Button
            android:id="@+id/btnClear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="DeleteDB"
            android:text="Очистить"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="233dp"
        android:layout_height="161dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:id="@+id/scoreText"
            android:hint="Очки"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/lvlText"
            android:hint="Уровень"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/tryText"
            android:hint="Попыток"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="264dp"
        android:layout_height="160dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

        <TextView
            android:id="@+id/scoreView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/lvlView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tryView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


Maine
package freijer.app.sucktest;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity  {

    Button btnAdd, btnRead, btnClear;
    EditText scoreText, lvlText, tryText;
    TextView scoreView, lvlView, tryView;

    String addsc;
    String addlvl;
    String addtryss;

    String score;
    String lvl;
    String trys;

    DataHelper dbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnAdd =  findViewById(R.id.btnAdd);
        btnRead =  findViewById(R.id.btnRead);
        btnClear =  findViewById(R.id.btnClear);

        scoreText =  findViewById(R.id.scoreText);
        lvlText =  findViewById(R.id.lvlText);
        tryText =  findViewById(R.id.tryText);

        scoreView = findViewById(R.id.scoreView);
        lvlView = findViewById(R.id.lvlView);
        tryView = findViewById(R.id.tryView);

        score = scoreText.getText().toString();
        lvl = lvlText.getText().toString();
        trys = tryText.getText().toString();

        dbHelper = new DataHelper(this);



    }

    public void AddDB(View v) {
        addsc = scoreText.getText().toString();
        addlvl = lvlText.getText().toString();
        addtryss = tryText.getText().toString();

        dbHelper.WriteDB(addsc, addlvl, addtryss);
    }
    
    public void ReadfromDB(View v) {
        dbHelper.ReadDB();
    }

    public void DeleteDB(View v){
    dbHelper.DeleteDB();
}

    public void UpdateDB(View v){
        addsc = scoreText.getText().toString();
        addlvl = lvlText.getText().toString();
        addtryss = tryText.getText().toString();
        dbHelper.UpdateDB(addsc, addlvl, addtryss);
    }
}


DBOpenHelper
package freijer.app.sucktest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataHelper extends SQLiteOpenHelper {
    
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "contactDb";
    public static final String TABLE_NAME = "contacts";

    public static final String KEY_ID = "_id";
    public static final String KEY_SCORE = "scores";
    public static final String KEY_LVL = "lvls";
    public static final String KEY_TRYS = "tryss";

    SQLiteDatabase database;
    ContentValues contentValues = new ContentValues();

    public DataHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + "(" + KEY_ID
            + " integer primary key," + KEY_SCORE + " text," +  KEY_LVL+ " text," +  KEY_TRYS + " text" +  ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_NAME);
        onCreate(db);
    }

    public void WriteDB(String score, String lvl, String tryss){
        database = getWritableDatabase();
        contentValues.put(KEY_SCORE, score);
        contentValues.put(KEY_LVL, lvl);
        contentValues.put(KEY_TRYS, tryss);

        database.insert(TABLE_NAME, null, contentValues);

    }

    public void ReadDB(){
        Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            int idIndex = cursor.getColumnIndex(KEY_ID);
            int scoreIndex = cursor.getColumnIndex(KEY_SCORE);
            int lvlIndex = cursor.getColumnIndex(KEY_LVL);
            int tryslIndex = cursor.getColumnIndex(KEY_TRYS);
            do {
                Log.d("myLogs", "ID = " + cursor.getInt(idIndex) +
                        ", очки = " + cursor.getString(scoreIndex) +
                        ", уровень = " + cursor.getString(lvlIndex) +
                        ", попыток = " + cursor.getString(tryslIndex) );
            } while (cursor.moveToNext());
        } else
            Log.d("myLogs","0 rows");
        cursor.close();
    }

    public void DeleteDB(){
        database.delete(TABLE_NAME, null, null);
        database.close();
    }

    public void UpdateDB(String score, String lvl, String tryss){
        database = getWritableDatabase();
        contentValues.put(KEY_SCORE, score);
        contentValues.put(KEY_LVL, lvl);
        contentValues.put(KEY_TRYS, tryss);

        String id = "1";
        int updCount  = database.update(TABLE_NAME, contentValues, KEY_ID + "= ?" , new String[] {id});
        Log.d("myLogs", "updated rows count = " + updCount);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question