mardi 23 février 2016

How to open an already created sqlite database

as shown in the code below I created sqlite database in an App. and i want to use that database in another App. how can i do that

DATABASE_NAME = "GEOLOC.db";

DATABASE_TABLE_NAME = "NODE_12";

code:

public class SQLiteHelper extends SQLiteOpenHelper {

private final String TAG = this.getClass().getSimpleName();

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "GEOLOC.db";
private static final String DATABASE_TABLE_NAME = "NODE_12";

private Context mCtx = null;

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mCtx = context;

    //Log.i(TAG, "DB_PATH: " + this.mCtx.getDatabasePath("GEOLOC.db"));
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.w(TAG, "onCreate");

    db.execSQL(" CREATE TABLE " + DATABASE_TABLE_NAME + " ( " +
            BaseColumns._ID + " INTEGER PRIMARY KEY, " +
            "nodeid TEXT, " +
            "lat TEXT, " +
            "lng TEXT, " +
            "maxspeed TEXT " +
            " ); ");
}

public String getNodeID(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT nodeid FROM " + DATABASE_TABLE_NAME + " WHERE "+
            BaseColumns._ID+" = "+
            Long.toString(id) +" AND nodeid IS NOT NULL ", null);
    String r;
    c.moveToFirst();
    if (c.getCount() == 0) {
        return "";
    } else {
        r = c.getString(0);
    }
    c.close();
    db.close();
    return r;
}

Aucun commentaire:

Enregistrer un commentaire