資源描述:
《Android實(shí)驗(yàn)四 Android應(yīng)用程序開(kāi)發(fā)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、計(jì)算機(jī)科學(xué)與技術(shù)系實(shí)驗(yàn)報(bào)告專(zhuān)業(yè)名稱(chēng)計(jì)算機(jī)科學(xué)與技術(shù)課程名稱(chēng)Android應(yīng)用程序開(kāi)發(fā)項(xiàng)目名稱(chēng)數(shù)據(jù)存儲(chǔ)與訪問(wèn)班級(jí)計(jì)科1班學(xué)號(hào)姓名同組人員無(wú)實(shí)驗(yàn)日期2016.9.2720一、實(shí)驗(yàn)?zāi)康呐c要求:1.1實(shí)驗(yàn)?zāi)康恼莆誗QLite存儲(chǔ)數(shù)據(jù)的方法,掌握ContentProvider的用法。1.2實(shí)驗(yàn)要求(1)練習(xí)使用SQLite數(shù)據(jù)庫(kù)的方式進(jìn)行數(shù)據(jù)存儲(chǔ)和訪問(wèn)。(2)練習(xí)使用ContentProvider訪問(wèn)其他應(yīng)用程序的數(shù)據(jù)。(3)完成實(shí)驗(yàn)報(bào)告。二、實(shí)驗(yàn)內(nèi)容2.1實(shí)驗(yàn)原理(1)數(shù)據(jù)存儲(chǔ)方式SharedPreferences、File存儲(chǔ)、SQLite數(shù)據(jù)庫(kù)(2)ContentProvider數(shù)據(jù)共享在
2、創(chuàng)建ContentProvider前,首先要實(shí)現(xiàn)底層的數(shù)據(jù)源,數(shù)據(jù)源包括數(shù)據(jù)庫(kù)、文件系統(tǒng)或網(wǎng)絡(luò)等,然后繼承ContentProvider類(lèi)中實(shí)現(xiàn)基本數(shù)據(jù)操作的接口函數(shù),包括添加、刪除、查找和更新等功能,調(diào)用者不能直接調(diào)用ContentProvider的接口函數(shù),而需要使用ContentResolver對(duì)象,通過(guò)URI間接調(diào)用ContentProvider,調(diào)用關(guān)系如圖:2.2實(shí)驗(yàn)過(guò)程及截圖(1)新建Android應(yīng)用程序項(xiàng)目SQLiteTest,創(chuàng)建SQLite數(shù)據(jù)庫(kù)和表,并實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)讀取和存入操作。201)新建SQLiteTest項(xiàng)目2)代碼邏輯:MainActivity.jav
3、a文件packageedu.hfuu.sqlitetest;importandroid.app.Activity;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;publicclassMainActivityextendsA
4、ctivity{SQLiteDatabasesld,sdtest;Buttonbt_open,bt_close,bt_add,bt_delete,bt_update,bt_query;EditTextet_log,et_query;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);20bt_open=(Button)findViewById(R.id.Button01);bt_
5、close=(Button)findViewById(R.id.Button02);bt_add=(Button)findViewById(R.id.Button03);bt_delete=(Button)findViewById(R.id.Button04);bt_update=(Button)findViewById(R.id.Button05);bt_query=(Button)findViewById(R.id.Button06);et_log=(EditText)this.findViewById(R.id.EditText01);et_query=(EditText)thi
6、s.findViewById(R.id.EditText02);}//創(chuàng)建、打開(kāi)數(shù)據(jù)庫(kù)publicvoidcreateDatabase(Viewv){try{sdtest=openOrCreateDatabase("dbtest.db3",MODE_PRIVATE,null);sld=SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/mydb.db3",null);appendMessage(et_log,"數(shù)據(jù)庫(kù)已經(jīng)成功打開(kāi)!");Stringsql="createtableifnotexistss
7、tudent(sidintegerprimarykeyautoincrement,snochar(5),"+"stunamevarchar(20),"+"sageinteger)";sld.execSQL(sql);appendMessage(et_log,"student已經(jīng)成功創(chuàng)建!");}catch(Exceptione){Toast.makeText(this,"數(shù)據(jù)庫(kù)錯(cuò)誤:"+e.toString(),Toast.LENGTH_SHO