My application is closing when starting the screen with database

Asked

Viewed 137 times

2

My activity with the database is with some problem, because I created the class right and Android Studio is not showing any error, but when I will run it on mobile when opening activity that has the database it closes.

Would anyone have any idea what it is?

package Bancosimples (bank)

Simplesbanco

package BancoSimples;

import com.allsport.miyonic.allsport.SimplesHome;

import java.io.Serializable;

public class SimplesBanco implements Serializable{
    public long id;
    public String timeum;
    public String timedois;
    public String golone;
    public String goldois;


    public SimplesBanco(long id, String timeum, String timedois, String golone, String goldois){
        this.id = id;
        this.timeum = timeum;
        this.timedois = timedois;
        this.golone = golone;
        this.goldois = goldois;
    }

    @Override
    public String toString() {
        return timeum;
    }
}

Simplesrepositorio

package BancoSimples;

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

public class SimplesRepositorio {

    private SimplesSQLHelper helper;

    public SimplesRepositorio(Context ctx) {
        helper = new SimplesSQLHelper(ctx);
    }

    private long inserir(SimplesBanco simplao) {
        SQLiteDatabase db = helper.getWritableDatabase();

        ContentValues cv = new ContentValues();

        cv.put(SimplesSQLHelper.COLUNA_NOMEONE, simplao.timeum);
        cv.put(SimplesSQLHelper.COLUNA_NOMETWO, simplao.timedois);
        cv.put(SimplesSQLHelper.COLUNA_GOLONE, simplao.golone);
        cv.put(SimplesSQLHelper.COLUNA_GOLTWO, simplao.goldois);

        long id = db.insert(SimplesSQLHelper.TABELA_SIMPLES, null, cv);
        if (id != -1) {
            simplao.id = id;
        }

        db.close();
        return id;
    }

    public void salvar(SimplesBanco simplao) {
        if (simplao.id == 0) {
            inserir(simplao);
    }
 }
}

Simplessqlhelper

package BancoSimples;

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


public class SimplesSQLHelper extends SQLiteOpenHelper {
    private static final String NOME_BANCO = "dbsimples";

    private static final int VERSAO_BANCO = 1;

    public static final String TABELA_SIMPLES = "result";
    public static final String COLUNA_ID = "_id";
    public static final String COLUNA_NOMEONE = "timeum";
    public static final String COLUNA_NOMETWO = "timedois";
    public static final String COLUNA_GOLONE = "golone";
    public static final String COLUNA_GOLTWO = "goldois";


    public SimplesSQLHelper(Context context){
        super(context, NOME_BANCO, null, VERSAO_BANCO);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase){
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + TABELA_SIMPLES + "(" + COLUNA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUNA_NOMEONE + "TEXT NOT NULL,"
                        + COLUNA_NOMETWO + "TEXT NOT NULL," + COLUNA_GOLONE + "TEXT NOT NULL," + COLUNA_GOLTWO + "TEXT NOT NULL)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion){
        //para as proximas versões
    }
}

activity_simple.xml

import BancoSimples.SimplesBanco;
import BancoSimples.SimplesRepositorio;
import BancoSimples.SimplesSQLHelper;

public void SalvarR (SimplesBanco simplao){
        SimplesRepositorio repo = new SimplesRepositorio(this);
        repo.salvar(simplao);
        setResult(RESULT_OK);
    }
  • 1

    missing post error log, see in Logcat and post here again

1 answer

1

Nathan

It is very difficult to find the error without the log, but we try to move around to see where the error is. First, let’s fix your table creation SQL, because it is missing some spaces during the concatenation of the string with the variables>

"CREATE TABLE " + TABELA_SIMPLES + "(" + COLUNA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUNA_NOMEONE + "TEXT NOT NULL,"
                    + COLUNA_NOMETWO + "TEXT NOT NULL," + COLUNA_GOLONE + "TEXT NOT NULL," + COLUNA_GOLTWO + "TEXT NOT NULL)"

Let’s look at the excerpt COLUNA_NOMEONE + "TEXT NOT NULL,", when concatenating, there will be no space between the column name and the TEXT type, being the result of something like colunaumTEXT NOT NULL. Note that you have several such cases in your SQL above.

Anything if only this does not solve, comment here below to go discovering.

  • Hello friend... sorry for the delay of the reply of the comment.... So I’ll follow your lead, but I have a plan to start all over again and I found a playlist on Youtube... in case I don’t get any help from you... but thank you very much for your attention.... but I’ll do your scheme first :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.