0

recently i am trying to build an application that can open existing SQLite database using Android Studio, and i am still a newbie on android programming...when i was searching for the way to open the database, i found this link :

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

btw i still don't know how to implement the code on that link into my project...is there anyone here who is kind enough to help make the examples (or at least give some direction) about how to use it? Just a simple example is really enough for me...thx before :)

  • Specify what happened when you tried this, Didn't it work as you expected ? or any error shown ? – Shree Krishna Mar 20 '16 at 04:15
  • maybe it is more like i don't know how to use it,,,could you give me some examples that use the code from that link? :) – DeadlyKitten999 Mar 20 '16 at 04:45
  • i am still new about android programming, but i will understand it quickly if there is at least one example about how to implement the code.. :) – DeadlyKitten999 Mar 20 '16 at 04:47
  • are you willing to copy already created database into your app ? – Shree Krishna Mar 20 '16 at 04:50
  • 1
    https://github.com/zaaach/CityPicker this library use a pre-loaded sqlite database – tiny sunlight Mar 20 '16 at 04:52
  • i am trying to use the code on the link to read the database that i made myself...i already know the instruction on how to put my own database into android studio but when i see the code it seems not clear for me...i don't know how to implement it into my main activity or any other class that i made...do you know how to use the code?? some direction is enough for me anyway :) – DeadlyKitten999 Mar 20 '16 at 04:55
  • @tiny sunlight wow chinese...i will look into it :D btw i just want to implement simple database,,,looks like it is still a bit complicated for me :D – DeadlyKitten999 Mar 20 '16 at 04:57

1 Answers1

0

You can just use new DBManager().getAllCities() in activity.

/**
 * author zaaach on 2016/1/26.
 */
public class DBManager {
    private static final String ASSETS_NAME = "china_cities.db";
    private static final String DB_NAME = "china_cities.db";
    private static final String TABLE_NAME = "city";
    private static final String NAME = "name";
    private static final String PINYIN = "pinyin";
    private static final int BUFFER_SIZE = 1024;
    private String DB_PATH;
    private Context mContext;

//    public static DBManager init(){
//        if (mInstance == null){
//            synchronized (DBManager.class){
//                if (mInstance != null){
//                    mInstance = new DBManager();
//                }
//            }
//        }
//        return mInstance;
//    }

    public DBManager(Context context) {
        this.mContext = context;
        DB_PATH = File.separator + "data"
                + Environment.getDataDirectory().getAbsolutePath() + File.separator
                + context.getPackageName() + File.separator + "databases" + File.separator;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void copyDBFile(){
        File dir = new File(DB_PATH);
        if (!dir.exists()){
            dir.mkdirs();
        }
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbFile.exists()){
            InputStream is;
            OutputStream os;
            try {
                is = mContext.getResources().getAssets().open(ASSETS_NAME);
                os = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int length;
                while ((length = is.read(buffer, 0, buffer.length)) > 0){
                    os.write(buffer, 0, length);
                }
                os.flush();
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public List<City> getAllCities(){
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
        List<City> result = new ArrayList<>();
        City city;
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(NAME));
            String pinyin = cursor.getString(cursor.getColumnIndex(PINYIN));
            city = new City(name, pinyin);
            result.add(city);
        }
        cursor.close();
        db.close();
        Collections.sort(result, new CityComparator());
        return result;
    }

}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42