Android內(nèi)置SQLite的使用詳細(xì)介紹
一、創(chuàng)建數(shù)據(jù)庫(kù)
1、新建數(shù)據(jù)庫(kù)幫助類(lèi)
包名——右擊——new——Java class——輸入類(lèi)名:MyDBOpenHelper— —父類(lèi):SQLiteOpenHelper。
2、在數(shù)據(jù)庫(kù)幫助類(lèi)中輸入代碼
public class MyDBOpenHelper extends SQLiteOpenHelper {
//定義數(shù)據(jù)庫(kù)名和版本號(hào)
private static final String DBNAME="student.db";
private static final int VERSION=1;
public MyDBOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
//創(chuàng)建數(shù)據(jù)庫(kù)
@Override
public void onCreate(SQLiteDatabase db) {
//創(chuàng)建數(shù)據(jù)表
db.execSQL("create table stu_info(id INTEGER primary key autoincrement,sno varchar(10),name varchar(10),sex varchar(4),professional varchar(10),deparment varchar(20) )");
}
//升級(jí)數(shù)據(jù)庫(kù)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}3、代碼講解
(1)簡(jiǎn)介:
Android為了讓用戶(hù)能夠更加方便地管理數(shù)據(jù)庫(kù),丏門(mén)提供了一個(gè) SQLiteOpenHelper幫助類(lèi),借助這個(gè)類(lèi)就可以非常簡(jiǎn)單地對(duì)數(shù)據(jù)庫(kù)進(jìn)行創(chuàng)建。
SQLiteOpenHelper是一個(gè)抽象類(lèi),這意味著如果想使用它的話(huà),這就需要自己 創(chuàng)建一個(gè)類(lèi)去繼承他它就可以了。
例如:
public class MyDBOpenHelper extends SQLiteOpenHelper {
}(2)方法
| 方法 | 作用 | 示例 |
| onCreate(SQLiteDatabase db) | 創(chuàng)建數(shù)據(jù)庫(kù) | |
| onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) | 升級(jí)數(shù)據(jù)庫(kù) | |
| db.execSQL( ……………… ) | 創(chuàng)建數(shù)據(jù)表 | db.execSQL(“create table stu_info (id INTEGER primary key autoincrement,sno varchar(10),..." ); |
| getReadableDatabase() | 以只讀方式 打開(kāi)數(shù)據(jù)庫(kù) | db=mhelper. getReadableDatabase(); |
| getWritableDatabase() | 以讀寫(xiě)方式 打開(kāi)數(shù)據(jù)庫(kù) | db=mhelper.getWritableDatabase(); |
(3)構(gòu)造方法
SQLiteOpenHelper中有三個(gè)構(gòu)造方法可供重寫(xiě),一般使用參數(shù)少點(diǎn)的那個(gè)構(gòu)造方 法即可,必須要有它才能對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,這個(gè)構(gòu)造方法中,接受4個(gè)參數(shù):

Cursor游標(biāo)結(jié)果集(本案例沒(méi)用到)
游標(biāo)是一段私有的SQL工作區(qū),即一段內(nèi)存區(qū)域,用于暫時(shí)存放受SQL語(yǔ)句影響到的數(shù)據(jù)。通俗理解就是將受影響的數(shù)據(jù)暫時(shí)存放到一個(gè)內(nèi)存區(qū)域的虛表中,這個(gè)虛表就是游標(biāo)。
游標(biāo)在數(shù)據(jù)庫(kù)的事務(wù)回滾中有非常重要的作用。由于對(duì)數(shù)據(jù)庫(kù)的操作會(huì)暫時(shí)存放在游標(biāo)中,只要不提交,就可以根據(jù)游標(biāo)中的內(nèi)容進(jìn)行回滾。這樣有利于數(shù)據(jù)庫(kù)的安全。
(4)總結(jié)

integer這里都要大寫(xiě)成INTEGER?。。?/p>
簡(jiǎn)介 :
對(duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)表的操作,一共有四種:添加、查詢(xún)、更新、刪除。每一種 操作又各自對(duì)應(yīng)了一種SQL命令:insert(添加),select(查詢(xún)),update(更 新),delete(刪除)。
二、添加數(shù)據(jù)
1、界面效果

2、準(zhǔn)備工作
(1)添加 3 個(gè)頁(yè)面
整個(gè)作品中,要完成學(xué)生信息的添加、查詢(xún)、修改、刪除四個(gè)功能。每個(gè)頁(yè)面完成某一個(gè)功能,所以,添加另外的 3 個(gè)頁(yè)面,類(lèi)文件分別為:SecondActivity、ThirdActivity、 FoutActivity,
(2)準(zhǔn)備背景圖片
選擇 4 張圖片,粘貼到工程的 drawable 文件夾下,當(dāng)做 4 個(gè)頁(yè)面的背景圖片,
圖片如圖所示:

3、布局界面 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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="信息添加頁(yè)面"
android:textSize="30sp"
android:textStyle="bold"
android: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="學(xué)號(hào)"
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="專(zhuān)業(yè)班級(jí)"
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="下一頁(yè)"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/>
</LinearLayout>4、類(lèi)文件代碼 MainActivity.java
public class MainActivity extends AppCompatActivity {
//定義對(duì)象
private EditText edit_onesno,edit_onename,edit_onesex,edit_onepro,edit_onedep;
private Button btn_oneadd,btn_oneclear,btn_onenext;
private MyDBOpenHelper mhelper;//定義數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
private SQLiteDatabase db;//定義一個(gè)可以操作的數(shù)據(jù)庫(kù)對(duì)象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1 綁定控件
initView();
//2 添加按鈕功能的實(shí)現(xiàn)
btnAdd();
//3 清除和下一頁(yè)按鈕的功能
btnClearNext();
}
//綁定控件-------------代碼
private void initView() {
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=new MyDBOpenHelper(MainActivity.this);//實(shí)例化數(shù)據(jù)庫(kù)幫助類(lèi)
db=mhelper.getWritableDatabase();//創(chuàng)建數(shù)據(jù)庫(kù),獲取數(shù)據(jù)庫(kù)的讀寫(xiě)權(quán)限
}
//2 添加按鈕功能的實(shí)現(xiàn)------代碼
private void btnAdd() {
btn_oneadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//定義一個(gè)對(duì)象,構(gòu)建一行數(shù)據(jù)
ContentValues values=new ContentValues();//用 value 表示一行
values.put("sno",edit_onesno.getText().toString());//把輸入的學(xué)號(hào)放到 sno 列
values.put("name",edit_onename.getText().toString());//把輸入的姓名放到 name 列
values.put("sex",edit_onesex.getText().toString());//把輸入的性別放到 sex 列
values.put("professional",edit_onepro.getText().toString());//把輸入的專(zhuān)業(yè)放到 professional 列
values.put("deparment",edit_onedep.getText().toString());//把輸入的系部放到 department 列
//將這一行數(shù)據(jù)存放到數(shù)據(jù)庫(kù)的數(shù)據(jù)表中。參數(shù):(表名,某些為空的列自動(dòng)賦值 null,ContentValue 對(duì)象)
db.insert("stu_info",null,values);
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}
});
}
//3 清除和下一頁(yè)按鈕的功能-----代碼
private void btnClearNext() {
//清除按鈕的功能
btn_oneclear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit_onesno.setText("");
edit_onename.setText("");
edit_onesex.setText("");
edit_onepro.setText("");
edit_onedep.setText("");
}
});
//下一頁(yè)按鈕的功能
btn_onenext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
finish();
}
});
}
}5、代碼講解
(1)插入一條數(shù)據(jù)的步驟

