温故知新----5种数据存储方式之SQLiteOpenHelper

前端之家收集整理的这篇文章主要介绍了温故知新----5种数据存储方式之SQLiteOpenHelper前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqliteOpenHelper常用方法:



DBHelper.Java

<span style="font-family:Microsoft YaHei;font-size:18px;">import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;

/**
 * Created by tangjiarao on 16/9/7.
 */
public class DbHelper extends sqliteOpenHelper{

    private static final String DATABASE_NAME = "person.db"; //数据库名称
    private static final int DATABASE_VERSION = 1;//数据库版本


    public DbHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }
    
    public void onCreate(sqliteDatabase db) {
        db.execsql("CREATE TABLE person " +
                "(_id integer primary key autoincrement," +
                "name varchar(20)," +
                "age varchar(10))");
    }

    @Override
    public void onUpgrade(sqliteDatabase db,int i,int i1) {
        db.execsql("DROP TABLE IF EXISTS person");
        onCreate(db);
    }
}
</span>
PersonService.Java
<span style="font-family:Microsoft YaHei;font-size:18px;">import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;

import java.util.ArrayList;
import java.util.List;

import tplusr.storagetest.db.DbHelper;

public class PersonService {
    private DbHelper dbHelper;
    private sqliteDatabase db;
    public PersonService(Context context) {

        dbHelper=new DbHelper(context);
        db=dbHelper.getReadableDatabase();
    }

    /**
     * 插入方法
     * @param person
     */
    public void insert(Person person){
        sqliteDatabase db=dbHelper.getWritableDatabase();
        db.execsql("insert into person(name,age) values(?,?)",new Object[]{person.getName(),person.getAge()});
    }

    /**
     * 删除方法
     * @param _id
     */
    public void delete(Integer _id){
        sqliteDatabase db=dbHelper.getWritableDatabase();
        db.execsql("delete from person where _id=?",new Object[]{_id});
    }

    /**
     * 查找某行数据方法
     * @param _id
     * @return
     */
    public Person find(Integer _id){
        sqliteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from person where _id=?",new String[]{_id.toString()});
        if(cursor.moveToFirst()){
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String age = cursor.getString(cursor.getColumnIndex("age"));
            Person person = new Person();
            person.set_id(id);
            person.setName(name);
            person.setAge(age);
            return person;
        }
        return null;
    }

    /**
     * 查找所有数据方法
     * @return
     */
    public List<Person> findAll(){
        sqliteDatabase db=dbHelper.getReadableDatabase();
        List<Person> persons = new ArrayList<Person>();
        Cursor cursor=db.rawQuery("select * from person",null);
        while(cursor.moveToNext()){
            Person person=new Person();
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            String age=cursor.getString(cursor.getColumnIndex("age"));
            person.set_id(id);
            person.setName(name);
            person.setAge(age);
            persons.add(person);
        }
        return persons;
    }
}</span>
Person.Java
<span style="font-family:Microsoft YaHei;font-size:18px;">public class Person {
    private int _id;
    private String name;
    private String age;

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}
</span>
DBActivity.Java
<span style="font-family:Microsoft YaHei;font-size:18px;">import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import tplusr.storagetest.R;

public class DBActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);

        PersonService service =new PersonService(this);
        Person person =new Person();
        person.setName("ahdad");
        person.setAge("39");
        service.insert(person);

        List<Person> p =service.findAll();
        for (int i =0;i<p.size();i++){
            Log.d("db",p.get(i).toString());
        }
    }
}
</span>
结果:

猜你在找的Sqlite相关文章