資源描述:
《安卓開發(fā)簡要.docx》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、packagecom.example.test;importandroid.app.Activity;importandroid.content.ContentValues;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;im
2、portandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsActivity{//button創(chuàng)建數(shù)據(jù)庫
3、
4、button1添加數(shù)據(jù)
5、
6、button2更新數(shù)據(jù)
7、
8、button3刪除數(shù)據(jù)
9、
10、button4查詢數(shù)據(jù)privateMyDatabaseHelpermd;privateTextViewtv;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCre
11、ate(savedInstanceState);setContentView(R.layout.activity_main);Buttonbtn=(Button)findViewById(R.id.button);//創(chuàng)建數(shù)據(jù)庫Buttonbtn1=(Button)findViewById(R.id.button1);//添加數(shù)據(jù)Buttonbtn2=(Button)findViewById(R.id.button2);//更新數(shù)據(jù)Buttonbtn3=(Button)findViewById(R.id.button3);//刪除數(shù)據(jù)Buttonbtn4=(Button)findViewBy
12、Id(R.id.button4);//查詢數(shù)據(jù)tv=(TextView)findViewById(R.id.textView2);md=newMyDatabaseHelper(MainActivity.this,"book.db",null,1);btn.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){md.getWritableDatabase();Toast.makeText(MainActivity.this,"添加0",Toast.LENGTH_SHORT).show();}})
13、;btn1.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){SQLiteDatabasedb=md.getWritableDatabase();ContentValuesvalues=newContentValues();//類似于mapvalues.put("bname","ThismyfirstCode");values.put("authod","yangA");values.put("price",30.0);values.put("pages",3000);db.insert(
14、"book",null,values);//添加按鈕values.clear();values.put("bname","ThismysecondCode");values.put("authod","yangB");values.put("price",40.0);values.put("pages",1000);db.insert("book",null,values);Toast.makeText(MainActivity.this,"添加完畢",Toast.LENGTH_SHORT).show();}});btn2.setOnClickListener(newOnClickListe
15、ner(){@OverridepublicvoidonClick(Viewarg0){SQLiteDatabasedb=md.getWritableDatabase();ContentValuesvalues=newContentValues();values.put("price",90);db.update("book",values,"pages=?",newString[]{"1000"});Toas