(2)insert()方法的三個(gè)參數(shù)

1、第一個(gè)參數(shù)表名;
2、第二個(gè)參數(shù)是某些為空的列自動(dòng)賦值null;
3、第三個(gè)參數(shù)是ContentValue對(duì)象,它提供了一系列put()方法重載,用于向ContentValues中添加對(duì)象,只需要將表中的每個(gè)列名以及相應(yīng)的待添加的數(shù) 據(jù)傳入即可。
(3)總結(jié)


三、查詢(xún)數(shù)據(jù)
1、界面效果圖

2、布局界面 activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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="信息查詢(xún)頁(yè)面"
android:textSize="30sp"
android:textStyle="bold"
android: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="請(qǐng)輸入要查詢(xún)的學(xué)號(hào)"
android:textSize="25sp"/>
<Button
android:id="@+id/button_twoquery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查詢(xún)"
android:textSize="25sp"/>
<TextView
android:id="@+id/textView_tworesult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示查詢(xún)結(jié)果"
android:textSize="25sp" />
<Button
android:id="@+id/button_twonext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁(yè)"
android:textSize="25sp"
android:layout_gravity="right"
android:layout_marginTop="30dp"/>
</LinearLayout>3、類(lèi)文件 SecondActivity.java
public class SecondActivity extends AppCompatActivity {
//定義對(duì)象
EditText edit_twosno;
Button btn_twoquery,btn_twonext;
TextView txt_tworesult;
MyDBOpenHelper mhelper;//定義一個(gè)數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
SQLiteDatabase db;//定義一個(gè)操作的數(shù)據(jù)庫(kù)的類(lèi)對(duì)象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//1 控件初始化
initView();
//2 查詢(xún)按鈕功能的實(shí)現(xiàn)
btnQuery();
//3 下一頁(yè)按鈕功能的實(shí)現(xiàn)
btnNext();
}
//1 控件初始化-----------------------代碼
private void initView() {
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=new MyDBOpenHelper(SecondActivity.this);//實(shí)例化數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
db=mhelper.getWritableDatabase();//獲取數(shù)據(jù)庫(kù)的讀寫(xiě)權(quán)限
}
//2 查詢(xún)按鈕功能的實(shí)現(xiàn)--------代碼
private void btnQuery() {
btn_twoquery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//開(kāi)始查詢(xún) 參數(shù):(實(shí)現(xiàn)查詢(xún)的 sql 語(yǔ)句,條件參數(shù))
Cursor cursor =db.rawQuery("select * from stu_info where sno=?",new String[]{edit_twosno.getText().toString()});
if(cursor.getCount()!=0){//判斷結(jié)果集中是否有數(shù)據(jù),有:查詢(xún)成功;無(wú):查詢(xún)失敗
Toast.makeText(SecondActivity.this,"查詢(xún)成功",Toast.LENGTH_SHORT).show();
//循環(huán)遍歷結(jié)果集,取出數(shù)據(jù),顯示出來(lái)
while (cursor.moveToNext()){
String mysno=cursor.getString(cursor.getColumnIndex("sno"));
String myname=cursor.getString(cursor.getColumnIndex("name"));
String mysex=cursor.getString(cursor.getColumnIndex("sex"));
String mypro=cursor.getString(cursor.getColumnIndex("professional"));
String mydep=cursor.getString(cursor.getColumnIndex("deparment"));
txt_tworesult.setText(mysno+"\n"+myname+"\n"+mysex+"\n"+mypro+"\n"+mydep);
}
}else{
Toast.makeText(SecondActivity.this,"沒(méi)有查詢(xún)到該學(xué)號(hào)的學(xué)生",Toast.LENGTH_SHORT).show();
txt_tworesult.setText("");
}
}
});
}
//3 下一頁(yè)按鈕功能的實(shí)現(xiàn)------代碼
private void btnNext() {
btn_twonext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
finish();
}
});
}
}4、代碼講解
(1)查詢(xún)時(shí)用到的方法——方法1
query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
方法各參數(shù)的含義:
table:表名。相當(dāng)于select語(yǔ)句from關(guān)鍵字后面的部分。如果是多表聯(lián)合查詢(xún),可以用逗號(hào)將兩個(gè)表名分開(kāi)。
columns:要查詢(xún)出來(lái)的列名。相當(dāng)于select語(yǔ)句select關(guān)鍵字后面的部分。
selection:查詢(xún)條件子句,相當(dāng)于select語(yǔ)句where關(guān)鍵字后面的部分,在條件子句允許使用占位符“?”
selectionArgs:對(duì)應(yīng)于selection語(yǔ)句中占位符的值,值在數(shù)組中的位置與占位符在語(yǔ)句中的位置必須一致,否則就 會(huì)有異常。
groupBy:相當(dāng)于select語(yǔ)句group by關(guān)鍵字后面的部分
having:相當(dāng)于select語(yǔ)句having關(guān)鍵字后面的部分
orderBy:相當(dāng)于select語(yǔ)句order by關(guān)鍵字后面的部分,如:personid desc, age asc;
limit:指定偏移量和獲取的記錄數(shù),相當(dāng)于select語(yǔ)句limit關(guān)鍵字后面的部分。

