Error in such table. Sqlite

Asked

Viewed 9,169 times

0

You’re making this mistake, I wonder if my DAO is right.

Error:

08-06 14:19:53.304: E/SQLiteLog(16893): (1) no such table: proposta
08-06 14:19:53.334: E/SQLiteDatabase(16893): Error inserting validadebrinde=x validade=cd idusuario=1 brinde=c 
08-06 14:19:53.334: E/SQLiteDatabase(16893): android.database.sqlite.SQLiteException: no such table: proposta (code 1): , while compiling: INSERT INTO proposta(validadebrinde,validade,idusuario,brinde) VALUES (?,?,?,?)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at br.com.android.controledevisitas.dao.PropostaDAO.cadastrar(PropostaDAO.java:43)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at br.com.android.controledevisitas.view.CadastrarPropostaActivity.cadastrarProposta(CadastrarPropostaActivity.java:122)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at br.com.android.controledevisitas.view.CadastrarPropostaActivity$2.onClick(CadastrarPropostaActivity.java:78)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.view.View.performClick(View.java:4091)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.view.View$PerformClick.run(View.java:17036)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.os.Handler.handleCallback(Handler.java:615)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.os.Looper.loop(Looper.java:137)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at android.app.ActivityThread.main(ActivityThread.java:5031)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-06 14:19:53.334: E/SQLiteDatabase(16893):    at dalvik.system.NativeStart.main(Native Method)

My DAO:

package br.com.android.controledevisitas.dao;

import br.com.android.controledevisitas.model.Proposta;
import br.com.android.controledevisitas.util.Dados;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PropostaDAO extends SQLiteOpenHelper {
    private static final String TABELA = "proposta";
    private static final Dados dados = new Dados();

    public PropostaDAO(Context ctx) {
        super(ctx, dados.DATABASE, null, dados.VERSAO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table "
                + TABELA
                + "(id integer primary key, idusuario integer, validade text, brinde text, validadebrinde text);";
        db.execSQL(sql);

    }

    public void onUpgrade(SQLiteDatabase db, int versaoAntiga, int versaoNova) {
        String sql = "drop table if exists " + TABELA;
        db.execSQL(sql);
        onCreate(db);
    }

    public Boolean cadastrar(Proposta p) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("idusuario", 1);
            cv.put("validade", p.getValidade());
            cv.put("brinde", p.getBrinde());
            cv.put("validadebrinde", p.getValidadeBrinde());

            getWritableDatabase().insert(TABELA, null, cv);
            return true;
        } catch (Exception e) {
            Log.e("DAO", e.getMessage());
            return false;
        }
    }

    public int getUltimoId() {
        Cursor c = getReadableDatabase().rawQuery(
                "select max(id) from " + TABELA, null);
        if (c.moveToNext()) {
            return c.getInt(c.getColumnIndex("id"));
        }
        return 0;
    }


}
  • 1

    You’re making a mistake in the INSERT INTO proposta but in your code you have not shown us any Insert. Your mistake is for sure that you are not doing create before doing Insert.

  • My input here getWritableDatabase(). Insert(TABLE, null, cv);

  • That’s probably the problem, but because he’s not calling onCreate() when I urge the DAO class

  • This is a good question. Try debugging your program and see if the onCreate method is running even or not. Are you not making an exception when trying to create?

  • I debugged, he is not being called, but I do not understand, because I installed the right class, was to be running onCreate(). Instanciei assim -> Propostadao pDao = new Propostadao(this);

  • I think I was mistaken in the previous comment. Try uninstalling the app from your emulator or mobile phone, then try running it again, so I read onCreate() is only called the first time the app runs, which is when your bd is effectively created. Or if you prefer, inform that there is a new version of BD to force the onUpgrade method to be called

  • meisterx7, solved your problem?

Show 2 more comments

2 answers

1

  • Checks the data.DATABASE and data method.VERSION is not empty or null.
public PropostaDAO(Context ctx)
{
        super(ctx, dados.DATABASE, null, dados.VERSAO);
}
  • Then check how these initialize the Proposed object.
PropostaDAO p=new PropostaDAO(context);
p.cadastrar(proposta);

0

Apparently Sqlite did not find the table "proposed" check if the database was created correctly and if the name is the same in the code and in Sqlite.

  • as I check if it was created, from what I see it seems that it was not created even, I wondered why

  • Which IDE are you using? If it is Eclipse you can try to access the data/data/package name_name directory. In this path you have access to the database. To access by eclipse go to Window > Show View > Other > Android > File Explorer. If the folder is blocked, please try to access: http://www.programarandroid.com.br/2013/12/dica-comontry

  • It was not created, the onCreate da dao was not called, but I instate the right class, at least I think so. Propostadao pDao = new Propostadao(this);

  • The ideal for the database creation classes is to run the every time application starts. I believe it is a standard for everyone to create a class that extends android.database.sqlite.Sqliteopenhelper overwriting the methods onCreate and onUpdate. The call of the methods is controlled by Android itself. In this case it will run a single time. The Sqliteopenhelper constructor receives an integer which means the version of the database, when this version changes the onUpdate method is executed. Check that it has been implemented correctly.

Browser other questions tagged

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