1

I'm trying to insert and recover data from a table in SQLite in Android. But, I'm getting this error:

android.database.sqlite.SQLiteException: table produto has no column named cor (code 1): , while compiling: INSERT INTO produto(cor,tamanho,idproduto,material,descricao,valor,classificacao) VALUES (?,?,?,?,?,?,?)

Here is my "Produto" basecolumn:

public static abstract class ProdutosDAO implements BaseColumns {
    public final static String TABLE_NAME = "produto";
    public final static String COLUMN_NAME_ID_PRODUTO = "idproduto";
    public final static String COLUMN_NAME_DESCRICAO = "descricao";
    public final static String COLUMN_NAME_VALOR = "valor";
    public final static String COLUMN_NAME_CLASSIFICACAO = "classificacao";
    public final static String COLUMN_NAME_MATERIAL = "material";
    public final static String COLUMN_NAME_TAMANHO = "tamanho";
    public final static String COLUMN_NAME_COR = "cor";


    public final static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "+ ProdutosDAO.TABLE_NAME + " (" +
            ProdutosDAO._ID + PRIMARY_KEY +
            ProdutosDAO.COLUMN_NAME_ID_PRODUTO + INTEGER_TYPE + UNIQUE_INDEX + COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_DESCRICAO + VARCHAR_TYPE + "150)"+ COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_VALOR + DOUBLE_TYPE + COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_CLASSIFICACAO + INTEGER_TYPE  + COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_MATERIAL + INTEGER_TYPE +  COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_TAMANHO + INTEGER_TYPE  + COMMA_SEP +
            ProdutosDAO.COLUMN_NAME_COR + INTEGER_TYPE + ");";

    public final static String DROP_TABLE = "DROP TABLE IF EXISTS " + ProdutosDAO.TABLE_NAME;

    public final static String[] ALL_COLUMNS = {ProdutosDAO._ID,
        COLUMN_NAME_ID_PRODUTO,
        COLUMN_NAME_DESCRICAO,
        COLUMN_NAME_VALOR,
        COLUMN_NAME_CLASSIFICACAO,
        COLUMN_NAME_MATERIAL,
        COLUMN_NAME_TAMANHO,
        COLUMN_NAME_COR
    };

}

I have some static variables to types too:

// Types definition
public final static String VARCHAR_TYPE = " VARCHAR(";
public final static String INTEGER_TYPE = " INTEGER";
public final static String DOUBLE_TYPE = " DOUBLE";

To to create this table I'm just using:

db.execSQL(SisLojaBanco.ProdutosDAO.CREATE_TABLE);

Well, I'm completly lost, and don't know whats happening.

Can someone help me please?

Thanks a lot,

Mathias

I'm trying to insert values this way:

ContentValues values = new ContentValues();

    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_ID_PRODUTO, "1");
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_DESCRICAO, "'Barbel Titanio 2mm Preto Com Brilho'");
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_VALOR, 50.0);
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_CLASSIFICACAO, "4"); // Barbel
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_MATERIAL, "1"); // Titanio
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_TAMANHO, "1"); // 2mm
    values.put(SisLojaBanco.ProdutosDAO.COLUMN_NAME_COR, "3"); // Preto
    save(values);
mTheSame
  • 80
  • 1
  • 10
  • Insert values in same order as you have created the table with – Vivek Mishra Jan 16 '16 at 13:20
  • Tks for repling, but I'm using "insert" method from SQLiteDatabase to do this: `SQLiteDatabase db = helper.getWritableDatabase(); long newRow = db.insert(SisLojaBanco.UsuarioDAO.TABLE_NAME, SisLojaBanco.COLUMN_NAME_NULLABLE, values); return (newRow != 0);` – mTheSame Jan 16 '16 at 13:22
  • your order of columns in table creation and insertion is different. Check that – Vivek Mishra Jan 16 '16 at 13:24
  • 2
    If you've added that column after having run your app, you'll need to delete the database to allow it to be recreated with the new column. Either clear your app's data, or uninstall and reinstall. – Mike M. Jan 16 '16 at 13:26
  • @VivekMishra how can I manupulate the order using insert method? – mTheSame Jan 16 '16 at 13:31
  • @Mike M. Tks for repling, well, every time I ran app (in virtual device) I've deleted app. "/ – mTheSame Jan 16 '16 at 13:31
  • Well there is no varchar or double type in SQLite. The correct terminology is TEXT and REAL respectively – KoalaKoalified Jan 16 '16 at 13:42
  • You're missing a comma after `PRIMARY_KEY`. I'm not sure how that would affect the table creation, but you might fix that and retry. – Mike M. Jan 16 '16 at 13:43
  • @KoalaKoalified Those are still valid in SQLite. They get converted accordingly. https://www.sqlite.org/datatype3.html – Mike M. Jan 16 '16 at 13:46
  • @MikeM. I forgot to post PRIMARY_KEY declaration, but It's here: `public final static String PRIMARY_KEY = " INTEGER PRIMARY KEY, ";` and I chaged VARCHAR and DOUBLE types to TEXT and REAL, but nothing happened. – mTheSame Jan 16 '16 at 14:01
  • Hmm. What happens if you omit the `cor` column in the `ContentValues`? That is, if you remove the last `values.put()` line. – Mike M. Jan 16 '16 at 14:08
  • @MikeM. the error moves to the next column: `Error inserting tamanho=1 idproduto=1 material=1 descricao='Barbel Titanio 2mm Preto Com Brilho' valor=50.0 classificacao=4 android.database.sqlite.SQLiteException: table produto has no column named tamanho (code 1): , while compiling: INSERT INTO produto(tamanho,idproduto,material,descricao,valor,classificacao) VALUES (?,?,?,?,?,?)` – mTheSame Jan 16 '16 at 14:36

0 Answers0