如何将应用程序链接到外部SQlite数据库文件

时间:2017-05-11 16:29:02

标签: android android-database

我正处于应用程序的最后一道障碍,并一直试图将应用程序连接到我已创建的外部SQLite数据库文件,名为" health1.db"。我希望我的应用程序从该文件中读取数据。我已经编写了一些代码,但该代码从/ data / data /...

中的默认数据库文件夹中读取

我已将该数据库文件复制到assets / databases文件夹中。以下是我的代码示例...

表类定义

package com.example.ahmed.doctorsinbahrain9.database;

public class FacilitiesTable {
    public static final String TABLE_FACILITY = "facility";
    public static final String COLUMN_ID = "facilityId";
    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_PHONE_NUMBER = "phoneNumber";
    public static final String COLUMN_RATING = "rating";
    public static final String COLUMN_DISTANCE = "distance";
    public static final String COLUMN_DESCRIPTION = "description";
    public static final String COLUMN_IMAGE = "image";

    public static final String[] ALL_COLUMNS =
            {COLUMN_ID, COLUMN_TYPE, COLUMN_NAME, COLUMN_PHONE_NUMBER,
            COLUMN_RATING, COLUMN_DISTANCE, COLUMN_DESCRIPTION, COLUMN_IMAGE};
}

我的DataSource类:

    package com.example.ahmed.doctorsinbahrain9.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.ahmed.doctorsinbahrain9.Facility;

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

public class DataSource {

    private Context mContext;
    private SQLiteDatabase mDatabase;
    SQLiteOpenHelper mDbHelper;

    public DataSource(Context context) {
        this.mContext = context;
        mDbHelper = new DBHelper(mContext);
        mDatabase = mDbHelper.getWritableDatabase();
    }

    public void open() {
        mDatabase = mDbHelper.getWritableDatabase();
    }

    public void close() {
        mDbHelper.close();
    }

    public List<Facility> getAllFacilities() {
        List<Facility> facilities = new ArrayList<>();
        Cursor cursor = mDatabase.query(FacilitiesTable.TABLE_FACILITY, FacilitiesTable.ALL_COLUMNS,
                        null, null, null, null, null);

        while(cursor.moveToNext()) {
            Facility facility = new Facility();
            facility.setFacilityID(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_ID)));
            facility.setFacilityType(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_TYPE)));
            facility.setFacilityName(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_NAME)));
            facility.setFacilityPhoneNumber(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_PHONE_NUMBER)));
            facility.setFacilityRating(cursor.getFloat(cursor.getColumnIndex(FacilitiesTable.COLUMN_RATING)));
            facility.setFacilityDistance(cursor.getDouble(cursor.getColumnIndex(FacilitiesTable.COLUMN_DISTANCE)));
            facility.setFacilityDescription(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_DESCRIPTION)));
            facility.setFacilityImage(cursor.getString(cursor.getColumnIndex(FacilitiesTable.COLUMN_IMAGE)));
            facilities.add(facility);
        }

        return facilities;
    }
}

我的DBHelper课程:

public class DBHelper extends SQLiteOpenHelper {

    public static final String DB_FILE_NAME = "health1.db";
    public static final int DB_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DB_FILE_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }
}

最后在我的活动类中编写代码:

public class DisplayActivity extends AppCompatActivity {

    DataSource mDataSource;                  // Make an instance of the DataSource class

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDataSource = new DataSource(this);
        mDataSource.open();                 // Open database file

        List<Facility> listFromDB = mDataSource.getAllFacilities();
        FacilitiesAdapter adapter = new FacilitiesAdapter(this, listFromDB);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.displayActivityRecyclerView);
        recyclerView.setAdapter(adapter);
    }
}

0 个答案:

没有答案