您好,欢迎访问三七文档
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;importandroid.widget.Button;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsActivity{//button创建数据库||button1添加数据||button2更新数据||button3删除数据||button4查询数据privateMyDatabaseHelpermd;privateTextViewtv;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Buttonbtn=(Button)findViewById(R.id.button);//创建数据库Buttonbtn1=(Button)findViewById(R.id.button1);//添加数据Buttonbtn2=(Button)findViewById(R.id.button2);//更新数据Buttonbtn3=(Button)findViewById(R.id.button3);//删除数据Buttonbtn4=(Button)findViewById(R.id.button4);//查询数据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();}});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(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(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){SQLiteDatabasedb=md.getWritableDatabase();ContentValuesvalues=newContentValues();values.put(price,90);db.update(book,values,pages=?,newString[]{1000});Toast.makeText(MainActivity.this,更新成功,Toast.LENGTH_SHORT).show();}});btn3.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){SQLiteDatabasedb=md.getWritableDatabase();db.delete(book,price=?,newString[]{40.0});Toast.makeText(MainActivity.this,删除成功,Toast.LENGTH_SHORT).show();}});btn4.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewarg0){SQLiteDatabasedb=md.getWritableDatabase();Stringsrc=;Cursorcursor=db.query(book,null,null,null,null,null,null);if(cursor.moveToFirst()){do{//遍历Cursor对象,取出数据打印Stringname=cursor.getString(cursor.getColumnIndex(bname));Stringauthor=cursor.getString(cursor.getColumnIndex(authod));intprice=cursor.getInt(cursor.getColumnIndex(price));intpages=cursor.getInt(cursor.getColumnIndex(pages));src+=name;Log.d(MainActivity,booknameis+name);Log.d(MainActivity,bookauthoris+author);Log.d(MainActivity,bookpagesis+pages);Log.d(MainActivity,bookpriceis+price);}while(cursor.moveToNext());}Log.d(MainActivity,src);cursor.close();tv.setText(src);}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}}、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、packagecom.example.test;importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteDatabase.CursorFactory;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.widget.Toast;publicclassMyDatabaseHelperextendsSQLiteOpenHelper{publicstaticfinalStringCREATE_BOOK=createtableBook(+idintegerprimarykeyautoincrement,+bnametext,+authodtext,+pricereal,+pagesinteger);privateContextmContext;publicMyDatabaseHelper(Contextcontext,Stringname,CursorFactoryfactory,intversion){super(context,name,factory,version);this.mContext=context;}@OverridepublicvoidonCreate(SQLiteDatabasedb){System.out.println(CREATE_BOOK);db.execSQL(CREATE_BOOK);//创建成功后提示Toast.makeText(mContext,数据库和数据库表创建成功,Toast.LENGTH_SHORT).show();}@OverridepublicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){}}publicStringstart(){Stringsrc=;//MySQLiteOpenHelperdbHelper=new//MySQLiteOpenHelper(MainActivity.this,//test_carson);//SQLiteDatabasesqliteDatabase=dbHelper.getWritableDatabase();//Cursorcursor=sqliteDatabase.query(user,newString[]{id,//name},id=?,newString[]{1},null,null,null);////Stringid=null;//Stringname=null;//////将光标移动到下一行,从而判断该结果集是否还有下一条数据////如果有则返回true,没有则返回false//while(cursor.moveToNext()){//id=cursor.getString(cursor.getColumnIndex(id));//name=cursor.getString(cursor.getColumnIndex(name));////输出查询结果//System.out.println(查询到的数据是:+id:+id++name://+name);////}////关闭数据库//sqliteDatabase.close();StringabsolutePath;DBUtildb=newDBUtil(MainActivity.this);try{absolutePath=getApplicationContext().getDatabasePath(data.db).getAbsolutePath();}catch(Exceptione){absolutePath=getApplicationContext().getDatabasePath(data).getAbsolutePath();}SQLiteDatabasesql=SQLiteDatabase.openDatabase(absolutePath,null,SQLiteDatabase.OPEN_READONLY);Cursorcursor=s
本文标题:安卓开发简要
链接地址:https://www.777doc.com/doc-5942031 .html