




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第Android內置SQLite的使用詳細介紹目錄一、創(chuàng)建數據庫1、新建數據庫幫助類2、在數據庫幫助類中輸入代碼3、代碼講解二、添加數據1、界面效果2、準備工作3、布局界面activity_main.xml4、類文件代碼MainActivity.java5、代碼講解三、查詢數據1、界面效果圖2、布局界面activity_second.xml3、類文件SecondActivity.java4、代碼講解四、修改數據1、界面效果圖2、布局界面activity_third.xml3、類文件ThirdActivity.java4、代碼講解五、刪除數據1、界面效果圖2、布局界面activity_four.xml3、類文件FourActivity.java4、代碼講解
一、創(chuàng)建數據庫
1、新建數據庫幫助類
包名右擊newJavaclass輸入類名:MyDBOpenHelper父類:SQLiteOpenHelper。
2、在數據庫幫助類中輸入代碼
publicclassMyDBOpenHelperextendsSQLiteOpenHelper{
//定義數據庫名和版本號
privatestaticfinalStringDBNAME="student.db";
privatestaticfinalintVERSION=1;
publicMyDBOpenHelper(Contextcontext){
super(context,DBNAME,null,VERSION);
//創(chuàng)建數據庫
@Override
publicvoidonCreate(SQLiteDatabasedb){
//創(chuàng)建數據表
db.execSQL("createtablestu_info(idINTEGERprimarykeyautoincrement,snovarchar(10),namevarchar(10),sexvarchar(4),professionalvarchar(10),deparmentvarchar(20))");
//升級數據庫
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)
}
3、代碼講解
(1)簡介:
Android為了讓用戶能夠更加方便地管理數據庫,丏門提供了一個SQLiteOpenHelper幫助類,借助這個類就可以非常簡單地對數據庫進行創(chuàng)建。
SQLiteOpenHelper是一個抽象類,這意味著如果想使用它的話,這就需要自己創(chuàng)建一個類去繼承他它就可以了。
例如:
publicclassMyDBOpenHelperextendsSQLiteOpenHelper{
}
(2)方法
方法作用示例onCreate(SQLiteDatabasedb)創(chuàng)建數據庫onUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion)升級數據庫db.execSQL()創(chuàng)建數據表db.execSQL(createtablestu_info(idINTEGERprimarykeyautoincrement,snovarchar(10),...);getReadableDatabase()以只讀方式打開數據庫db=mhelper.getReadableDatabase();getWritableDatabase()以讀寫方式打開數據庫db=mhelper.getWritableDatabase();
(3)構造方法
SQLiteOpenHelper中有三個構造方法可供重寫,一般使用參數少點的那個構造方法即可,必須要有它才能對數據庫進行操作,這個構造方法中,接受4個參數:
Cursor游標結果集(本案例沒用到)
游標是一段私有的SQL工作區(qū),即一段內存區(qū)域,用于暫時存放受SQL語句影響到的數據。通俗理解就是將受影響的數據暫時存放到一個內存區(qū)域的虛表中,這個虛表就是游標。
游標在數據庫的事務回滾中有非常重要的作用。由于對數據庫的操作會暫時存放在游標中,只要不提交,就可以根據游標中的內容進行回滾。這樣有利于數據庫的安全。
(4)總結
integer這里都要大寫成INTEGER?。。?/p>
簡介:
對數據庫中的數據表的操作,一共有四種:添加、查詢、更新、刪除。每一種操作又各自對應了一種SQL命令:insert(添加),select(查詢),update(更新),delete(刪除)。
二、添加數據
1、界面效果
2、準備工作
(1)添加3個頁面
整個作品中,要完成學生信息的添加、查詢、修改、刪除四個功能。每個頁面完成某一個功能,所以,添加另外的3個頁面,類文件分別為:SecondActivity、ThirdActivity、FoutActivity,
(2)準備背景圖片
選擇4張圖片,粘貼到工程的drawable文件夾下,當做4個頁面的背景圖片,
圖片如圖所示:
3、布局界面activity_main.xml
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/addbg"
tools:context=".MainActivity"
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息添加頁面"
android:textSize="30sp"
android:textandroid:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/
EditText
android:id="@+id/editText_onesno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="學號"
android:textSize="25sp"/
EditText
android:id="@+id/editText_onename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:textSize="25sp"/
EditText
android:id="@+id/editText_onesex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="性別"
android:textSize="25sp"/
EditText
android:id="@+id/editText_onepro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="專業(yè)班級"
android:textSize="25sp"/
EditText
android:id="@+id/editText_onedep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="所屬系部"
android:textSize="25sp"/
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
Button
android:id="@+id/button_oneadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:textSize="25sp"
android:layout_weight="1"/
Button
android:id="@+id/button_oneclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除"
android:textSize="25sp"
android:layout_weight="1"/
/LinearLayout
Button
android:id="@+id/button_onenext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/
/LinearLayout
4、類文件代碼MainActivity.java
publicclassMainActivityextendsAppCompatActivity{
//定義對象
privateEditTextedit_onesno,edit_onename,edit_onesex,edit_onepro,edit_onedep;
privateButtonbtn_oneadd,btn_oneclear,btn_onenext;
privateMyDBOpenHelpermhelper;//定義數據庫幫助類對象
privateSQLiteDatabasedb;//定義一個可以操作的數據庫對象
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1綁定控件
initView();
//2添加按鈕功能的實現
btnAdd();
//3清除和下一頁按鈕的功能
btnClearNext();
//綁定控件-------------代碼
privatevoidinitView(){
edit_onesno=findViewById(R.id.editText_onesno);
edit_onename=findViewById(R.id.editText_onename);
edit_onesex=findViewById(R.id.editText_onesex);
edit_onepro=findViewById(R.id.editText_onepro);
edit_onedep=findViewById(R.id.editText_onedep);
btn_oneadd=findViewById(R.id.button_oneadd);
btn_oneclear=findViewById(R.id.button_oneclear);
btn_onenext=findViewById(R.id.button_onenext);
mhelper=newMyDBOpenHelper(MainActivity.this);//實例化數據庫幫助類
db=mhelper.getWritableDatabase();//創(chuàng)建數據庫,獲取數據庫的讀寫權限
//2添加按鈕功能的實現------代碼
privatevoidbtnAdd(){
btn_oneadd.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//定義一個對象,構建一行數據
ContentValuesvalues=newContentValues();//用value表示一行
values.put("sno",edit_onesno.getText().toString());//把輸入的學號放到sno列
values.put("name",edit_onename.getText().toString());//把輸入的姓名放到name列
values.put("sex",edit_onesex.getText().toString());//把輸入的性別放到sex列
values.put("professional",edit_onepro.getText().toString());//把輸入的專業(yè)放到professional列
values.put("deparment",edit_onedep.getText().toString());//把輸入的系部放到department列
//將這一行數據存放到數據庫的數據表中。參數:(表名,某些為空的列自動賦值null,ContentValue對象)
db.insert("stu_info",null,values);
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
//3清除和下一頁按鈕的功能-----代碼
privatevoidbtnClearNext(){
//清除按鈕的功能
btn_oneclear.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
edit_onesno.setText("");
edit_onename.setText("");
edit_onesex.setText("");
edit_onepro.setText("");
edit_onedep.setText("");
//下一頁按鈕的功能
btn_onenext.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(MainActivity.this,SecondActivity.class);
startActivity(intent);
finish();
}
5、代碼講解
(1)插入一條數據的步驟
(2)insert()方法的三個參數
1、第一個參數表名;
2、第二個參數是某些為空的列自動賦值null;
3、第三個參數是ContentValue對象,它提供了一系列put()方法重載,用于向ContentValues中添加對象,只需要將表中的每個列名以及相應的待添加的數據傳入即可。
(3)總結
三、查詢數據
1、界面效果圖
2、布局界面activity_second.xml
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/querybg"
tools:context=".SecondActivity"
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息查詢頁面"
android:textSize="30sp"
android:textandroid:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/
EditText
android:id="@+id/editText_twosno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入要查詢的學號"
android:textSize="25sp"/
Button
android:id="@+id/button_twoquery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查詢"
android:textSize="25sp"/
TextView
android:id="@+id/textView_tworesult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示查詢結果"
android:textSize="25sp"/
Button
android:id="@+id/button_twonext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/
/LinearLayout
3、類文件SecondActivity.java
publicclassSecondActivityextendsAppCompatActivity{
//定義對象
EditTextedit_twosno;
Buttonbtn_twoquery,btn_twonext;
TextViewtxt_tworesult;
MyDBOpenHelpermhelper;//定義一個數據庫幫助類對象
SQLiteDatabasedb;//定義一個操作的數據庫的類對象
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//1控件初始化
initView();
//2查詢按鈕功能的實現
btnQuery();
//3下一頁按鈕功能的實現
btnNext();
//1控件初始化-----------------------代碼
privatevoidinitView(){
edit_twosno=findViewById(R.id.editText_twosno);
btn_twoquery=findViewById(R.id.button_twoquery);
txt_tworesult=findViewById(R.id.textView_tworesult);
btn_twonext=findViewById(R.id.button_twonext);
mhelper=newMyDBOpenHelper(SecondActivity.this);//實例化數據庫幫助類對象
db=mhelper.getWritableDatabase();//獲取數據庫的讀寫權限
//2查詢按鈕功能的實現--------代碼
privatevoidbtnQuery(){
btn_twoquery.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//開始查詢參數:(實現查詢的sql語句,條件參數)
Cursorcursor=db.rawQuery("select*fromstu_infowheresno=",newString[]{edit_twosno.getText().toString()});
if(cursor.getCount()!=0){//判斷結果集中是否有數據,有:查詢成功;無:查詢失敗
Toast.makeText(SecondActivity.this,"查詢成功",Toast.LENGTH_SHORT).show();
//循環(huán)遍歷結果集,取出數據,顯示出來
while(cursor.moveToNext()){
Stringmysno=cursor.getString(cursor.getColumnIndex("sno"));
Stringmyname=cursor.getString(cursor.getColumnIndex("name"));
Stringmysex=cursor.getString(cursor.getColumnIndex("sex"));
Stringmypro=cursor.getString(cursor.getColumnIndex("professional"));
Stringmydep=cursor.getString(cursor.getColumnIndex("deparment"));
txt_tworesult.setText(mysno+"\n"+myname+"\n"+mysex+"\n"+mypro+"\n"+mydep);
}else{
Toast.makeText(SecondActivity.this,"沒有查詢到該學號的學生",Toast.LENGTH_SHORT).show();
txt_tworesult.setText("");
//3下一頁按鈕功能的實現------代碼
privatevoidbtnNext(){
btn_twonext.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
finish();
}
4、代碼講解
(1)查詢時用到的方法方法1
query(table,columns,selection,selectionArgs,groupBy,having,orderBy,limit)
方法各參數的含義:
table:表名。相當于select語句from關鍵字后面的部分。如果是多表聯(lián)合查詢,可以用逗號將兩個表名分開。
columns:要查詢出來的列名。相當于select語句select關鍵字后面的部分。
selection:查詢條件子句,相當于select語句where關鍵字后面的部分,在條件子句允許使用占位符
selectionArgs:對應于selection語句中占位符的值,值在數組中的位置與占位符在語句中的位置必須一致,否則就會有異常。
groupBy:相當于select語句groupby關鍵字后面的部分
having:相當于select語句having關鍵字后面的部分
orderBy:相當于select語句orderby關鍵字后面的部分,如:personiddesc,ageasc;
limit:指定偏移量和獲取的記錄數,相當于select語句limit關鍵字后面的部分。
(2)查詢時用到的方法方法2
rawQuery(Stringsql,String[]selectionArgs)
方法各參數的含義:
sql:實現查詢的sql語句,例如:select*fromstu_infowheresno=
selectionArgs:是?條件參數,如果?這個內占位符容為null的話就表示把所有的學號的學生都查出來
(3)查詢結果處理
(4)總結
四、修改數據
1、界面效果圖
2、布局界面activity_third.xml
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/modifybg"
tools:context=".ThirdActivity"
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息修改頁面"
android:textSize="30sp"
android:textandroid:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="30dp"
EditText
android:id="@+id/editText_threeinputsno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="請輸入要查詢的學號"
android:textSize="25sp"/
Button
android:id="@+id/button_threequery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查詢"
android:textSize="25sp"/
/LinearLayout
EditText
android:id="@+id/editText_threesno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="學號"
android:textSize="25sp"/
EditText
android:id="@+id/editText_threename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"
android:textSize="25sp"/
EditText
android:id="@+id/editText_threedep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="所屬系部"
android:textSize="25sp"/
Button
android:id="@+id/button_threemodify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/
Button
android:id="@+id/button_threenext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="25sp"
android:layout_gravity="right"/
/LinearLayout
3、類文件ThirdActivity.java
publicclassThirdActivityextendsAppCompatActivity{
//定義對象
EditTextedit_threeinputsno,edit_threesno,edit_threename,edit_threedep;
Buttonbtn_threequery,btn_threemodify,btn_threenext;
MyDBOpenHelpermhelper;//定義一個數據庫幫助類對象
SQLiteDatabasedb;//定義一個操作的數據庫的類對象
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
//1控件初始化
initView();
//2查詢按鈕功能的實現
btnQuery();
//3修改按鈕功能的實現
btnModify();
//4下一步按鈕功能的實現
btnNext();
//1控件初始化-------------代碼
privatevoidinitView(){
edit_threeinputsno=findViewById(R.id.editText_threeinputsno);
edit_threesno=findViewById(R.id.editText_threesno);
edit_threename=findViewById(R.id.editText_threename);
edit_threedep=findViewById(R.id.editText_threedep);
btn_threequery=findViewById(R.id.button_threequery);
btn_threemodify=findViewById(R.id.button_threemodify);
btn_threenext=findViewById(R.id.button_threenext);
mhelper=newMyDBOpenHelper(ThirdActivity.this);//實例化數據庫幫助類對象
db=mhelper.getWritableDatabase();//獲取數據庫的讀寫權限
//2查詢按鈕功能的實現
privatevoidbtnQuery(){
btn_threequery.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//先查詢顯示,再修改。參數(Stringsql,String[]selectionArgs)
Cursorcursor=db.rawQuery("select*fromstu_infowheresno=",newString[]{edit_threeinputsno.getText().toString()});
if(cursor.getCount()!=0){
Toast.makeText(ThirdActivity.this,"查詢成功",Toast.LENGTH_SHORT).show();
while(cursor.moveToNext()){
Stringmysno=cursor.getString(cursor.getColumnIndex("sno"));
Stringmyname=cursor.getString(cursor.getColumnIndex("name"));
Stringmydep=cursor.getString(cursor.getColumnIndex("deparment"));
edit_threesno.setText(mysno);
edit_threename.setText(myname);
edit_threedep.setText(mydep);
}else{
Toast.makeText(ThirdActivity.this,"沒有查詢到該學號的學生",Toast.LENGTH_SHORT).show();
edit_threesno.setText("");
edit_threename.setText("");
edit_threedep.setText("");
//3修改按鈕功能的實現---------代碼
privatevoidbtnModify(){
btn_threemodify.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//修改數據代碼如何寫呢?參數:(表名,ContentValues對象,更新的條件,條件的參數)
ContentValuesvalues=newContentValues();
values.put("deparment",edit_threedep.getText().toString());
db.update("stu_info",values,"sno=",newString[]{edit_threesno.getText().toString()});
Toast.makeText(ThirdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
//4下一頁按鈕功能的實現------代碼
privatevoidbtnNext(){
btn_threenext.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Intentintent=newIntent(ThirdActivity.this,FourActivity.class);
startActivity(intent);
finish();
}
4、代碼講解
(1)update()方法的四個參數
update(Stringtable,ContentValuesvalues,StringwhereClause,String[]whereArgs)
1、第一個參數表名;
2、第二個參數是ContentValues對象,要把更新的數據在這里組裝進去;
3、第三個參數是更新的條件
4、第四個參數是條件的參數
(2)總結
五、刪除數據
1、界面效果圖
2、布局界面activity_four.xml
xmlversion="1.0"encoding="utf-8"
LinearLayoutxmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年福建武夷山三茶集團有限公司下屬子公司(第一批次)招聘考試筆試試題(含答案)
- 【銅川】2025年陜西銅川市事業(yè)單位招聘高層次人才13人(第三批)筆試歷年典型考題及考點剖析附帶答案詳解
- 小學憫農教學課件
- 文庫發(fā)布:的說課課件
- 收納職業(yè)課件教學
- 《鳥鳴澗》教學課件
- 教學課件改稿怎么寫好
- 語文片段教學課件
- DB33T 1126-2016 城市軌道交通巖土工程勘察規(guī)范
- 【成都】2025年成都市科學技術協(xié)會所屬1家事業(yè)單位招聘工作人員2人筆試歷年典型考題及考點剖析附帶答案詳解
- 混凝土基層檢驗批質量檢驗記錄
- 食品加工與保藏原理期末考試復習題及參考答案
- 主播藝人入職面試信息登記表
- 特應性皮炎的診斷與治療課件
- 社會學概論全套PPT完整教學課件
- 2016-2023年浙江新高考英語讀后續(xù)寫試題真題及范文賞析
- 2023數學建模國賽A題優(yōu)秀
- 山西省貫徹《二手車流通管理辦法》實施細則
- 社區(qū)工作者經典備考題庫(必背300題)
- 2023年陜西韓城象山中學高一物理第二學期期末聯(lián)考試題(含答案解析)
- 年產10萬噸污水處理藥劑菌劑項目環(huán)評報告書
評論
0/150
提交評論