日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

sqlite数据库的基本增删改查操作

發(fā)布時(shí)間:2025/1/21 数据库 78 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqlite数据库的基本增删改查操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

效果圖示例

?

?

?

1、在清單里添加相應(yīng)的權(quán)限

?

?<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
??? <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
??? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

?

-----------------------------------------

?

?

2、在res文件夾下創(chuàng)建一個(gè)菜單文件夾menu

該菜單文件夾有2個(gè)菜單布局文件

add_menu.xml菜單布局文件

代碼

?

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
???
??? <item android:id="@+id/adddata_menu"
??????? android:title="添加學(xué)生信息"/>

</menu>

?

?

-------------------------

?

list_menu.xml菜單布局文件

?

代碼

?

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

??? <item
??????? android:id="@+id/list_delete"
??????? android:title="刪除"/>
??? <item
??????? android:id="@+id/list_change"
??????? android:title="修改"/>
??? <item
??????? android:id="@+id/list_no"
??????? android:title="不操作"/>

</menu>

?

======================


?

3、布局界面 -- 3個(gè)布局文件

1) activity_main.xml -- 只有一個(gè)ListView控件

2)item_activity.xml -- 有3個(gè)TextView 控件 用來(lái)顯示 性別 年齡 分?jǐn)?shù)

3)editdata_activity.xml -- 有3個(gè)EditText控件 用來(lái)添加 修改數(shù)據(jù)的編輯框

?

activity_main.xml布局文件

?

代碼

?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? tools:context="${relativePackage}.${activityClass}" >

??? <ListView
??????? android:id="@+id/listview"
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"/>

</RelativeLayout>

?

?

================================

?

?

item_activity.xml布局文件

?

代碼

?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="horizontal" >

??? <TextView
??????? android:id="@+id/item_name"
??????? android:layout_width="0dp"
??????? android:layout_weight="1"
??????? android:layout_height="wrap_content"
??????? android:textSize="30sp"
??????? android:text="dddd"/>
???
???? <TextView
??????? android:id="@+id/item_sex"
??????? android:layout_width="0dp"
??????? android:layout_weight="1"
??????? android:layout_height="wrap_content"
??????? android:textSize="30sp"
??????? android:text="dddd"/>
???
????
????? <TextView
??????? android:id="@+id/item_age"
??????? android:layout_width="0dp"
??????? android:layout_weight="1"
??????? android:layout_height="wrap_content"
??????? android:textSize="30sp"
??????? android:text="dddd"/>
????

</LinearLayout>

?

?

=================================

?

editdata_activity.xml布局文件

?

代碼

?

?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical" >

??? <EditText
??????? android:id="@+id/edit_name"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="姓名"/>
???
???? <EditText
??????? android:id="@+id/edit_sex"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="性別"/>
????
????? <EditText
??????? android:id="@+id/edit_age"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:hint="年齡"/>

</LinearLayout>

?

?

================================

?

4、有2個(gè)類(lèi) 一個(gè)工具類(lèi) 用來(lái)對(duì)數(shù)據(jù)庫(kù)的增刪改查的工作 一個(gè)MainActivity類(lèi)

?

工具類(lèi)

?

代碼

?

public class Sqlite_operate_utils {
//File.separator -- 相當(dāng)于? /
?private static String DB_PATH = Environment.getExternalStorageDirectory() + File.separator + "stuentinfo.db";
?private SQLiteDatabase db;
?
?//構(gòu)造函數(shù) new 該類(lèi)的時(shí)候 就去找 需要找的 數(shù)據(jù)庫(kù)
?public Sqlite_operate_utils() {
??db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
?}
?
?//查詢(xún) 數(shù)據(jù)的 方法1
?public Cursor sqlite_select(String content, String[] condition){
??return db.rawQuery(content, condition);
?}
?
?//查詢(xún) 數(shù)據(jù) 的 方法2
?public List<Map<String, String>> sqlite_selectlist(String content, String[] condition){
?//?Log.i("data", "cursor:");
??Cursor cursor = db.rawQuery(content, condition);
??return cursorToList(cursor);
?}
?//返回List
?public List<Map<String, String>> cursorToList(Cursor cursor) {
??List<Map<String, String>> list = new ArrayList<Map<String,String>>();
??while(cursor.moveToNext()){//數(shù)據(jù)庫(kù)表的 行
???Map<String, String> map = new HashMap<String, String>();
???for(int i = 0;i<cursor.getColumnCount();i++){//數(shù)據(jù)庫(kù)表的列
????map.put(cursor.getColumnName(i), cursor.getString(i));
???}
???list.add(map);
??}
??cursor.close();
?//?Log.i("data", "list:" + list.size());
??return list;
?}
?
?//增刪改 的方法
?//返回布爾型 方便 查看 數(shù)據(jù) 操作 是否成功
?public boolean executeData(String execute_content, Object[] bindArgs){
??try {
???if(bindArgs == null){//要綁定占位符 的參數(shù)值
????db.execSQL(execute_content);
????return true;
???}else{
????db.execSQL(execute_content, bindArgs);
????return true;
???}
??} catch (SQLException e) {
???e.printStackTrace();
???return false;
??}
?}
?
?//關(guān)閉db
?public void destroy(){
??if(db != null){
???db.close();
??}
?}
}

============================

?

MainActivity 類(lèi)

?

代碼在? --? sqlite數(shù)據(jù)庫(kù)的基本增刪改查操作MainActivity類(lèi)代碼?

鏈接:http://my.oschina.net/u/2542711/blog/608649

?

轉(zhuǎn)載于:https://my.oschina.net/u/2542711/blog/608648

總結(jié)

以上是生活随笔為你收集整理的sqlite数据库的基本增删改查操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。