irpas技术客

Android SQLiteStorage数据库存储实例_青帝a

未知 6580

1.创建一个类MySqliteOpenHelper继承SQLiteOpenHelper用来创建数据库,定义其构造方法

public MySqliteOpenHelper(Context context, String name, CursorFactory factory, int version){ super(context, name, factory, version); }

(1)context是一个访问application环境全局信息的接口,即上下文环境

(2)name是数据库的名字

(3)CursorFactory factory游标工厂,用来指向数据库里面的某一行 CursorFactory 用来创建游标的,默认值为null,指向一开始的位置

(4)version数据库的版本号

@Override public void onCreate(SQLiteDatabase db) { // 当前第一次创建数据库的时候回调 db.execSQL("create table Student(id Integer primary key autoincrement, " + "name varchar(10)," + "sex varchar(4)," + "age Integer)"); }

onCreate( ):利用SQL语句,在系统中创建数据库(表)功能

onUpgrade( ):利用SQL语句,在系统中升级数据库(表)功能

2.编写用户界面

<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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:hint="请输入搜索的姓名"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="btn_search" android:text="搜索" /> </LinearLayout> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" /> <EditText android:id="@+id/et_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入性别" /> <EditText android:id="@+id/et_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入年龄" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn_insert" android:text="添加" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn_delete" android:text="删除" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn_select" android:text="查询" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="btn_update" android:text="修改" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#000000" > <TextView android:id="@+id/tv_id" android:layout_width="0dp" android:background="#ffffff" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="学号" android:layout_margin="3dp" android:textColor="#ff0000" /> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:background="#ffffff" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="姓名" android:layout_margin="3dp" android:textColor="#ff0000" /> <TextView android:layout_margin="3dp" android:background="#ffffff" android:id="@+id/tv_sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="性别" android:textColor="#ff0000" /> <TextView android:background="#ffffff" android:layout_margin="3dp" android:id="@+id/tv_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="年龄" android:textColor="#ff0000" /> </LinearLayout> <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>

创建好ListView布局后,需要创建其对应的item(条目)布局 ,显示每个条目的信息:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#000000" > <TextView android:id="@+id/tv_id" android:layout_width="0dp" android:background="#ffffff" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_margin="1dp" android:textColor="#ff0000" /> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:background="#ffffff" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:layout_margin="1dp" android:textColor="#ff0000" /> <TextView android:layout_margin="1dp" android:background="#ffffff" android:id="@+id/tv_sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#ff0000" /> <TextView android:background="#ffffff" android:layout_margin="1dp" android:id="@+id/tv_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:textColor="#ff0000" /> </LinearLayout>

?效果:

3.编写实体类 用于数据的封装与传递

package com.hx.demo09_storage.SQLiteStorage; public class Student { private int id; private String name; private String sex; private int age; public Student() { } public Student(int id, String name, String sex, int age) { this.id = id; this.name = name; this.sex = sex; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

4.编写交互类代码,实现对数据的增删改查

EditText et_name, et_sex, et_age, et_search; ListView lv_list; List<Student> oList = new ArrayList<Student>(); MyAdapter adapter; // 数据库辅助类,用于创建和更新 MySqliteOpenHelper helper; // 操作数据库增删改查 SQLiteDatabase db; private void initview(){ et_name = (EditText) findViewById(R.id.et_name); et_sex = (EditText) findViewById(R.id.et_sex); et_age = (EditText) findViewById(R.id.et_age); et_search = (EditText) findViewById(R.id.et_search); lv_list = (ListView) findViewById(R.id.lv_list); helper = new MySqliteOpenHelper(this, "student_db", null, 2); db = helper.getWritableDatabase();//获得一个可读写数据库的对象 } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sqlite_storage); initview();//初始化 }

(1)?//创建指定数据库

?????????MySqliteOpenHelper helper;

?????????helper = new MySqliteOpenHelper(this, "student_db", null, 2);? ? ?

?(2)获得数据库操作对象

????????SQLiteDatabase db;

????????db = helper.getWritableDatabase();//获得一个可读写数据库的对象

public void insert() { String name = et_name.getText().toString(); String sex = et_sex.getText().toString(); String age = et_age.getText().toString(); // table 表名 // nullColumnHack 当我们需要插入数据时,没有明确指定字段名,默认插入数据为空 // values :ContentValues 插入的字段值 ContentValues values = new ContentValues(); values.put("name", name); values.put("sex", sex); values.put("age", Integer.parseInt(age)); // {name="",sex="",age=""} // 返回的是long类型,返回的是当前插入的数据的ID号 long index = db.insert("Student", null, values); if (index >= 0) { Show_Toase("插入成功" + index); } db.close(); }

(3)添加:当用户点击添加按钮时,会调用此方法,利用数据库操作对象db实现其插入方法

该方法接收3个参数,1数据库表名,2指定列的值默认为空,3要插入的数据,通过ContentValues对象封装

(4)查询修改和删除

public void query() { if (oList.size()>0){ oList.clear(); } // table 表名 // columns 查询的列名 // selection 带有占位符的条件 // selectionArgs 条件所对应的值 // groupBy 分组 // having 分组的条件 // orderBy 排序(分为升序ASC,降序DESC) // select * from Student order by id asc Cursor cursor = db.query("Student", null, null, null, null, null, "id asc"); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String sex = cursor.getString(cursor.getColumnIndex("sex")); int age = cursor.getInt(cursor.getColumnIndex("age")); Student student = new Student(id, name, sex, age); oList.add(student); } dateSetChang(); cursor.close(); db.close(); }

与ResultSet对象相同,Cursor游标对象用来实现对结果集的遍历

游标默认指向表中第一条记录,配合while循环实现向下移动,实现对表的遍历

public void update() { String name = et_name.getText().toString(); String sex = et_sex.getText().toString(); String age = et_age.getText().toString(); // table 表名 // valuse ContentValues 修改后的数据 // whereClause 带占位符的条件 // whereArgs 条件的值 // update Student set name=?,age=? where sex=? ContentValues values = new ContentValues(); values.put("name", name); values.put("age", Integer.parseInt(age)); // 返回值表示执行修改语句后,别修改了的行数 int i = db.update("Student", values, "sex=?", new String[]{sex}); if (i > 0) { Show_Toase("修改成功" + i); } db.close(); }

db.update(表名,数据,可选的where语句,参数列表用于替换占位符)

public void delete() { String name = et_name.getText().toString(); String sex = et_sex.getText().toString(); String age = et_age.getText().toString(); // table 表名 // whereClause 带有占位符的条件 // whereArgs 条件所对应的值数据 // 当前整型返回的是删除记录的行数 int i = db.delete("Student", "name=? and sex=? and age=?", new String[]{name, sex, age}); if (i > 0) { Show_Toase("删除成功" + i); } db.close(); }

(5)搜索

模糊查询 like 张%

public void btn_search(View v) { if (oList.size() > 0) { oList.clear(); } String seach_str = et_search.getText().toString(); Cursor cursor = db.rawQuery("select * from Student where name like ?", new String[]{"%" + seach_str + "%"}); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); String sex = cursor.getString(cursor.getColumnIndex("sex")); int age = cursor.getInt(cursor.getColumnIndex("age")); Student student = new Student(id, name, sex, age); oList.add(student); } dateSetChang(); db.close(); } private void dateSetChang() { if (adapter == null) { adapter = new MyAdapter(this, oList); lv_list.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } }

lv_list.setAdapter(adapter);? ? //给ListView列表设置适配器?

private LayoutInflater inflater;? ? ? //inflater充气机

this.inflater=LayoutInflater.from(context);? ? //context交互类

v=inflater.inflate(R.layout.item_layout, null);? ?//? v:ListView

package com.hx.demo09_storage.SQLiteStorage; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.hx.demo09_storage.R; public class MyAdapter extends BaseAdapter { private Context context; private List<Student> oList; private LayoutInflater inflater;//inflater充气机 public MyAdapter(Context context,List<Student> oList) { this.context=context; this.oList=oList; this.inflater=LayoutInflater.from(context); } @Override public View getView(int position, View v, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (v==null) { holder=new ViewHolder(); // this.inflater=LayoutInflater.from(context);context父容器布局 v:ListView v=inflater.inflate(R.layout.item_layout, null); holder.tv_id=(TextView)v.findViewById(R.id.tv_id); holder.tv_name=(TextView)v.findViewById(R.id.tv_name); holder.tv_sex=(TextView)v.findViewById(R.id.tv_sex); holder.tv_age=(TextView)v.findViewById(R.id.tv_age); v.setTag(holder); }else { holder=(ViewHolder)v.getTag(); } holder.tv_id.setText( oList.get(position).getId()+""); holder.tv_name.setText( oList.get(position).getName()); holder.tv_sex.setText( oList.get(position).getSex()); holder.tv_age.setText( oList.get(position).getAge()+""); return v; } }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android #context #string #name