




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Android實(shí)現(xiàn)對(duì)SQLite數(shù)據(jù)庫(kù)增刪改查(學(xué)?管理系統(tǒng)項(xiàng)?)SQLite數(shù)據(jù)庫(kù)的使?上效果:?、介紹Sqlite是?種輕量級(jí)的數(shù)據(jù)庫(kù),它的設(shè)計(jì)?標(biāo)是嵌?式的,很多嵌?式的設(shè)備使?到了它,它的占?資源?常的低,通常只需要?百K的內(nèi)存就?夠了?、?致流程1.通過(guò)繼承SQLiteOpenHelper抽象類,完成對(duì)數(shù)據(jù)庫(kù)的創(chuàng)建2.進(jìn)?Sqlite數(shù)據(jù)增刪改查操作常?的SQLite語(yǔ)句:(id,age為字段,user為表名)添加:insertintouser(xxvarchar(20),xxxInteger)values(?,?)刪除:deletefromuserwhereid=?修改:updateusersetage=?whereid=?查詢:select*fromuserwhereid=?代碼如下MainActivity:packagecom.example.zheng.myapplication;importandroid.database.Cursor;importandroid.database.sqlite.SQLiteDatabase;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{privateEditTexted_name,ed_class,ed_age,ed_ID,ed_select_number;privateButtonbtn_insert,btn_select,btn_update,btndelete;privateTextViewfindResult;SQLiteDatabasedatabase;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Init();//初始化控件SQLitesqLite=newSQLite(this);//實(shí)例化對(duì)象,對(duì)象為初始創(chuàng)建的名稱databasesqLite.getWritableDatabase();//讀寫操作}privatevoidInit(){ed_name=findViewById(R.id.ed_name);ed_class=findViewById(R.id.ed_class);ed_age=findViewById(R.id.ed_age);ed_ID=findViewById(R.id.ed_number);ed_select_number=findViewById(R.id.ed_select_number);btn_insert=findViewById(R.id.btn_insert);btn_select=findViewById(R.id.btn_select);btn_update=findViewById(R.id.btn_update);btndelete=findViewById(R.id.btn_delete);findResult=findViewById(R.id.textView);btn_insert.setOnClickListener(this);btndelete.setOnClickListener(this);btn_update.setOnClickListener(this);btn_select.setOnClickListener(this);}@OverridepublicvoidonClick(Viewview){Stringsql;//這?需要注意?點(diǎn),敲出來(lái)的逗號(hào)?定要是英?的,否則會(huì)報(bào)錯(cuò)//這?需要注意?點(diǎn),敲出來(lái)的逗號(hào)?定要是英?的,否則會(huì)報(bào)錯(cuò)switch(view.getId()){caseR.id.btn_insert://添加sql="insertintouser(name,class,age,number)values(?,?,?,?)";//這?的表名和創(chuàng)建時(shí)的對(duì)應(yīng),字段名稱和創(chuàng)建時(shí)的對(duì)應(yīng)database.execSQL(sql,newObject[]{ed_name.getText().toString(),ed_class.getText().toString(),Integer.parseInt(ed_age.getText().toString()),ed_ID.getText().toString//Integer.parseInt()是對(duì)字符串進(jìn)?轉(zhuǎn)換,轉(zhuǎn)換int類型Toast.makeText(this,"數(shù)據(jù)添加成功!",Toast.LENGTH_SHORT).show();break;caseR.id.btn_delete://刪除sql="deletefromuserwherenumber=?";database.execSQL(sql,newObject[]{ed_ID.getText().toString()});Toast.makeText(this,"刪除成功!",Toast.LENGTH_SHORT).show();break;caseR.id.btn_select://查詢try{findResult.setText("");sql="select*fromuserwherenumber=?";//查詢字段名稱numberCursorcursor=database.rawQuery(sql,newString[]{ed_select_number.getText().toString()});//獲取?個(gè)游標(biāo)while(cursor.moveToNext())//判斷是否可以移動(dòng)到下??{//獲取這??名稱的數(shù)據(jù),對(duì)應(yīng)的是添加時(shí)的字段(列名稱)Strings_name=cursor.getString(cursor.getColumnIndex("name"));Strings_class=cursor.getString(cursor.getColumnIndex("class"));inti_age=cursor.getInt(cursor.getColumnIndex("age"));Strings_ID=cursor.getString(cursor.getColumnIndex("number"));findResult.setText("姓名:"+s_name+"\n"+"班級(jí):"+s_class+"\n"+"年齡:"+i_age+"\n"+"編號(hào):"+s_ID);Toast.makeText(this,"查詢成功!",Toast.LENGTH_SHORT).show();}}catch(Exceptione){e.printStackTrace();}break;caseR.id.btn_update://修改sql="updateusersetnumber=?,name=?,class=?,age=?wherenumber=?";//通過(guò)查詢編號(hào)進(jìn)?修改database.execSQL(sql,newObject[]{ed_ID.getText().toString(),ed_name.getText().toString(),ed_class.getText().toString(),Integer.parseInt(ed_age.getText().toString()),ed_select_number.getText().toString()});Toast.makeText(this,"修改成功!",Toast.LENGTH_SHORT).show();break;}}}代碼如下activity_main:<?xmlversion="1.0"encoding="utf-8"?><LinearLayout<LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="0.2"android:text="姓名:"android:textSize="25dp"/><EditTextandroid:id="@+id/ed_name"android:id="@+id/ed_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><TextViewandroid:id="@+id/textView3"android:layout_height="wrap_content"android:layout_weight="0.2"android:text="班級(jí):"android:textSize="25dp"/><EditTextandroid:layout_width="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><TextViewandroid:id="@+id/textView4"android:layout_height="wrap_content"android:text="android:textSize="25dp/><EditTextandroid:layout_width="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><TextViewandroid:id="@+id/textView6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="0.2"android:text="編號(hào):"android:textSize="25dp"/><EditTextandroid:id="@+id/ed_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:textSize="20dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><TextViewandroid:id="@+id/textView7"android:layout_weight="1"android:text=android:textSize="25dp"/><EditTextandroid:id="@+id/ed_select_number"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10"android:hint="請(qǐng)輸?查詢的編號(hào)"android:inputType="textPersonName"android:textSize="20dp"/><Buttonandroid:id="@+id/btn_select"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="查詢"android:textSize="18dp"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="0.2"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_insert"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="添加"android:layout_margin="10dp"android:textSize="18dp"/><Buttonandroid:id="@+id/btn_delete"android:layout_width="wrap_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="刪除"android:layout_margin="10dp"android:textSize="18dp"/><Buttonandroid:id="@+id/btn_update"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="修改"android:layout_margin="10dp"android:textS
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公路養(yǎng)護(hù)合同范本
- 人力資源外包合同范例
- 代理旗艦店合同范本
- 農(nóng)戶種植水稻合同范本
- 2024年中國(guó)移動(dòng)招聘考試真題
- 個(gè)人債權(quán)抵押合同范本
- 2024年西安工業(yè)大學(xué)專任教師招聘考試真題
- 企業(yè)簽訂勞務(wù)合同范本
- 供熱站拆除合同范本
- 兼職技術(shù)總工合同范本
- 如何讓孩子積極參與家務(wù)勞動(dòng)
- 2023年污水處理行業(yè)洞察報(bào)告及未來(lái)五至十年預(yù)測(cè)分析報(bào)告(修訂版)
- 小學(xué)生素質(zhì)發(fā)展報(bào)告單
- 人衛(wèi)版急診與災(zāi)難醫(yī)學(xué)之呼吸困難教學(xué)課件
- 廚房油煙凈化設(shè)備日常保養(yǎng)與維護(hù)
- 拓?fù)鋽?shù)據(jù)分析理論
- (完整版)200210號(hào)文-工程勘察設(shè)計(jì)收費(fèi)標(biāo)準(zhǔn)(2002年修訂本)
- 部編版語(yǔ)文二年級(jí)上冊(cè)第1單元核心素養(yǎng)教案
- (完整版)污水管道施工方案
- 國(guó)際法與國(guó)際爭(zhēng)端解決 課件全套 人大 第1-18章 導(dǎo)論、國(guó)際法淵源-國(guó)際人權(quán)法
- 發(fā)展?jié)h語(yǔ)初級(jí)口語(yǔ)I-第18課課件
評(píng)論
0/150
提交評(píng)論