Why does my app stop when using this line of code?

2

This is the code of my database class.

public class SQLite_OpenHelper extends SQLiteOpenHelper {
public SQLite_OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE t_emisiones(pais TEXT primary key, emision REAL);");
}

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

This is where my app stops, when it arrives at (db.execSQL).

SQLite_OpenHelper dbhelp = new SQLite_OpenHelper(this,"emisores_paises", null, 1);
    SQLiteDatabase db = dbhelp.getWritableDatabase();
    db.execSQL("INSERT INTO t_emisiones VALUES('colombia', 0.205)");
    db.execSQL("INSERT INTO t_emisiones VALUES('espana', 0.388)");
    db.execSQL("INSERT INTO t_emisiones VALUES('mexico', 0.582)");
    db.close();

I have been programming in Android Studio and I am very new, what I want to do is a database with values that I add as constants and then I can read them to work with them but adding the line of code to insert data is stop the app.

PS: suggestions in case it is better to work with fixes than by database.

    
asked by Kevin Martinez 08.11.2018 в 19:29
source

2 answers

1

It is important to make use of the LogCat to have more information about a problem in your application, another way sometimes the causes of closure can be diverse and sometimes you will waste too much time trying to solve the problem:

Write and view records with Logcat

An error that I notice at first glance is when creating the tables of your database, the definition of the table should not be next to the fields:

@Override
public void onCreate(SQLiteDatabase db) {
    //db.execSQL("CREATE TABLE t_emisiones(pais TEXT primary key, emision REAL);");
   db.execSQL("CREATE TABLE t_emisiones (pais TEXT primary key, emision REAL);");
}
    
answered by 08.11.2018 в 19:58
0

You can research the code blocks of " try catch " that will prevent your program from stopping, but beware of abusing them because it is difficult to debug the code afterwards. p>

try{
    SQLite_OpenHelper dbhelp = new SQLite_OpenHelper(this,"emisores_paises", null, 1);
    SQLiteDatabase db = dbhelp.getWritableDatabase();
    db.execSQL("INSERT INTO t_emisiones VALUES('colombia', 0.205)");
    db.execSQL("INSERT INTO t_emisiones VALUES('espana', 0.388)");
    db.execSQL("INSERT INTO t_emisiones VALUES('mexico', 0.582)");
}catch (Exception e){
   //Aqui muestra los errores
   //En caso de que tu programa no pueda ejecutar las lineas de arriba
   Log.d("tag", "El error es" + e);
}finally {
   //Finalmente cierra la conexión después de realizar tus tareas.
   db.close();
}

I leave more information on how to use the try catch link

But I remind you that this is only for your program to continue, this will only make it not sound.

    
answered by 08.11.2018 в 22:24