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

效果图示例

 

195000_mnuH_2542711.png

195000_8n9Y_2542711.png

195000_u30v_2542711.png

195000_Sqs6_2542711.png195104_mERQ_2542711.png

 

 

1、在清单里添加相应的权限

 

 <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文件夹下创建一个菜单文件夹menu

该菜单文件夹有2个菜单布局文件

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="添加学生信息"/>

</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个布局文件

1) activity_main.xml -- 只有一个ListView控件

2)item_activity.xml -- 有3个TextView 控件 用来显示 性别 年龄 分数

3)editdata_activity.xml -- 有3个EditText控件 用来添加 修改数据的编辑框

 

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个类 一个工具类 用来对数据库的增删改查的工作 一个MainActivity类

 

工具类

 

代码

 

public class Sqlite_operate_utils {
//File.separator -- 相当于  /
 private static String DB_PATH = Environment.getExternalStorageDirectory() + File.separator + "stuentinfo.db";
 private SQLiteDatabase db;
 
 //构造函数 new 该类的时候 就去找 需要找的 数据库
 public Sqlite_operate_utils() {
  db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
 }
 
 //查询 数据的 方法1
 public Cursor sqlite_select(String content, String[] condition){
  return db.rawQuery(content, condition);
 }
 
 //查询 数据 的 方法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()){//数据库表的 行
   Map<String, String> map = new HashMap<String, String>();
   for(int i = 0;i<cursor.getColumnCount();i++){//数据库表的列
    map.put(cursor.getColumnName(i), cursor.getString(i));
   }
   list.add(map);
  }
  cursor.close();
 // Log.i("data", "list:" + list.size());
  return list;
 }
 
 //增删改 的方法
 //返回布尔型 方便 查看 数据 操作 是否成功
 public boolean executeData(String execute_content, Object[] bindArgs){
  try {
   if(bindArgs == null){//要绑定占位符 的参数值
    db.execSQL(execute_content);
    return true;
   }else{
    db.execSQL(execute_content, bindArgs);
    return true;
   }
  } catch (SQLException e) {
   e.printStackTrace();
   return false;
  }
 }
 
 //关闭db
 public void destroy(){
  if(db != null){
   db.close();
  }
 }
}

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

 

MainActivity 类

 

代码在  --  sqlite数据库的基本增删改查操作MainActivity类代码 

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

 

转载于:https://my.oschina.net/u/2542711/blog/608648

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值