android作业笔记
编写SQLite数据库相关操作的代码,实现下图中的功能
SQLite展示
先上源码:https://gitee.com/meng-fanyang/SQLiteWork
里边有三个分支,对应这不同的写法:
创建数据库和数据表的步骤
package com.example.sqlitework;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper {//上面的建表建错了,要有下面的这建表,必须主键为_id,_id,_id重要的事情说三遍。这里设置主键_id自增private final static String SQL_PERSON_id="create table person_id" +"(_id integer primary key autoincrement,name text,age text,height text)";//通过调用父类的构造方法完成数据库的创建MyOpenHelper(Context context){super(context,"Allperson.db",null,1);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {//在本方法中完成数据库对象的创建sqLiteDatabase.execSQL(SQL_PERSON);}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {//在本方法中进行数据库的升级操作}
}
这一步部分太长了,就先不贴全部了,全部的在上边gitee仓库中可以查看
public class MainActivity extends AppCompatActivity {//先定义一些全局的,每次都要用的private ListView viewById;private SimpleCursorAdapter adapter;
// private Cursor cursor;//SimpleCursorAdapter所需要的参数String from[] = new String[]{"_id", "name", "age", "height"};int[] to = new int[]{R.id.tv_id, R.id.tv_name, R.id.tv_age, R.id.tv_height};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MyOpenHelper myOpenHelper = new MyOpenHelper(this);//打开数据库SQLiteDatabase sqLiteDatabase = myOpenHelper.getReadableDatabase();//关闭数据库sqLiteDatabase.close();viewById = (ListView) findViewById(R.id.LV);}/*** 插入数据* @param view*/public void addData(View view){Log.d(MainActivity.class.getSimpleName(),"=============接收到插入数据点击==============");count = count+1;nameText = (EditText) findViewById(R.id.name);ageText = (EditText) findViewById(R.id.age);heightText = (EditText) findViewById(R.id.height);String Name = nameText.getText().toString();String Age = ageText.getText().toString();String Height = heightText.getText().toString();MyOpenHelper myOpenHelper = new MyOpenHelper(this);SQLiteDatabase sqLiteDatabase = null;try {//打开数据库sqLiteDatabase = myOpenHelper.getReadableDatabase();//开启事务sqLiteDatabase.beginTransaction();//执行插入操作ContentValues contentValues = new ContentValues();contentValues.put("name", Name);contentValues.put("age", Age);contentValues.put("height", Height);sqLiteDatabase.insert("person_id",null,contentValues);Toast.makeText(this, "Successful insert data", Toast.LENGTH_SHORT).show();sqLiteDatabase.setTransactionSuccessful();//提交sqLiteDatabase.endTransaction();//关闭事务} catch (Exception e) {e.printStackTrace();}finally {String limit = String.valueOf(count);Cursor cursor = sqLiteDatabase.query("person_id", new String[]{"_id","name","age","height"},null,null,null,null,"_id desc" , limit);
// db.execSQL("select * from person_id order by _id desc limit 0,i;"); //此处是直接执行SQL语句adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);viewById.setAdapter(adapter);if(sqLiteDatabase != null){//关闭数据库sqLiteDatabase.close();}}}
这是用来分列展示
要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用SimpleAdapter创建(要求绑定的数据是List
第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)
注意:使用第二种方式在获取数据集合时必须指定主键"_id"
Android采用ListView实现数据列表显示
参考文章:
Android开发使用SQLite数据库和Listview实现数据的存储与展示
Android创建SQLite数据库和表
android中sqlite数据库的基本使用和添加多张表
Android——ListView与数据库的结合
上一篇:XSS挑战之旅1-10关
下一篇:智能云门禁解决方案来了