SQLite Android Studio

6 respostas Resolvido
E

Gente, não estou conseguindo criar um banco para minha aplicação.

O que pode estar faltando? obs: já tentei trocar a versão do bd e de nada adiantou.

no momento do findAll dá esse erro:

06-10 13:31:15.870 2222-2222/com.leandro.trabalhofinal E/SQLiteLog: (1) no such table: tarefa

Segue as classes:

public class DatabaseHelper extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "trabalho.db";
private Context context;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE tarefa (_id INTEGER NOT NULL PRIMARY KEY, titulo TEXT NOT NULL, descricao TEXT NOT NULL, pomodoros INTEGER NOT NULL)");
}

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

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private Button mMainButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final TarefaDao tarefaDao = new TarefaDao(this);

    List<Tarefa> tarefas = tarefaDao.findAll();
    tarefas.add(new Tarefa(1,"Titulo teste", "Descrição Teste", 4,R.mipmap.ic_launcher));

    MyAdapter adapter = new MyAdapter(this, tarefas);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    recyclerView = (RecyclerView) findViewById(R.id.main_recyclerview);
    recyclerView.setLayoutManager(llm);
    recyclerView.setAdapter(adapter);

    mMainButton = (Button) findViewById(R.id.botao_nova_tarefa);
    mMainButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent it = new Intent(MainActivity.this, CadastroActivity.class);
            startActivity(it);
        }
    });


    }
}

6 Respostas

L

Olá Eiro,

Desinstala o APP antes de rodar ele de novo, é um “bug” de reconhecimento das tabelas novas.

Abraços.

E

vc diz reinstalar o android studio?

L

Não, você está testando sua aplicação direto no device?

Desinstala o app do device, antes de rodar ele de novo.

E

não. eu estou executando direto pelo android studio usando um emulador.

eu tenho outra versão do studio bem antiga no pc, será que está interferindo?

E
Solucao aceita

Era algum bug de criação de tabela mesmo, reinstalei tudo e funcionou

P

Ao adicionar uma tabela nova você precisa mudar a versão do DATABASE_VERSION, assim o SQL Lite reconhece que precisa criar uma tabela nova.

Criado 10 de junho de 2016
Ultima resposta 23 de jun. de 2016
Respostas 6
Participantes 3