SQLite数据库存储

作者: wxyass 分类: Android 发布时间: 2017-03-29 17:12

SQLite数据库存储

项目中,我用的是ormlite框架, 等后期我把操作步骤搬上来.

1 导入jar

ormlite-android-4.43.jar
ormlite-core-4.43.jar

2 创建实体类

3 创建Dao

4 创建DaoImpl

5 创建DatabaseHelper

6 修改MainActivity代码,做测试

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.yassblog.db.table.DoctorInfo;
import com.yassblog.db.table.PatientInfo;

public class MainActivity extends Activity {

    private EditText edit;
    private MainService mainService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = (EditText) findViewById(R.id.edit);

        mainService = new MainService(getApplicationContext(), null);

        // 读取数据
        PatientInfo patientInfo = mainService.searchPatient2("abcd");
        if (patientInfo != null) {
            edit.setText(patientInfo.getPatientmobile());
            edit.setSelection(patientInfo.getPatientmobile().length());
            Toast.makeText(this, "读取成功", 0).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 保存数据
        DoctorInfo doctorinfo = new DoctorInfo();
        doctorinfo.setDoctorkey("123");
        doctorinfo.setDoctorid("1234");
        mainService.saveDoctorInfo(doctorinfo);

        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setPatientkey("abc");
        patientInfo.setPatientmobile("abcd");
        mainService.savePatientInfo(patientInfo);

    }
}  

代码下载: http://oss.wxyass.com/wxyass/demo/zqzb.rar

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注