Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kimossab.cp_tp3;
- //http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- public class GestorBDados
- {
- public static final String TABLEN = "Contactos";
- public static final String CRIARTABELA = "CREATE TABLE "+TABLEN+" (ID INTEGER PRIMARY KEY autoincrement, Nome TEXT, Contacto TEXT)";
- public boolean creado;
- //-------------------------------------
- private Context ref_contexto;
- private SQLiteDatabase database;
- private BDHelper HelpBD;
- public GestorBDados(Context con)
- {
- ref_contexto = con;
- HelpBD = new BDHelper(ref_contexto);
- database = HelpBD.getWritableDatabase();
- creado = HelpBD.creado;
- }
- public void FecharGestorBDados()
- {
- if (database != null && database.isOpen())
- database.close();
- }
- public void InsertBD(String nome, String cont)
- {
- ContentValues values = new ContentValues();
- values.put("Nome", nome);
- values.put("Contacto", cont);
- database.insert(TABLEN, null, values);
- }
- public int PesquisarNome(String nome)
- {
- String cmd = "SELECT Count(*) FROM "+TABLEN+" WHERE Nome LIKE '%"+nome+"%'";
- Log.i("Pesquisar",cmd);
- Cursor c = database.rawQuery(cmd, null);
- c.moveToFirst();
- int n = c.getInt(0);
- c.close();
- return n;
- }
- public void Update()
- {
- HelpBD.onUpgrade(database,1, 2);
- database = HelpBD.getWritableDatabase();
- }
- public void RemDB()
- {
- database.rawQuery("DROP TABLE "+TABLEN, null);
- creado = false;
- HelpBD.onCreate(database);
- }
- public void RemUmPal()
- {
- Cursor c;
- int n;
- c=database.rawQuery("SELECT Count(*) FROM "+TABLEN, null);
- c.moveToFirst();
- n = c.getInt(0);
- /*String cmd = "DELETE FROM "+TABLEN+" WHERE Nome LIKE '%Ca%'";
- database.rawQuery(cmd,null);*/
- database.delete(TABLEN,"Nome NOT LIKE '% %'",null);
- c=database.rawQuery("SELECT Count(*) FROM "+TABLEN, null);
- c.moveToFirst();
- n-=c.getInt(0);
- c.close();
- Toast.makeText(ref_contexto,"Removidas " + n + " entradas.",Toast.LENGTH_SHORT).show();
- }
- public ArrayList<String> GetNome(String nome)
- {
- ArrayList<String> lresultado = new ArrayList<>();
- String cmd = "SELECT * FROM Contactos WHERE Nome LIKE '" + nome + "'";
- Cursor c = database.rawQuery(cmd, null);
- c.moveToFirst();
- lresultado.add(c.getString(c.getColumnIndex("ID")));
- lresultado.add(c.getString(c.getColumnIndex("Nome")));
- lresultado.add(c.getString(c.getColumnIndex("Contacto")));
- c.close();
- return lresultado;
- }
- public void UpdateRow(int id, String nome, String num)
- {
- /* String cmd = "UPDATE "+TABLEN+" SET Nome = '"+nome+"', Contacto = '"+num+"' WHERE ID = "+id;
- Log.i("Update row", cmd);
- database.rawQuery(cmd, null);*/
- ContentValues values = new ContentValues();
- values.put("Nome", nome);
- values.put("Contacto", num);
- database.update(TABLEN,values,"ID = "+id,null);
- }
- public ArrayList<String> GetDados()
- {
- ArrayList<String> res = new ArrayList<>();
- String cmd = "SELECT * FROM Contactos";
- Cursor c = database.rawQuery(cmd, null);
- String aux;
- if (c.moveToFirst())
- do
- {
- res.add(c.getString(c.getColumnIndex("ID")) + " - "+ c.getString(c.getColumnIndex("Nome")));
- }while (c.moveToNext());
- c.close();
- return res;
- }
- public String GetNumber(int id)
- {
- String cmd = "SELECT Contacto FROM Contactos WHERE ID = "+id;
- Cursor c = database.rawQuery(cmd,null);
- c.moveToFirst();
- String x = c.getString(0);
- c.close();
- return x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment