Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
前言:
SQLite簡介:是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),它包含在一個相對小的C庫中。它是D.RichardHipp建立的公有領(lǐng)域項目。它的設(shè)計目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時能夠跟很多程序語言相結(jié)合,比如 Tcl、C#、PHP、Java等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源的世界著名數(shù)據(jù)庫管理系統(tǒng)來講,它的處理速度比他們都快。SQLite第一個Alpha版本誕生于2000年5月。
SQLite數(shù)據(jù)庫,它廣泛用于包括瀏覽器、IOS,Android以及一些便攜需求的小型web應(yīng)用系統(tǒng)。
接下來,我會通過一個登錄功能來介紹一下SQLite數(shù)據(jù)庫在實際Android項目中的使用。
SQLite數(shù)據(jù)庫的常用操作:
包含建表、刪除表、增、刪、改、查,SQL語法如下:
建表:
create table if not exists 表名(字段1 類型(長度),字段2 類型(長度),...)
刪除表:
drop table if exists 表名
增:
insert into 表名 (字段1,字段2,字段3 ...) values (值1,值2,值3 ...); insert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表;
刪:
delete from 表名 where 條件表達式
改:
update 表名 set 字段1=值1,字段2=值2... where 條件表達式
查:
select * from 表名 where 條件表達式
實例:
1、首先先創(chuàng)建一個DBHelper類(DBOpenHelper.java)
在這里會執(zhí)行建庫、建表的操作
package com.hyl.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; /** * @programName: DBOpenHelper.java * @programFunction: database helper class * @createDate: 2018/09/29 * @author: AnneHan * @version: * xx. yyyy/mm/dd ver author comments * 01. 2018/09/29 1.00 AnneHan New Create */ public class DBOpenHelper extends SQLiteOpenHelper { public DBOpenHelper(Context context,String name, CursorFactory factory, int version){ super(context, name, factory, version); } @Override //首次創(chuàng)建數(shù)據(jù)庫的時候調(diào)用,一般可以執(zhí)行建庫,建表的操作 //Sqlite沒有單獨的布爾存儲類型,它使用INTEGER作為存儲類型,0為false,1為true public void onCreate(SQLiteDatabase db){ //user table db.execSQL("create table if not exists user_tb(_id integer primary key autoincrement," + "userID text not null," + "pwd text not null)"); } @Override//當(dāng)數(shù)據(jù)庫的版本發(fā)生變化時,會自動執(zhí)行 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ } }
2、進入登錄界面
在點擊登錄按鈕時,會去數(shù)據(jù)庫里面進行查詢,判斷賬號是否存在(Query查詢范例)
/** * login event * @param v */ public void OnMyLoginClick(View v){ //判斷賬號/密碼是否有輸入的處理... //調(diào)用DBOpenHelper (qianbao.db是創(chuàng)建的數(shù)據(jù)庫的名稱) DBOpenHelper helper = new DBOpenHelper(this,"qianbao.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); //根據(jù)畫面上輸入的賬號/密碼去數(shù)據(jù)庫中進行查詢(user_tb是表名) Cursor c = db.query("user_tb",null,"userID=? and pwd=?",new String[]{參數(shù)1的值,參數(shù)2的值},null,null,null); //如果有查詢到數(shù)據(jù) if(c!=null && c.getCount() >= 1){ //可以把查詢出來的值打印出來在后臺顯示/查看 /*String[] cols = c.getColumnNames(); while(c.moveToNext()){ for(String ColumnName:cols){ Log.i("info",ColumnName+":"+c.getString(c.getColumnIndex(ColumnName))); } }*/ c.close(); db.close(); this.finish(); } //如果沒有查詢到數(shù)據(jù) else{ Toast.makeText(this, "手機號或密碼輸入錯誤!", Toast.LENGTH_SHORT).show(); } }
3、如果賬號不存在,則需要去注冊一個新賬號(Insert新增范例)
import com.hyl.dao.DBOpenHelper; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * register event * @param v */ public void OnMyRegistClick(View v){ //對用戶輸入的值的格式進行判斷的處理... //調(diào)用DBOpenHelper DBOpenHelper helper = new DBOpenHelper(this,"qianbao.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); //根據(jù)畫面上輸入的賬號去數(shù)據(jù)庫中進行查詢 Cursor c = db.query("user_tb",null,"userID=?",new String[]{參數(shù)1的值},null,null,null); //如果有查詢到數(shù)據(jù),則說明賬號已存在 if(c!=null && c.getCount() >= 1){ Toast.makeText(this, "該用戶已存在", Toast.LENGTH_SHORT).show(); c.close(); } //如果沒有查詢到數(shù)據(jù),則往數(shù)據(jù)庫中insert一筆數(shù)據(jù) else{ //insert data ContentValues values= new ContentValues(); values.put("userID","畫面上輸入的值"); values.put("pwd","畫面上輸入的值"); long rowid = db.insert("user_tb",null,values); Toast.makeText(this, "注冊成功", Toast.LENGTH_SHORT).show();//提示信息 this.finish(); } db.close(); }
4、如果用戶忘記密碼,則需要進行密碼重置(Update修改范例)
/** * confirm event */ private void confirmInfo() { //對界面上用戶輸入的值進行判斷的處理... //調(diào)用DBOpenHelper DBOpenHelper helper = new DBOpenHelper(this,"qianbao.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); //根據(jù)畫面上輸入的賬號/密碼去數(shù)據(jù)庫中進行查詢 Cursor c = db.query("user_tb",null,"userID=?",new String[]{editPhone.getText().toString()},null,null,null); //如果有查詢到數(shù)據(jù),說明賬號存在,可以進行密碼重置操作 if(c!=null && c.getCount() >= 1){ ContentValues cv = new ContentValues(); cv.put("pwd", editPhone.getText().toString());//editPhone界面上的控件 String[] args = {String.valueOf(editPhone.getText().toString())}; long rowid = db.update("user_tb", cv, "userID=?",args); c.close(); db.close(); Toast.makeText(this, "密碼重置成功!", Toast.LENGTH_SHORT).show(); this.finish(); } //如果沒有查詢到數(shù)據(jù),提示用戶到注冊界面進行注冊 else{ new AlertDialog.Builder(this) .setTitle("提示") .setMessage("該用戶不存在,請到注冊界面進行注冊!") .setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setResult(RESULT_OK); Intent intent=new Intent(當(dāng)前重置密碼界面.this,注冊界面.class); 當(dāng)前重置密碼界面.this.startActivity(intent); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { return; } }) .show(); } }
以上是一個登錄功能完整的處理流程,包含了建庫、增/改/查數(shù)據(jù)等操作,希望能讓大家對SQLite數(shù)據(jù)庫在實際項目中的使用有一個大概了解,不足之處,歡迎指正。如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫的方法
- Android使用SQLite數(shù)據(jù)庫的示例
- Android創(chuàng)建和使用數(shù)據(jù)庫SQLIte
- 實例講解Android App使用自帶的SQLite數(shù)據(jù)庫的基本方法
- Android App使用SQLite數(shù)據(jù)庫的一些要點總結(jié)
- Android中使用SQLite3 命令行查看內(nèi)嵌數(shù)據(jù)庫的方法
- Android使用SQLite數(shù)據(jù)庫的簡單實例
- Android SQLite數(shù)據(jù)庫增刪改查操作的使用詳解
- SQLite數(shù)據(jù)庫在Android中的使用小結(jié)
相關(guān)文章
Android如何基于坐標(biāo)對View進行模擬點擊事件詳解
這篇文章主要給大家介紹了關(guān)于Android如何基于坐標(biāo)對View進行模擬點擊事件的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03Android仿iOS側(cè)滑退出當(dāng)前界面功能
這篇文章主要為大家詳細介紹了Android仿iOS側(cè)滑退出當(dāng)前界面功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12AndroidQ分區(qū)存儲權(quán)限變更及適配的實現(xiàn)
這篇文章主要介紹了AndroidQ分區(qū)存儲權(quán)限變更及適配的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android入門之LinearLayout、AbsoluteLayout的用法實例講解
這篇文章主要介紹了Android入門之LinearLayout、AbsoluteLayout的用法,對于Android初學(xué)者有很好的參考借鑒價值,需要的朋友可以參考下2014-08-08