Skip to content Skip to sidebar Skip to footer

Fetch Data From Existing Sqlite Database

I am having an existing sqlite database. I am developing an android app and I want to connect it with this existing sqlite DataBase. Problem 1: I have already included the sqlite

Solution 1:

The onCreate and onUpgrade methods will be empty since you already have the database. There is a great tutorial on how to achieve this here.

You could then access the database like such (example):

publicArrayList<String> getValues(String table) {
    ArrayList<String> values = newArrayList<String>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT value FROM " + table, null);

    if(cursor.moveToFirst()) {
        do {
            values.add(cursor.getString(cursor.getColumnIndex("value")));
        }while(cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return values;
}

Solution 2:

Unless you are very comfortable with queries, databases, etc. I highly recommend you use http://satyan.github.io/sugar/ , it will also remove a lot of the boiler plate code required to do sqlite in Android

Solution 3:

1. If DB already exists, onCreate will not invoke. onUpgrade will be invoked only if you will change DB version. onUpgrade you should to use if there some changes in your APP's database, and you have to make migration on new structure of data smoothly.

publicclassDbInitextendsSQLiteOpenHelper{
    privatestaticfinalString DATABASE_NAME = "name";
    privatestaticfinalint DATABASE_VERSION = 3;
    privatestaticfinalString DATABASE_CREATE = "create table  connections  . .. . ...

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         if (isChangeFromToVersion(1, 2, oldVersion, newVersion)) {
             //Execute UPDATE here
         }
    }

    private boolean isChangeFromToVersion(int from, int to, int oldVersion, int newVersion ) {
        return (from == oldVersion && to == newVersion);
  }
....

2. Simple example how to open connection to DB and get cursor object.

public class DAO {

private SQLiteDatabase database;
private DbInit dbHelper;

publicConnectionDAO(Context context) {
    dbHelper = newDbInit(context);
}

publicvoidopen()throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public Connection getConnectionById(long id) {
    Cursorcursor=null;
    try {
        open();
        cursor = database.query(DbInit.TABLE_CONNECTIONS, allColumns, DbInit.COLUMN_ID + " = '" + id + "'", null, null, null, null);
        if (!cursor.moveToFirst())
            returnnull;
        return cursorToConnection(cursor);
    } finally {
        if (cursor != null)
            cursor.close();
        close();
    }
}

private Connection cursorToConnection(Cursor cursor) {
    Connectionconnection=newConnection();
    connection.setId(cursor.isNull(0) ? null : cursor.getInt(0));
    connection.setName(cursor.isNull(1) ? null : cursor.getString(1));
    .....
    .....
    return connection;
}

Post a Comment for "Fetch Data From Existing Sqlite Database"