E / SQLiteLog:(1)没有这样的表:HOSPITALS

时间:2016-12-08 11:10:30

标签: android sqlite android-sqlite

它会产生错误。我在assets文件夹中有一个名为Hospitals.sqlite的数据库。我尝试过this,但它仍无效。我还尝试在手机上卸载并重新安装应用程序。我还尝试在我的onCreate()和onUpgrade()(注释部分)上添加这段代码。

DBHandler

public class DBHandler extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 2;
    // Database Name
    private static final String DATABASE_NAME = "Hospitals";
    // Contacts table name
    private static final String HOSPITALS = "HOSPITALS";
    // Shops Table Columns names
    private static final String hospitalName = "HospitalName";
    private static final String latitude = "Latitude";
    private static final String longitude = "Longitude";

    public DBHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public List<String> getHospName() {
        List<String> hospName = new ArrayList<>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + HOSPITALS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                HospitalNodes hospitals = new HospitalNodes();
                hospitals.setHospital(cursor.getString(0));
                // Adding contact to list
                hospName.add(hospitals.getHospital());
            } while (cursor.moveToNext());
        }
        return hospName;
    }

    public List<Float> getLat() {
        List<Float> hospLat = new ArrayList<>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + HOSPITALS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                HospitalNodes hospitals = new HospitalNodes();
                hospitals.setLatitude(Integer.parseInt(cursor.getString(1)));
                // Adding contact to list
                hospLat.add(hospitals.getLatitude());
            } while (cursor.moveToNext());
        }
        return hospLat;
    }

    public List<Float> getLong() {
        List<Float> hospLong = new ArrayList<>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + HOSPITALS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                HospitalNodes hospitals = new HospitalNodes();
                hospitals.setLongitude(Integer.parseInt(cursor.getString(2)));
                // Adding contact to list
                hospLong.add(hospitals.getLongitude());
            } while (cursor.moveToNext());
        }
        return hospLong;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        /*String CREATE_HOSPITALS_TABLE = "CREATE TABLE " + HOSPITALS + "(Location varchar2(255), "
        + hospitalName + " varchar2(255), ServiceCapability varchar2(255), Class varchar2(255), Type varchar2(255), "
         + latitude + " numeric(15,5)" + longitude + " numeric(15,5));";
        db.execSQL(CREATE_HOSPITALS_TABLE);*/
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        /*db.execSQL("DROP TABLE IF EXISTS " + HOSPITALS);
        onCreate(db);*/
    }
}

医院节点

public class HospitalNodes {
    private String hospital;
    private float latitude;
    private float longitude;

    public HospitalNodes() {
    }

    public HospitalNodes(String hospital, float latitude, float longitude) {
        this.hospital = hospital;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public void setHospital(String hospital) {
        this.hospital = hospital;
    }

    public void setLatitude(float latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(float longitude) {
        this.longitude = longitude;
    }

    public String getHospital() {
        return hospital;
    }

    public float getLatitude() {
        return latitude;
    }

    public float getLongitude() {
        return longitude;
    }
}

主要活动

public void getAllHospitals(){
    DBHandler hosp = new DBHandler(this);
    List<String> hospName = hosp.getHospName();
    List<Float> hospLat = hosp.getLat();
    List<Float> hospLong = hosp.getLong();
    List<Marker> markers = new ArrayList<>();
    ArrayList<OverlayItem> anotherOverlayItemArray = new ArrayList<>();

    for(int i = 0; i < hospName.size(); i++)
        anotherOverlayItemArray.add(new OverlayItem("Road", "Nodes", new GeoPoint(hospLat.get(i), hospLong.get(i))));

    ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay
            = new ItemizedIconOverlay<OverlayItem>(
            this, anotherOverlayItemArray, null);
    map.getOverlays().add(anotherItemizedIconOverlay);
}

1 个答案:

答案 0 :(得分:1)

取消注释代码

  @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_HOSPITALS_TABLE = "CREATE TABLE " + HOSPITALS + "(Location varchar2(255), "
        + hospitalName + " varchar2(255), ServiceCapability varchar2(255), Class varchar2(255), Type varchar2(255), "
         + latitude + " TEXT" + longitude + " TEXT;";
        db.execSQL(CREATE_HOSPITALS_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       db.execSQL("DROP TABLE IF EXISTS " + HOSPITALS);
        onCreate(db);
    }