Advertisement
RedNexX

DBHelper.java

Mar 31st, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. package com.zezoca.listadetarefas.helper;
  2.  
  3. import android.content.Context;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.os.Build;
  7. import android.util.Log;
  8.  
  9. import androidx.annotation.Nullable;
  10.  
  11. public 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement