2015-10-19 155 views
1

我有我的数据库帮助,模型和mainactivity但数据仍然不显示。我不知道我的代码出了什么问题或缺失。来自SQLite的数据不显示

这是我浏览使用DB浏览器的SQLite

enter image description here

DatabaseHelper.java

数据sqlite的
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; 
    } 
} 

Model类

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); 
    } 
} 

回答

1

你忘了在doctorsArrayList添加DoctorModel ObjectgetAllDoctorList()

// 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()); 
    } 
+0

在那里我将添加此?对不起我只是在新的编程 –

+0

下'getAllDoctorList @GemUbaldo(),而loop'下。请仔细检查我的答案 –

+1

好吧,我很抱歉..只是想问一下mainactivity.class中的代码是否已经好吗?谢谢 –

0

什么时候使用:私人无效打印(名单列表)? 打开应用程序时不可用。

@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()); 

} 
+0

我想要在运行应用程序时显示它。我已经谷歌它,我认为这段代码是用于显示数据库中的列表 –

+0

因此,在onCreate()中添加这个代码:print(db.getAllDoctorList()); –

+0

我得到这个错误“致命例外:主要 进程:com.example.jathniel.myapplication,PID:4539 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jathniel.myapplication/com.example.jathniel .myapplication.MainActivity}:android.database.sqlite.SQLiteException:no such table:doctors(code 1):,while compiling:SELECT * FROM doctors“ –