(2)查詢(xún)時(shí)用到的方法——方法2
rawQuery(String sql,String[ ] selectionArgs)
方法各參數(shù)的含義:
sql :實(shí)現(xiàn)查詢(xún)的sql語(yǔ)句,例如: select * from stu_info where sno=?
selectionArgs:是?條件參數(shù),如果?這個(gè)內(nèi)占位符容為null的話(huà)就表示把所有的學(xué)號(hào)的學(xué)生都查出來(lái)

(3)查詢(xún)結(jié)果處理

(4)總結(jié)

四、修改數(shù)據(jù)
1、界面效果圖

2、布局界面 activity_ third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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="信息修改頁(yè)面"
android:textSize="30sp"
android:textStyle="bold"
android: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="請(qǐng)輸入要查詢(xún)的學(xué)號(hào)"
android:textSize="25sp"/>
<Button
android:id="@+id/button_threequery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查詢(xún)"
android:textSize="25sp"/>
</LinearLayout>
<EditText
android:id="@+id/editText_threesno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="學(xué)號(hào)"
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="下一頁(yè)"
android:textSize="25sp"
android:layout_gravity="right"/>
</LinearLayout>3、類(lèi)文件 ThirdActivity.java
public class ThirdActivity extends AppCompatActivity {
//定義對(duì)象
EditText edit_threeinputsno,edit_threesno,edit_threename,edit_threedep;
Button btn_threequery,btn_threemodify,btn_threenext;
MyDBOpenHelper mhelper;//定義一個(gè)數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
SQLiteDatabase db;//定義一個(gè)操作的數(shù)據(jù)庫(kù)的類(lèi)對(duì)象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
//1 控件初始化
initView();
//2 查詢(xún)按鈕功能的實(shí)現(xiàn)
btnQuery();
//3 修改按鈕功能的實(shí)現(xiàn)
btnModify();
//4 下一步按鈕功能的實(shí)現(xiàn)
btnNext();
}
//1 控件初始化-------------代碼
private void initView() {
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=new MyDBOpenHelper(ThirdActivity.this);//實(shí)例化數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
db= mhelper.getWritableDatabase();//獲取數(shù)據(jù)庫(kù)的讀寫(xiě)權(quán)限
}
//2 查詢(xún)按鈕功能的實(shí)現(xiàn)
private void btnQuery() {
btn_threequery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//先查詢(xún)顯示,再修改。參數(shù)(String sql,String[ ] selectionArgs)
Cursor cursor=db.rawQuery("select * from stu_info where sno=?",new String[]{edit_threeinputsno.getText().toString()});
if(cursor.getCount()!=0){
Toast.makeText(ThirdActivity.this,"查詢(xún)成功",Toast.LENGTH_SHORT).show();
while(cursor.moveToNext()){
String mysno=cursor.getString(cursor.getColumnIndex("sno"));
String myname=cursor.getString(cursor.getColumnIndex("name"));
String mydep=cursor.getString(cursor.getColumnIndex("deparment"));
edit_threesno.setText(mysno);
edit_threename.setText(myname);
edit_threedep.setText(mydep);
}
}else{
Toast.makeText(ThirdActivity.this,"沒(méi)有查詢(xún)到該學(xué)號(hào)的學(xué)生",Toast.LENGTH_SHORT).show();
edit_threesno.setText("");
edit_threename.setText("");
edit_threedep.setText("");
}
}
});
}
//3 修改按鈕功能的實(shí)現(xiàn)---------代碼
private void btnModify() {
btn_threemodify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//修改數(shù)據(jù)代碼如何寫(xiě)呢?參數(shù):(表名,ContentValues 對(duì)象,更新的條件,條件的參數(shù))
ContentValues values=new ContentValues();
values.put("deparment",edit_threedep.getText().toString());
db.update("stu_info",values,"sno=?",new String[]{edit_threesno.getText().toString()});
Toast.makeText(ThirdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
}
});
}
//4 下一頁(yè)按鈕功能的實(shí)現(xiàn)------代碼
private void btnNext() {
btn_threenext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(ThirdActivity.this,FourActivity.class);
startActivity(intent);
finish();
}
});
}
}4、代碼講解
(1)update()方法的四個(gè)參數(shù)
update(String table,ContentValues values,String whereClause,String[ ] whereArgs)
1、第一個(gè)參數(shù)表名;
2、第二個(gè)參數(shù)是ContentValues對(duì)象,要把更新的數(shù)據(jù)在這里組裝進(jìn)去;
3、第三個(gè)參數(shù)是更新的條件
4、第四個(gè)參數(shù)是條件的參數(shù)

