实验六 数据库
1、实验目的
(1) 掌握Android 中sqlite数据库的读写
2、实验要求
(1) 认真填写实验报告,要求附加部分运行界面和主要代码;
(2) 对设计好的程序,检查输出是否符合预期,如有错请分析错误原因并解决;
3、实验内容:
(1) 采用sqlite创建Person数据库;
(2) 实现Person数据库的增删改查;
(3) 使用simpleCursorAdapter同步ListView;
效果图
MainAvtivity.java
package com.example.sqlite;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
// 拿到db数据库
SQLiteDatabase db;
EditText editText_name,editText_age;
ListView listView;
// listview的适配器
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_name = findViewById(R.id.editText_name);
editText_age = findViewById(R.id.editText_age);
listView = findViewById(R.id.listView);
//拿到可写的数据库
MySQLiteHelper helper = new MySQLiteHelper(this,"database.db",null,1);
db = helper.getWritableDatabase();
// 捆绑住适配器,拿到游标 (可以理解为一个表的指向一行的指针,从0开始的)
// Cursor cursor = db.rawQuery("select * from person",null);这种写法不提倡
Cursor cursor = db.query(MySQLiteHelper.PersonTable,null,null,null,null,null,null,null);
// 简单游标适配器
adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"name","age"},new int[]{android.R.id.text1,android.R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
}
public void add(View view){
String name = editText_name.getText().toString();
String age = editText_age.getText().toString();
ContentValues values = new ContentValues();
values.put("name",name);
values.put("age",age);
db.insert(MySQLiteHelper.PersonTable,null,values);
// 下面不推荐写法,一般用上面的ContentValues 来构造
// db.execSQL("insert into person(name,age) values('"+name+"','"+age+"')");
reloadData();
}
public void delete(View v){
String name = editText_name.getText().toString();
db.delete(MySQLiteHelper.PersonTable,"name=?",new String[]{name});
reloadData();
}
public void update(View v){
// 根据name 修改年龄
String name = editText_name.getText().toString();
String age = editText_age.getText().toString();
ContentValues values = new ContentValues();
values.put("age",age);
db.update(MySQLiteHelper.PersonTable,values,"name=?",new String[]{name});
reloadData();
}
public void retrieve(View v){
// 根据name来查询
String name = editText_name.getText().toString();
// new 一个 我的异步类 参数是一个字符串
new MyAsyncTask().execute("%"+name+"%");
// Cursor cursor = db.query(MySQLiteHelper.PersonTable,null,"name like ?",new String[]{"%"+name+"%"},null,null,null,null);
// adapter.swapCursor(cursor);// 告诉适配器更新游标
}
public void reloadData(){
// 全部查询后刷新
// 下面不推荐游标查询的写法
// Cursor cursor = db.rawQuery("select * from person",null);
// 一般这种写法
Cursor cursor = db.query(MySQLiteHelper.PersonTable,null,null,null,null,null,null,null);
adapter.swapCursor(cursor);
}
// 查询一般异步线程 防止主线程因数据过大而阻塞
class MyAsyncTask extends AsyncTask{
@Override
protected Cursor doInBackground(String... strings) {
Cursor cursor = db.query(MySQLiteHelper.PersonTable,null,"name like ?",new String[]{"%"+strings[0]+"%"},null,null,null,null);
// 告诉适配器更新游标
return cursor;
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
// 告诉适配器更新游标
adapter.swapCursor(cursor);
}
}
}
MySQLiteHelper.java
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteHelper extends SQLiteOpenHelper {
static final String PersonTable = "person";
public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//用来产生这个库所需要的表
db.execSQL("create table if not exists person(_id integer primary key autoincrement,name text,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}