Skip to content
Advertisement

Problems with insertions after adding new tables to database

UPDATE:

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
    if (oldVersion < 3) {
        db?.execSQL(ARTIGO_TABLE_NAME);
    }
    if (oldVersion < 3) {
        db?.execSQL(PAIS_TABLE_NAME);
        db?.execSQL("INSERT INTO $PAIS_TABLE_NAME values('PT','Portugal')")
    }
    if (oldVersion < 3) {
        db?.execSQL(POSTAL_TABLE_NAME);
    }

}

I was trying to add new tables recently, but after adding those new tables the insertion in my old table is no longer working.

I already cleared my DB and reinstall the app, but when I try to insert the new data (client) into my old table it isn’t working and doesn’t appear any error message.

The DB code (client table is my first table , the others are the new tables):

override fun onCreate(db: SQLiteDatabase?) {
    val CREATE_CLIENTES_TABLE = ("CREATE TABLE IF NOT EXISTS $CLIENTES_TABLE_NAME (" +
            "$DB_Cliente_Codigo  INTEGER UNIQUE PRIMARY KEY," +
            "$DB_Cliente_Nome VARCHAR(40)," +
            "$DB_Cliente_Morada VARCHAR(40)," +
            "$DB_Cliente_Localidade VARCHAR(30)," +
            "$DB_Cliente_Postal VARCHAR(8)," +
            "$DB_Cliente_Contribuinte VARCHAR(15)," +
            "$DB_Cliente_Telefone VARCHAR(15)," +
            "$DB_Cliente_Pais VARCHAR(3))")

    val CREATE_ARTIGO_TABLE = ("CREATE TABLE IF NOT EXISTS $ARTIGO_TABLE_NAME (" +
            "$DB_ARTIGO_CODIGO INTEGER UNIQUE PRIMARY KEY," +
            "$DB_ARTIGO_ABREVIADO VARCHAR (20), " +
            "$DB_ARTIGO_EXTENSO VARCHAR (45), " +
            "$DB_ARTIGO_FAMILIA INTEGER (3)," +
            "$DB_ARTIGO_IVA INTEGER (2)," +
            "$DB_ARTIGO_PRECO INTEGER," +
            "$DB_ARTIGO_PRECO_2 INTEGER," +
            "$DB_ARTIGO_PRECO_3 INTEGER )")

    val CREATE_PAIS_TABLE = ("CREATE TABLE IF NOT EXISTS $PAIS_TABLE_NAME (" +
            "$DB_ABR_PAIS  VARCHAR (3)," +
            "$DB_DESC_PAIS VARCHAR)")

    val CREATE_POSTAL_TABLE = ("CREATE TABLE IF NOT EXISTS $POSTAL_TABLE_NAME (" +
            "$DB_CODIGO_POSTAL VARCHAR (8)," +
            "$DB_DESCRICAO_POSTAL VARCHAR (30))")

    db?.execSQL(CREATE_CLIENTES_TABLE)
    db?.execSQL(CREATE_POSTAL_TABLE)
    db?.execSQL(CREATE_PAIS_TABLE)
    db?.execSQL(CREATE_ARTIGO_TABLE)
    db?.execSQL("INSERT INTO $PAIS_TABLE_NAME values('PT','Portugal')")

I didn’t changed my method to addclient, it’s the same and was working perfectly

fun addClient(mCtx: Context, clientes: clientes) {
    val values = ContentValues()
    values.put(DB_Cliente_Codigo, clientes.codigo_cliente)
    values.put(DB_Cliente_Nome, clientes.nome_cliente)
    values.put(DB_Cliente_Morada, clientes.morada_cliente)
    values.put(DB_Cliente_Localidade, clientes.localidade_cliente)
    values.put(DB_Cliente_Postal, clientes.postal_cliente)
    values.put(DB_Cliente_Contribuinte, clientes.contribuinte_cliente)
    values.put(DB_Cliente_Telefone, clientes.telefone_cliente)
    values.put(DB_Cliente_Pais, clientes.pais_cliente)
    val db = this.writableDatabase

    try {
        db.insert(CLIENTES_TABLE_NAME, null, values)
        Toast.makeText(mCtx, "Cliente Adicionado", Toast.LENGTH_SHORT).show()
    } catch (e: Exception) {
        Toast.makeText(mCtx, e.message, Toast.LENGTH_SHORT).show()

    }
    db.close()
}

Advertisement

Answer

Sometimes it happens when you change your DB schema and do not update the version. let’s say you first created the DB and passed the version 1. if you change your DB next time you must update your version from 1 to 2. try this and let me know if it works.

8 People found this is helpful
Advertisement