Contenido sobre Android
Buscar
Social
Ofertas laborales ES

Foro sobre Android > aplicacion sqlite 2

//donde ocupo metodos explicacion con comentarios
package cl.rq.chile;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidBd extends Activity{
TextView text;
UsuariosSQLiteHelper uss;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.sentencia);
//ABRIR BASE DE DATOS USUARIOS EN MODO DE ESCRITURA
UsuariosSQLiteHelper us = new UsuariosSQLiteHelper(this, "DBUsuarios", null , 1);
SQLiteDatabase db = us.getWritableDatabase();

List<String> list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
//SI SE A ABIERTO CORRECTAMENTE LA BD
if (db != null)
{
//INSERTAR 5 USUARIOS
/*for(int i=0;i<=5;i++){
int codigo = i;
String nombre = "Usuario" + i;*/

//INSERTAR DATOS
//db.execSQL("INSERT INTO Usuarios(codigo,nombre)"+"VALUES(" + codigo + ",'" + nombre + "')");
//}
//ACTUALIZAR REGISTRO
//db.execSQL("UPDATE Usuarios set nombre = 'rodrigo' WHERE codigo = 4");

//ELIMINAR REGISTROS
//db.execSQL("DELETE FROM Usuarios WHERE codigo<=3");


//OTRA MANERA DE INSERTAR
/*ContentValues nRegistro = new ContentValues();
nRegistro.put("codigo", 6);
nRegistro.put("nombre", "marcelo rios");

db.insert("Usuarios", null, nRegistro);*/

//OTRA MANERA DE ACTUALIZAR
/*ContentValues valor = new ContentValues();
valor.put("nombre", "rodrigo");
db.update("Usuarios", valor,"nombre='roger'", null);
*/

//OTRA MANERA DE BORRAR
//db.delete("Usuarios", "nombre='marcelo rios'" ,null);

//Eliminar un registro con execSQL(), utilizando argumentos
/*String[] args = new String[]{"usu1"};
db.execSQL("DELETE FROM Usuarios WHERE usuario=?", args);*/

//Actualizar dos registros con update(), utilizando argumentos
/*ContentValues valores = new ContentValues();
valores.put("nombre","ciisa");
String[] args = new String[]{"rodrigo", "Usuario5"};
db.update("Usuarios", valores, "nombre=? OR nombre=?", args);*/

int i=0;
//String []campos = new String[]{"codigo","nombre"};
//String []args = new String[]{"ciisa"};

//PARA MOSTRAR TODOS LOS DATOS
Cursor c = db.query("Usuarios", new String[]{"codigo,nombre"}, null, null,null,null,"nombre desc");

//PARA HACER CONSULTA
//Cursor c = db.rawQuery("SELECT codigo, nombre FROM Usuarios WHERE codigo = 6",null);
if (c.moveToFirst())
{

do{
i++;
int codigo = c.getInt(0);
String nombre = c.getString(1);
String cod = String.valueOf(codigo);
list.add(cod + nombre);

}while(c.moveToNext());
if (c != null && !c.isClosed()) {
c.close();
}
}
for(String name:list){
sb.append(name);
sb.append("\n");
}
db.close();

}
text.setText(sb.toString());
}


}

//usuariossqlitehelper
package cl.rq.chile;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class UsuariosSQLiteHelper extends SQLiteOpenHelper {

//SENTENCIA SQL PARA CREAR LA TABLA USUARIOS
String sqlCreate = "CREATE TABLE Usuarios (codigo INTEGER , nombre TEXT)";
public UsuariosSQLiteHelper(Context contexto, String nombre,CursorFactory factory, int version) {
super(contexto, nombre, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sqlCreate);
}

@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
// TODO Auto-generated method stub
//SE ELIMINA LA VERSION ANTERIOR DE LA TABLA
db.execSQL("DROP TABLE IF EXISTS Usuarios");
//SE CREA LA NUEVA VERSION DE LA TABLA
db.execSQL(sqlCreate);

}

}


noviembre 11, 2011 | Unregistered Commenterrquiroz