来自SQLite的数据无法显示

时间:2015-10-19 06:41:41

标签: java android sqlite

我有自己的数据库帮助,模型和主要活动,但数据仍未显示。我不知道我的代码有什么问题或遗漏。

这是我使用DB Browser for SQLite浏览的数据sqlite

enter image description here

  

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/com.example.jathniel.myapplication/databases/";
    private static String DB_NAME = "mydoctorfinder_new_migrate.sqlite";
    private SQLiteDatabase myDataBase;
    private Context myContext = null;
    public static String TAG = "tag";

    private static final String TABLE_DOCTORS = "doctors";
    private static final String KEY_ID = "id";
    private static final String KEY_FULLNAME = "full_name";
    private static final String KEY_GENDER = "gender";


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {
        boolean dbExist = this.checkDataBase();
        if(!dbExist) {
            this.getReadableDatabase();

            try {
                this.copyDataBase();
            } catch (IOException e) {
                throw new Error("Error");
            }
        }
    }

    public void copyDataBase() throws IOException {
        InputStream myInput = this.myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        FileOutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];

        int length;
        while((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        try {
            String e = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(e, (SQLiteDatabase.CursorFactory)null, 0);
        } catch (SQLiteException e) {
            ;
        }

        if(checkDB != null) {
            checkDB.close();
        }

        return checkDB != null;
    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        this.myDataBase = SQLiteDatabase.openDatabase(myPath, (SQLiteDatabase.CursorFactory)null, 0);
    }

    public synchronized void close() {
        if(this.myDataBase != null) {
            this.myDataBase.close();
        }

        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void createDatabase() {
    }

    public List<DoctorModel> getAllDoctorList() {
        List<DoctorModel> doctorsArrayList = new ArrayList<DoctorModel>();

        String selectQuery = "SELECT  * FROM " + TABLE_DOCTORS;
        Log.d(TAG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {

                DoctorModel doctor = new DoctorModel();
                doctor.id = c.getInt(c.getColumnIndex(KEY_ID));
                doctor.full_name = c.getString(c.getColumnIndex(KEY_FULLNAME));
                doctor.gender = c.getString(c.getColumnIndex(KEY_GENDER));

                doctorsArrayList.add(doctor);

            } while (c.moveToNext());
        }

        return doctorsArrayList;
    }
}
  

模型类

public class DoctorModel {

    public int id;
    public String full_name;
    public String gender;

    public DoctorModel(){

    }

    public DoctorModel(int id, String full_name, String gender) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.full_name = full_name;
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}
  

MainActivity

public class MainActivity extends AppCompatActivity {


    List<DoctorModel> list = new ArrayList<DoctorModel>();
    DatabaseHelper db;
    TextView tv;



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

        db = new DatabaseHelper(getApplicationContext());
        tv = (TextView) findViewById(R.id.tv);

        print(db.getAllDoctorList());

    }

    private void print(List<DoctorModel> list) {
        // TODO Auto-generated method stub
        String value = "";
        for(DoctorModel dm : list){
            value = value+"id: "+dm.id+", fullname: "+dm.full_name+" Gender: "+dm.gender+"\n";
        }
        tv.setText(value);
    }
}

2 个答案:

答案 0 :(得分:1)

您忘了在DoctorModel Object

下的doctorsArrayList中添加getAllDoctorList()
   // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {

            DoctorModel doctor = new DoctorModel();
            doctor.id = c.getInt(c.getColumnIndex(KEY_ID));
            doctor.full_name = c.getString(c.getColumnIndex(KEY_FULLNAME));
            doctor.gender = c.getString(c.getColumnIndex(KEY_GENDER));

            doctorsArrayList.add(doctor); // add Object in ArrayList

        } while (c.moveToNext());
    }

答案 1 :(得分:0)

您何时使用:private void print(列表列表)? 不是在打开应用程序时。

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

    db = new DatabaseHelper(getApplicationContext());
    tv = (TextView) findViewById(R.id.tv);

    // ADD THIS LINE:
    // NOW THIS WILL RUN WHEN OPENING/RUNNING THE APPLICATION
    print(db.getAllDoctorList());

}