0

I'm trying to create a database to store Sudokus for a Sudoku app. I save them as strings which I wanna split into an ArrayList later on. I created a really simple activity to test this out, which consists of a GridView that shows the Sudoku with an adapter. The GridView works just fine when I try to insert something I don't get from the DB, but when I try to use the DB the app crashes right when it opens. I followed various tutorials about SQLite DB and even tried some of my teacher code but I really don't know what's happening here.

Here's my main activity code:

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;
import static biel.edu.fje.bdsudokuprova.SudokuDB.nomTaulaSudokus;

public class MainActivity extends AppCompatActivity {

    static final String[] numbers = new String[] {
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9",
            "1", "2", "3", "4", "5", "6", "7", "8", "9"};

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

        SudokuDB sudoDButil = new SudokuDB(getBaseContext());
        SQLiteDatabase db = sudoDButil.getWritableDatabase(); // This is where it fails (I have to
                                                              // comment from here to line 60 for the app to at least open)

        ContentValues valors = new ContentValues();
        valors.put(nomTaulaSudokus, "123456789123456789123456789123456789123456789123456789123456789123456789123456789");
        db.insert(nomTaulaSudokus, null, valors);
        Cursor cursorSudoku = db.rawQuery("select sudoku from sudokus", null);
        String sudo = null;

        try {
            if (cursorSudoku.moveToFirst()) {
                do {
                    sudo = cursorSudoku.getString(1);
                } while(cursorSudoku.moveToNext());
            }
        } catch (Exception e) {
            Log.d(TAG, "Error a l'hora d'intentar agafar la informació de la base de dades");
        } finally {
            if (cursorSudoku != null && !cursorSudoku.isClosed()) {
                cursorSudoku.close();
            }
        }

        ArrayList<String> sudoArray = new ArrayList<String>();
        for (int i = 0; i < 81; i++) {
            sudoArray.add("1");
            //String.valueOf(sudo.charAt(i))
        }

        GridView gridView = findViewById(R.id.gridView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, numbers);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(),
                        ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Edit: I forgot to mention that I use the "numbers" variable to test out if the GridView works fine without using the ArrayList. Sorry, it's a bit messy.

And here's my DB helper class:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SudokuDB extends SQLiteOpenHelper {

    public static final String nomBd = "Sudoku";
    public static final int versioBd = 1;
    public static final String nomTaulaSudokus = "sudokus";
    public static final String columnIdSudokus = "id";
    public static final String columnSudo = "sudoku";

    private static final String SQL_CREACIO_TAULA_SUDOKUS = "CREATE TABLE " + nomTaulaSudokus + " ( "
            + columnIdSudokus + "INTEGER PRIMARY KEY AUTOINCREMENT,"
            + columnSudo + " TEXT )";

    private static final String SQL_ESBORRAT_TAULA_SUDOKUS = "DROP TABLE IF EXISTS "
            + nomTaulaSudokus;

    public SudokuDB(Context context) {
        super(context, nomBd, null, versioBd);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREACIO_TAULA_SUDOKUS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_ESBORRAT_TAULA_SUDOKUS);
        onCreate(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}

Thank you very much for any help given and sorry if this is any nuisance for you all. It might be a really simple and silly error but I still don't understand very well how SQLite DB works.

Biel
  • 193
  • 1
  • 3
  • 15
  • generally a bad idea to do DB transactions on the main UI thread. you may want to use an AsyncTask and put all the db transactions there you may want to change getBaseContext() to this on the failing line. it would be helpful if you share the logcat output – chaitanya Nov 19 '18 at 23:46
  • You have a syntax error in the `CREATE TABLE` statement. You're missing a space between `columnIdSudokus` and `INTEGER` in `columnIdSudokus + "INTEGER PRIMARY KEY AUTOINCREMENT,"`. – Mike M. Nov 19 '18 at 23:49

0 Answers0