· 5 years ago · Mar 31, 2020, 10:00 AM
1package com.zezoca.listadetarefas.helper;
2
3import android.content.Context;
4import android.database.sqlite.SQLiteDatabase;
5import android.database.sqlite.SQLiteOpenHelper;
6import android.os.Build;
7import android.util.Log;
8
9import androidx.annotation.Nullable;
10
11public class DbHelper extends SQLiteOpenHelper {
12
13 public static int VERSION = 1;
14 public static String NOME_DB = "DB_notas";
15 public static String TABELA_NOTAS = "notas";
16
17 public DbHelper(@Nullable Context context) {
18
19 super(context, NOME_DB, null, VERSION);
20 }
21
22 @Override
23 public void onCreate(SQLiteDatabase db) {
24 String sql = "CREATE TABLE IF NOT EXISTS " + TABELA_NOTAS
25 + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
26 "nome TEXT NOT NULL );";
27
28 try {
29 db.execSQL(sql);
30 Log.i("INFO DB", "Sucesso ao criar a tabela");
31 } catch (Exception e) {
32 Log.i("INFO DB", "Erro ao criar a tabela" + e.getMessage());
33 }
34 }
35
36 @Override
37 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
38
39 String sql = "DROP TABLE IF EXISTS " + TABELA_NOTAS + ";" ;
40
41 try {
42 db.execSQL(sql);
43 onCreate(db);
44 Log.i("INFO DB", "Sucesso ao atualizar App");
45 } catch (Exception e) {
46 Log.i("INFO DB", "Erro ao atualizar App" + e.getMessage());
47 }
48 }
49}