(2)總結(jié)

五、刪除數(shù)據(jù)
1、界面效果圖

2、布局界面 activity_ four.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/deletebg"
tools:context=".FourActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息刪除頁(yè)面"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_gravity="center"
android:layout_margin="80dp"/>
<EditText
android:id="@+id/editText_foursno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入要?jiǎng)h除的學(xué)號(hào)"
android:textSize="25sp"/>
<Button
android:id="@+id/button_fourdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除"
android:textSize="25sp"
android:layout_gravity="right"/>
</LinearLayout>3、類(lèi)文件 FourActivity.java
public class FourActivity extends AppCompatActivity {
//定義對(duì)象
EditText edit_foursno;
Button btn_fourdelete;
MyDBOpenHelper mhelper;//定義一個(gè)數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
SQLiteDatabase db;//定義一個(gè)操作的數(shù)據(jù)庫(kù)的類(lèi)對(duì)象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
//1 控件初始化
initView();
//2 刪除按鈕功能的實(shí)現(xiàn)
btnDelete();
}
//1 控件初始化----------代碼
private void initView() {
edit_foursno=findViewById(R.id.editText_foursno);
btn_fourdelete=findViewById(R.id.button_fourdelete);
mhelper=new MyDBOpenHelper(FourActivity.this);//實(shí)例化數(shù)據(jù)庫(kù)幫助類(lèi)對(duì)象
db=mhelper.getWritableDatabase();//獲取數(shù)據(jù)庫(kù)的讀寫(xiě)權(quán)限
}
//2 刪除按鈕功能的實(shí)現(xiàn)-----代碼
private void btnDelete() {
btn_fourdelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//怎么樣刪除呢?參數(shù):(表名,刪除的條件,條件的參數(shù))
db.delete("stu_info","sno=?",new String[]{edit_foursno.getText().toString()});
Toast.makeText(FourActivity.this,"刪除成功",Toast.LENGTH_SHORT).show();
}
});
}
}4、代碼講解
(1)delete()方法的三個(gè)參數(shù)
delete(String table,String whereClause,String[ ] whereArgs)
1、第一個(gè)參數(shù):表名;
2、第二個(gè)參數(shù):刪除的條件
3、第三個(gè)參數(shù):條件的參數(shù)

