在学习了ContentProvider 的几个常用类和如何创建一个内容提供者的方法之后,下面我们用一个实例来进一步加深理解。通过一个完整的实例来展示如何用ContentProvider类进行数据库操作。将通过创建 100条模拟数据,用ListView 展示出来。
在进行具体的数据库操作之前,先建立一个Java 实体类 User类,再通过这个实体类进行数据的封装。User类中包括 userId、name、age三个属性,具体代码如下∶
package com.rfstar.sqlitetest2;
public class User {
private int userId;
private String name;
private int age;
public int getUserId()
{
return userId;
}
public void setUserId(int userId)
{
this.userId=userId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age=age;
}
}
完成Java实体类的创建之后,通过继承SQLiteOpenHelper来实现数据库辅助类,并实现其中的onCeate()方法和onUpgrate()方法。在onCreate()方法中创建表并使表中的字段与User类的属性相一致。同时实现onUpgrate()方法,以更新数据库。
package com.rfstar.sqlitetest2;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class OpenHelper extends SQLiteOpenHelper {
private static final String name="test.db";
private static final int version=1;
public OpenHelper(Context context)
{
super(context,name,null,version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "+
"user (person_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"name varchar(32),age INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
{
sqLiteDatabase.execSQL("ALTER TABLE user ADD phone VARCHAR(11)");
}
}
}
接下来创建UserDao类,并在该类中实现对User的增、删、改、查操作,代码如下:
package com.rfstar.sqlitetest2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
private SQLiteDatabase database;
public UserDao(SQLiteDatabase sqLiteDatabase) {
this.database = sqLiteDatabase;
}
public boolean insert(User user) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", user.getName());
contentValues.put("age", user.getAge());
long insertResult = database.insert("user", null, contentValues);
if (insertResult == -1) {
return false;
}
return true;
}
public boolean delete(User user) {
int deleteResult = database.delete("user", "user_id+?", new String[]{user.getUserId() + ""});
if (deleteResult == 0){
return false;
}
return true;
}
public boolean update(User user)
{
ContentValues contentValues=new ContentValues();
contentValues.put("name",user.getName());
contentValues.put("age",user.getAge());
int updateResult=database.update("user",contentValues,"user_id=?",new String[]{user.getUserId()+""});
if(updateResult==0)
{
return false;
}
return true;
}
public User queryOne(User user)
{
Cursor cursor=database.query("user",null,"name=?",new String[]
{user.getName()},null,null,null);
while (cursor.moveToNext())
{
user.setUserId(cursor.getInt(0));
user.setAge(cursor.getInt(2));
}
return user;
}
public List<User> queryAll()
{
List<User> userList=new ArrayList<>();
Cursor cursor=database.query("user",null,null,null,null,null,null);
while (cursor.moveToNext())
{
User user=new User();
user.setUserId(cursor.getInt(0));
user.setName(cursor.getString(1));
user.setAge(cursor.getInt(2));
userList.add(user);
}
return userList;
}
}
当完成上述3个类之后,接下来就可以使用Activity进行各项操作了(1)创建数据
创建一个MainActivity,并在MainActivity中创建一个循环生成100条数据的方法,代码如下:
package com.rfstar.sqlitetest2;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private OpenHelper openHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openHelper=new OpenHelper(this);
initData();
}
private void initData()
{
SQLiteDatabase sqLiteDatabase=openHelper.getReadableDatabase();
UserDao userDao=new UserDao(sqLiteDatabase);
sqLiteDatabase.beginTransaction();
try
{
User user=new User();
for(int i=0;i<100;i++)
{
user.setName("大鸟科创空间"