Created
November 24, 2018 18:24
-
-
Save erick-jeronimo/4bd9293bb373accc1daa3c4f1d136d7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.exemplosqlite; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
public class BD { | |
private SQLiteDatabase bd; | |
public BD(Context context){ | |
BDCore auxBd = new BDCore(context); | |
bd = auxBd.getWritableDatabase(); | |
} | |
public void inserir(Usuario usuario){ | |
ContentValues valores = new ContentValues(); | |
valores.put("nome", usuario.getNome()); | |
valores.put("email", usuario.getEmail()); | |
valores.put("senha", usuario.getSenha()); | |
bd.insert("usuario", null, valores); | |
} | |
public void atualizar(Usuario usuario){ | |
ContentValues valores = new ContentValues(); | |
valores.put("nome", usuario.getNome()); | |
valores.put("email", usuario.getEmail()); | |
bd.update("usuario", valores, "_id = ?", new String[]{""+usuario.getId()}); | |
} | |
public void deletar(Usuario usuario){ | |
bd.delete("usuario", "_id = "+usuario.getId(), null); | |
} | |
public List<Usuario> buscar(){ | |
List<Usuario> list = new ArrayList<Usuario>(); | |
String[] colunas = new String[]{"_id", "nome", "email"}; | |
Cursor cursor = bd.query("usuario", colunas, null, null, null, null, "nome ASC"); | |
if(cursor.getCount() > 0){ | |
cursor.moveToFirst(); | |
do{ | |
Usuario u = new Usuario(); | |
u.setId(cursor.getLong(0)); | |
u.setNome(cursor.getString(1)); | |
u.setEmail(cursor.getString(2)); | |
list.add(u); | |
}while(cursor.moveToNext()); | |
} | |
return(list); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.exemplosqlite; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class BDCore extends SQLiteOpenHelper { | |
private static final String NOME_BD = "teste"; | |
private static final int VERSAO_BD = 7; | |
public BDCore(Context ctx){ | |
super(ctx, NOME_BD, null, VERSAO_BD); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase bd) { | |
bd.execSQL("create table usuario(_id integer primary key autoincrement, nome text not null, email text not null, senha text not null);"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase bd, int arg1, int arg2) { | |
bd.execSQL("drop table usuario;"); | |
onCreate(bd); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment