Back to project page SQLite.
The source code is released under:
GNU General Public License
If you think the Android project SQLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bugsoft.erwin.sqlite1.BD; /*from w w w .ja v a 2 s .c o m*/ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; public class ConPers extends SQLiteOpenHelper { public String TableName = "personas"; public ConPers(Context context) { super(context,"mibase", null, 1); } @Override public void onCreate(SQLiteDatabase db) { String crearTabla = "CREATE TABLE "+TableName+" (" + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " + "nombre TEXT DEFAULT NULL, " + "appat TEXT DEFAULT NULL, " + "apmat TEXT DEFAULT NULL, " + "edad INTEGER(3) DEFAULT NULL, " + "sexo TEXT DEFAULT NULL, " + "cel TEXT DEFAULT NULL, " + "dir TEXT DEFAULT NULL, " + "carro TEXT DEFAULT NULL" + ")"; db.execSQL(crearTabla); ContentValues persona1 = new ContentValues(); persona1.put("nombre","Erwin"); persona1.put("appat","De La Luz"); persona1.put("apmat","De Leon"); persona1.put("edad",23); persona1.put("sexo","Masculino"); persona1.put("cel","5538879526"); persona1.put("dir","1 Norte"); persona1.put("carro","true"); ContentValues persona2 = new ContentValues(); persona2.put("nombre","Andrea"); persona2.put("appat","Bell"); persona2.put("apmat","Sun"); persona2.put("edad",22); persona2.put("sexo","Femenino"); persona2.put("cel","5547592634"); persona2.put("dir","23 Sur"); persona2.put("carro","false"); db.insert(TableName,null,persona1); db.insert(TableName,null,persona2); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("Drop table if exist " + TableName); onCreate(db); } public long insertar(PersonaGetSet pPersona){ long res = 0; ContentValues valores = new ContentValues(); valores.put("nombre",pPersona.getNombre()); valores.put("appat",pPersona.getAppat()); valores.put("apmat",pPersona.getApmat()); valores.put("edad",pPersona.getEdad()); valores.put("sexo",pPersona.getSexo()); valores.put("cel",pPersona.getCel()); valores.put("dir",pPersona.getDirec()); valores.put("carro",pPersona.getCarro()); res = this.getWritableDatabase().insert(TableName, null, valores); return res; } public ArrayList<String> Obtiene(){ ArrayList<String> lista = new ArrayList<String>(); lista.add("id"); lista.add("Nombre"); String[] columnas = new String[]{"id","nombre"}; Cursor c = this.getReadableDatabase().query(TableName,columnas,null,null,null,null,"id"); int id= c.getColumnIndex("id"); int nombre = c.getColumnIndex("nombre"); for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ lista.add(String.valueOf(c.getInt(id))); lista.add(c.getString(nombre)); } return lista; } public ArrayList<String> BusquedaObtiene(String palabra){ ArrayList<String> lista = new ArrayList<String>(); lista.add("id"); lista.add("Nombre"); Cursor c = this.getReadableDatabase().rawQuery("select id, nombre from "+TableName+" where nombre like '%"+palabra+"%' or appat like '%"+palabra+"%' or apmat like '%"+palabra+"%' ",null); int id= c.getColumnIndex("id"); int nombre = c.getColumnIndex("nombre"); for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ lista.add(String.valueOf(c.getInt(id))); lista.add(c.getString(nombre)); } return lista; } public PersonaGetSet PerActual(String idper){ String[] columnas= new String[]{"nombre","appat","apmat","edad","sexo","cel","dir","carro"}; String[] argumentos = new String[]{idper}; Cursor c = this.getReadableDatabase().query(TableName,columnas,"id=?",argumentos,null,null,null,null); PersonaGetSet pPersona = new PersonaGetSet(); c.moveToFirst(); pPersona.setNombre(c.getString(c.getColumnIndex("nombre"))); pPersona.setAppat(c.getString(c.getColumnIndex("appat"))); pPersona.setApmat(c.getString(c.getColumnIndex("apmat"))); pPersona.setEdad(Integer.valueOf(c.getString(c.getColumnIndex("edad")))); pPersona.setSexo(c.getString(c.getColumnIndex("sexo"))); pPersona.setCel(c.getString(c.getColumnIndex("cel"))); pPersona.setDirec(c.getString(c.getColumnIndex("dir"))); pPersona.setCarro(c.getString(c.getColumnIndex("carro"))); return pPersona; } public int Elimina(String id){ int res = 0; String[] argumentos = new String[]{id}; res = this.getWritableDatabase().delete(TableName,"id=?",argumentos); return res; } public int Actualizar(PersonaGetSet pPersona){ int res = 0; ContentValues contenedor = new ContentValues(); contenedor.put("nombre",pPersona.getNombre()); contenedor.put("appat",pPersona.getAppat()); contenedor.put("apmat",pPersona.getApmat()); contenedor.put("edad",pPersona.getEdad()); contenedor.put("sexo",pPersona.getSexo()); contenedor.put("cel",pPersona.getCel()); contenedor.put("dir",pPersona.getDirec()); contenedor.put("carro",pPersona.getCarro()); String id = pPersona.getId(); String[] argumentos = new String[]{id}; res = this.getWritableDatabase().update(TableName,contenedor,"id=?",argumentos); return res; } }