- packagecom.wansha;
- importandroid.app.Activity;
- importandroid.content.ContentValues;
- importandroid.database.Cursor;
- importandroid.database.sqlite.sqliteDatabase;
- importandroid.database.sqlite.sqliteOpenHelper;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importcom.wansha.db.DatabaseHelper;
- publicclassActivity12extendsActivity{
- privateButtoncreateDatabase;
- privateButtonupdateDatabase;
- privateButtoninsert;
- privateButtonupdate;
- privateButtonread;
- privateButtondelete;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- voidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.createDatabase=(Button)this.findViewById(R.id.createDatabase);
- this.createDatabase.setOnClickListener(newCreateDatabaseListener());
- this.updateDatabase=(Button)this.findViewById(R.id.updateDatabase);
- this.updateDatabase.setOnClickListener(newUpdateDatabaseListener());
- this.insert=(Button)this.findViewById(R.id.insert);
- this.insert.setOnClickListener(newInsertListener());
- this.update=(Button)this.findViewById(R.id.update);
- this.update.setOnClickListener(newUpdateListener());
- this.read=(Button)this.findViewById(R.id.read);
- this.read.setOnClickListener(newReadListener());
- this.delete=(Button)this.findViewById(R.id.delete);
- this.delete.setOnClickListener(newDeleteListener());
- }
- classCreateDatabaseListenerimplementsOnClickListener{
- voidonClick(Viewv){
- sqliteOpenHelpersqlite=newDatabaseHelper(Activity12.this,"sharp");
- sqlite.getReadableDatabase();
- }
- classUpdateDatabaseListenerimplementsOnClickListener{
- @Override
- voidonClick(Viewv){
- System.out.println("UpdateDatabaseListenerisinvoked!!!");
- "sharp",2);
- classInsertListener sqliteOpenHelpersqlite="sharp");
- sqliteDatabasedatabase=sqlite.getWritableDatabase();
- ContentValuesvalues=newContentValues();
- values.put("username","xiaopeng");
- database.insert("sharp",null,values);
- classUpdateListener"pengpeng");
- database.update("sharp",values,"id=?",153); background-color:inherit; font-weight:bold">newString[]{"2"});
- classReadListener Cursorcursor=database.query("sharp",153); background-color:inherit; font-weight:bold">newString[]{"id","username"},153); background-color:inherit; font-weight:bold">null);
- while(cursor.moveToNext()){
- Stringusername=cursor.getString(cursor.getColumnIndex("username"));
- System.out.println("username---->"+username);
- classDeleteListener database.delete("sharp",153); background-color:inherit; font-weight:bold">newString[]{"1"});
- }
main.xml
[html]
?