D
D
Dmitry2020-08-29 15:30:14
Java
Dmitry, 2020-08-29 15:30:14

Trying to understand SQLite. Where is the mistake?

I just started learning SQLite, I can't figure out why in this case I get in the log: Does it seem to refer to problems with the table name? activity_main

Log: ID = 0, score = 1, lvl = 2, trys = 3

E/SQLiteLog: (1) no such table: statistics


<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="3"
        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/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>


MainActivity
package freijer.app.sucktest;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity  {

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

    DataBaseHelper dbh;

    String score;
    String lvl;
    String trys;

    @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();

        dbh = new DataBaseHelper(this);


    }

        public void AddDB(View v) {
        dbh.AddDataBase(score, lvl, trys);
        }

        public void ReadfromDB(View v) {
              dbh.ReadFromDataBase();
        }

         public void DeleteDB(View v){
             dbh.CleanDB();
        }
}


DataBaseHelper
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 DataBaseHelper extends SQLiteOpenHelper {
    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "player";
    public static final String TABLE_NAME = "statistics";

    public static final String KEY_ID = "_id";
    public static final String COLUM_SCORE = "score";
    public static final String COLUM_LVL = "lvl";
    public static final String COLUM_TRYS = "trys";


    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) { //    Создаем таблицу:
        db.execSQL("create table " + TABLE_NAME + "( " + KEY_ID + " INTEGER PRIMARY KEY, " + COLUM_SCORE + " TEXT, " + COLUM_LVL + " TEXT, " + COLUM_TRYS + " TEXT " + ")");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void AddDataBase(String score, String lvl, String trys) {
        ContentValues contentValues = new ContentValues();
        SQLiteDatabase database = this.getWritableDatabase();

        contentValues.put(COLUM_SCORE, score);
        contentValues.put(COLUM_LVL, lvl);
        contentValues.put(COLUM_TRYS, trys);

        database.insert(TABLE_NAME, null, contentValues);
        database.close();
    }

    public void  ReadFromDataBase(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            int id = cursor.getColumnIndex(KEY_ID);
            int lvl = cursor.getColumnIndex(COLUM_LVL);
            int score = cursor.getColumnIndex(COLUM_SCORE);
            int trys = cursor.getColumnIndex(COLUM_TRYS);
            do {
                Log.d("Log",
                        "ID = " + id +
                        ", score = " + score +
                        ", lvl = " + lvl +
                        ", trys = " + trys);
            } while (cursor.moveToNext());
        } else
            cursor.close();
    }

    public void CleanDB(){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, null, null);
        db.close();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BitNeBolt, 2020-08-30
@Starv

You forgot about the semicolon at the end of the request to the database to create.
It should be like this:

db.execSQL("create table " + TABLE_NAME + "( " + KEY_ID + " INTEGER PRIMARY KEY, " + COLUM_SCORE + " TEXT, " + COLUM_LVL + " TEXT, " + COLUM_TRYS + " TEXT " + ");");

And check the syntax in other commands, just in case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question