public class Cliente扩展了SQLiteOpenHelper {
public Cliente(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
private String query = "CREATE TABLE Clientes "+
"(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
" nombre TEXT, "+
" telefono TEXT, "+
" email TEXT )";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE Clientes");
db.execSQL(query);
}
}
公共类ClienteProvider扩展了ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.inkadroid.ejemplocp/Cliente");
//Nombres de columnas
public static final String COL_NOMBRE = "nombre";
public static final String COL_TELEFONO = "telefono";
public static final String COL_EMAIL = "email";
private Cliente db;
private static final String BD_NOMBRE = "BD_Ventas";
private static final int BD_VERSION = 1;
private static final String TABLA_CLIENTES = "Clientes";
//UriMatcher acceso a una tabla o registro especifico
private static final int CLIENTES = 1;
private static final int CLIENTES_ID = 2;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.inkadroid.ejmeplocp2", "Clientes", CLIENTES);
uriMatcher.addURI("com.inkadroid.ejmeplocp2", "Clientes/#", CLIENTES_ID);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
//Si es una consulta a un ID concreto construimos el WHERE
String where = selection;
Log.e("Select", where);
if(uriMatcher.match(uri) == CLIENTES_ID){
Log.e("URI", uri+"");
where = "_id=" + uri.getLastPathSegment();
}
SQLiteDatabase access= db.getWritableDatabase();
Cursor c = access.query(TABLA_CLIENTES, projection, where, selectionArgs, null, null,
sortOrder);
Log.e("TABLA_CLIENTES", TABLA_CLIENTES);
Log.e("where", where);
return c;
}
}
公共类MainActivity扩展了Activity {
String[] projection = new String[]{"codigo","nombre","telefono","email"};
String selection = "SELECT * FROM Clientes ;";
String[] selectionArgs = null;
String sortOrder = null;
ClienteProvider clienteProvider;
ContentResolver resolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cliente cliente = new Cliente(this, "BD_Ventas", null, 1);
SQLiteDatabase conexion = cliente.getWritableDatabase();
for(int i=0; i<10;i++){
conexion.execSQL("INSERT INTO Clientes (nombre,telefono,email) values ('cliente"+i+"','977915615','cliente@gmail.com')");
}
Cursor cursor = resolver.query(ClienteProvider.CONTENT_URI,projection,selection,selectionArgs,sortOrder);
while (cursor.moveToNext()) {
do {
String codigo= cursor.getString(0);
Log.e("Codigo", codigo);
String nombre= cursor.getString(1);
Log.e("nombre", nombre);
String telefono = cursor.getString(2);
Log.e("telefono", telefono);
String email = cursor.getString(3);
Log.e("email", email);
String inString = codigo + " " + nombre + " " + telefono +" " + email + " .";
Log.e("Sentencia", inString);
Toast.makeText(this, inString, Toast.LENGTH_LONG).show();
} while(cursor.moveToNext());
}
conexion.close();
}
}
01-02 02:01:56.710:E / AndroidRuntime(13717):java.lang.RuntimeException:无法启动活动ComponentInfo {com.inkadroid.ejemplocp / com.inkadroid.ejemplocp.MainActivity}:java.lang。的NullPointerException