Problem with update to SQLite database from Android

0

Good morning. I have a problem updating data in a SQLITE database. I put the necessary code:

Variables:

public static final String TABLE_INVENTARIO_NAME = "inventario";
public static final String INV_NOMBRE="inv_nombre";
public static final String INV_TIPO="inv_tipo";
public static final String INV_NUMERO="inv_numero";
public static final String INV_IMAGEN="inv_imagen";

Creation of the database:

db.execSQL("CREATE TABLE " + TABLE_INVENTARIO_NAME + " ("
                +"ID INTEGER PRIMARY KEY NOT NULL,"
                +INV_NUMERO + "INTEGER NOT NULL,"
                +INV_TIPO + "INTEGER NOT NULL,"
                +INV_NOMBRE + "TEXT NOT NULL,"
                +INV_IMAGEN + "INTEGER NOT NULL)");

Creating an initial record:

ContentValues registroInventario = new ContentValues();
registroInventario.put("ID",1);
registroInventario.put(AccesoDB.INV_NOMBRE,inv1_nombre);
registroInventario.put(AccesoDB.INV_TIPO,inv1_tipo);
registroInventario.put(AccesoDB.INV_NUMERO,inv1_numero);
registroInventario.put(AccesoDB.INV_IMAGEN,inv1_imagen);
bd.insert(AccesoDB.TABLE_INVENTARIO_NAME,null,registroInventario);

And the method to update:

private void actualizarInventario(Context context,String valorNombre, int valorImagen, int valorTipo, int valorNumero, int id){
    acceso = new AccesoDB(context);
    bd = acceso.getWritableDatabase();
    ContentValues valores = new ContentValues();
    valores.put(AccesoDB.INV_NOMBRE,valorNombre);
    valores.put(AccesoDB.INV_TIPO,valorTipo);
    valores.put(AccesoDB.INV_NUMERO,valorNumero);
    valores.put(AccesoDB.INV_IMAGEN,valorImagen);
    int up=bd.update(AccesoDB.TABLE_INVENTARIO_NAME,valores,"ID="+id,null);
    if(up!=0){
        Toast.makeText(context,"Se ha añadido",Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(context,"Fallo",Toast.LENGTH_SHORT).show();
    }
    //Cerramos la BD
        bd.close();
    }

And the error that gives me is this:

android.database.sqlite.SQLiteException: no such column: inv_numero (code 1): , while compiling: UPDATE inventario SET inv_numero=?,inv_tipo=?,inv_nombre=?,inv_imagen=? WHERE ID=1

If I try to launch a Select it gives me a similar error:

android.database.sqlite.SQLiteException: no such column: inv_nombre (code 1): , while compiling: SELECT inv_nombre FROM inventario WHERE ID=1

I suppose there will be some problem or when creating the table or filling in the data, but I do not give with the key.

I've tried adding simple quotes to the ID but it does not work either. Any idea? Thanks and best regards.

    
asked by Frandalf 01.09.2017 в 17:48
source

2 answers

2

When you create the inventory table, you are not adding a space to the names of the columns.

For example:

  +INV_NUMERO + "INTEGER NOT NULL,"

The result would be:

"inv_tipoINTEGER NOT NULL"

So it fails to create the table. Give it a space:

db.execSQL("CREATE TABLE " + TABLE_INVENTARIO_NAME + " ("
                +"ID INTEGER PRIMARY KEY NOT NULL,"
                +INV_NUMERO + " INTEGER NOT NULL,"  // espacio al princio
                +INV_TIPO + " INTEGER NOT NULL,"  // espacio al princio
                +INV_NOMBRE + " TEXT NOT NULL," // espacio al princio
                +INV_IMAGEN + " INTEGER NOT NULL)"); // espacio al princio
    
answered by 01.09.2017 / 17:54
source
-1

The problem is precisely the one detailed in the message:

  

SQLiteException: no such column: inv_number (code 1):

Indicates that the column does not exist: inv_numero

But the problem is your query, add spaces so that the name of the field and the type of data are not left together, it is forming like this:

  

"inv_numeroINTEGER NOT NULL," .....

should be:

 db.execSQL("CREATE TABLE " + TABLE_INVENTARIO_NAME + " ("
                +"ID INTEGER PRIMARY KEY NOT NULL,"
                +INV_NUMERO + " INTEGER NOT NULL,"
                +INV_TIPO + " INTEGER NOT NULL,"
                +INV_NOMBRE + " TEXT NOT NULL,"
                +INV_IMAGEN + " INTEGER NOT NULL)");

In android, as you already created a structure of the table, it is important to correct the query to create the table and then delete the application data or delete it and add a new version again because the onUpgrade() method will not be called unless you change the versionCode .

    
answered by 01.09.2017 в 17:54