(2)總結(jié)

到此這篇關(guān)于A(yíng)ndroid內(nèi)置SQLite的使用詳細(xì)介紹的文章就介紹到這了,更多相關(guān)Android SQLite使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Kotlin使用SQLite案例詳解
- android中SQLite使用及特點(diǎn)
- android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫(kù)的方法
- Android Studio 通過(guò)登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程
- android 中 SQLiteOpenHelper的封裝使用詳解
- Android開(kāi)發(fā)之使用SQLite存儲(chǔ)數(shù)據(jù)的方法分析
- Android中SQLite 使用方法詳解
- Android使用SQLite數(shù)據(jù)庫(kù)的示例
- Android開(kāi)發(fā)中使用sqlite實(shí)現(xiàn)新聞收藏和取消收藏的功能
相關(guān)文章
使用Fragment來(lái)處理Andoird app的UI布局的實(shí)例分享
這篇文章主要介紹了使用Fragment來(lái)處理Andoird appUI布局的實(shí)例分享,Fragment的出現(xiàn)緩解了代碼依賴(lài)于A(yíng)ctivity而造成的臃腫狀況,需要的朋友可以參考下2016-02-02
Android開(kāi)發(fā)中TextView各種常見(jiàn)使用方法小結(jié)
這篇文章主要介紹了Android開(kāi)發(fā)中TextView各種常見(jiàn)使用方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中TextView各種常見(jiàn)布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04
Android擴(kuò)大View點(diǎn)擊區(qū)域方案示例
這篇文章主要為大家介紹了Android擴(kuò)大View點(diǎn)擊區(qū)域方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
android開(kāi)發(fā)教程之文本框加滾動(dòng)條scrollview
EditText與TextView加上滾動(dòng)條其實(shí)很簡(jiǎn)單,只需要在文本輸入框或者文本顯示框上面加上滾動(dòng)條控件即可2014-02-02
Android漲姿勢(shì)知識(shí)點(diǎn)之你沒(méi)用過(guò)的BadgeDrawable
現(xiàn)在A(yíng)ndroid中有許多的應(yīng)用仿蘋(píng)果的在應(yīng)用圖標(biāo)上顯示小紅點(diǎn),下面這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid漲姿勢(shì)知識(shí)點(diǎn)之你沒(méi)用過(guò)的BadgeDrawable的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Android性能優(yōu)化之Bitmap圖片優(yōu)化詳解
在A(yíng)ndroid項(xiàng)目的imageview中使用大圖bitmap時(shí)會(huì)占據(jù)很大的內(nèi)存,而且在很多時(shí)候我們并不需要顯示原圖那么大的圖片, 所以我們需要對(duì)圖片進(jìn)行優(yōu)化,這篇文章主要介紹了Android性能優(yōu)化之Bitmap圖片優(yōu)化的相關(guān)資料,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Android破解微信獲取聊天記錄和通訊錄信息(靜態(tài)方式)
這篇文章主要介紹了Android以靜態(tài)方式破解微信獲取聊天記錄和通訊錄信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android dip,px,pt,sp 的區(qū)別詳解
本篇文章是對(duì)Android中dip,px,pt,sp的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類(lèi)的ArrayList示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類(lèi)的ArrayList,涉及Android對(duì)象序列化、反序列化、Intent數(shù)據(jù)傳輸?shù)认嚓P(guān)操作技巧,需要的朋友可以參考下2017-08-08

