安卓的数据库为SQLite,使用的sql语句都是互通的,这里就不多讲了,看代码:
static ArrayAdapter adapter;
ImageView img;
Button add;
Button delete;
Button updata;
Button select;
ListView liv;
SQLiteDatabase db;
ArrayList<User> users = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day06);
// img = findViewById(R.id.img);
// new MyAsyncTask().execute("http://upload.jianshu.io/users/upload_avatars/6416344/5e103cde-c77d-49f9-8a47-2ed4418c564a.jpg");
add = findViewById(R.id.add);
delete = findViewById(R.id.delete);
updata = findViewById(R.id.updata);
select = findViewById(R.id.select);
liv = findViewById(R.id.liv);
//先创建数据库对象
MyDataBase base = new MyDataBase(this);
db = base.getWritableDatabase();
//增加
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAdd();
users.clear();
getSelect();
}
});
//删除
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDelete();
users.clear();
getSelect();
}
});
//修改
updata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getUpData();
users.clear();
getSelect();
}
});
//查询
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
users.clear();
getSelect();
}
});
}
定义的数据库类,继承了SQLiteOpenHelper
public class MyDataBase extends SQLiteOpenHelper {
public MyDataBase(Context context) {
super(context, "mydb", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//新建 只执行一次
db.execSQL("create table jiao (_id integer primary key autoincrement,name varchar(10),age integer)");
db.execSQL("insert into jiao values(null,'焦子晗',20)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//更新版本号
}
}
将各种操作的sql语句封装到各个方法中去
/**
* 增加数据方法
*/
public void getAdd(){
// db.execSQL("insert into jiao values(null,'焦子澳',20)");
ContentValues values = new ContentValues();
values.put("name","焦子澳");
values.put("age",20);
db.insert("jiao",null,values);
}
/**
* 删除方法
*/
public void getDelete(){
// db.execSQL("delete from jiao where name = '焦子晗'");
db.delete("jiao","name = ?",new String[]{"焦子晗"});
}
/**
* 修改方法
*/
public void getUpData(){
// db.execSQL("update jiao set name = '焦雪' where name = ?",new String[]{"焦子澳"});
ContentValues values = new ContentValues();
values.put("name","焦雪");
db.update("jiao",values,"name = ?",new String[]{"焦子澳"});
}
/**
* 查询方法
*/
public void getSelect(){
// Cursor cursor = db.rawQuery("select * from jiao", null);
// while(cursor.moveToNext()){
// String name = cursor.getString(cursor.getColumnIndex("name"));
// int age = cursor.getInt(cursor.getColumnIndex("age"));
// int id = cursor.getInt(cursor.getColumnIndex("_id"));
// users.add(new User(name,age,id));
// }
Cursor cursor = db.query("jiao", null, null, null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
int id = cursor.getInt(cursor.getColumnIndex("_id"));
users.add(new User(name,age,id));
}
adapter = new ArrayAdapter(Activity_day06.this,R.layout.support_simple_spinner_dropdown_item,users);
liv.setAdapter(adapter);
}
增删改查各两种方法,两种方法皆可以使用,第二种方法各有不同的参数,根据自己的情况而定;
如果想添加到ListView中去,创建集合,将数据放入集合中。
存储适配器集合数据的类
public class User{
String name;
int age;
int id;
public User(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}