jeudi 17 septembre 2015

db file in assets folder and database ,table and colomn is also created

i have some misconceptions please clear me.. first i have created a database , its all tables and columns respectively, but my app is just for retrieving property means as user enter the word and my app will give him all data regarding it(which is pre populated , no insertion occurs ) .. so i have imported my database to system to insert all information in each column using SQLite Manager then i copy this db file to assets folder (using android studio) ... the database name is same.. and my app is running but with an empty screen.. here is my coding .. please answer me with deep description ..im really confused .. `public class Cosmos_ResultActivity extends ActionBarActivity { public final static String MESSAGE_KEY="hans.mycompanion.massage_key"; TextView textView; ListView result_list; SQLiteDatabase sqlDB; DBHelper myDB; Cursor cursor; ListDataAdapter LDA; private SQLiteDatabase sql = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String word = intent.getStringExtra(MESSAGE_KEY);
    setContentView(R.layout.activity_cosmos__result);
    textView = (TextView) findViewById(R.id.textView);
    textView.setText(word);
    result_list = (ListView) findViewById(R.id.listView);


    myDB = new DBHelper(this);

    try {

        myDB.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        myDB.openDataBase();

    }catch(SQLException sqle){

        throw sqle;

    }

    LDA = new ListDataAdapter(getApplicationContext(), R.layout.custom_rows);
    result_list.setAdapter(LDA);
    myDB = new DBHelper(getApplicationContext());
    sqlDB = myDB.getReadableDatabase();
    cursor = myDB.getData(sqlDB);
    if (cursor.moveToFirst()) {
        do {
            String ayats, ayats_ref, ayats_eng, ayats_urdu;
            ayats = cursor.getString(0);
            ayats_ref = cursor.getString(1);
            ayats_eng = cursor.getString(2);
            ayats_urdu = cursor.getString(3);
            DataProvider dataProv = new DataProvider(ayats, ayats_ref, ayats_eng, ayats_urdu);
            LDA.add(dataProv);

        } while (cursor.moveToNext());
    }
}

public class DBHelper extends SQLiteOpenHelper {

private static final String LOGTAG = "LOGTAG";
private static final String DATABASE_NAME="Demo_cosmos_and_life_DB";
private static final int DATABASE_VERSION = 4;
private static String DB_PATH = "/data/data/hans.appxone.demomycompanion/databases/";

public final Context myContext; private SQLiteDatabase myDatabase;

public static final String Create_Word_Table = "CREATE TABLE " + Cosmos_tables.Word_Table_Class.Table_Words + "("
        + Cosmos_tables.Word_Table_Class.Word_id + " INTEGER PRIMARY KEY, "
        + Cosmos_tables.Word_Table_Class.Words + " TEXT NOT NULL);";

public static final String Create_Synonyums_Table = "CREATE TABLE " + Cosmos_tables.Synonyums_Table_Class.Table_Synonyums + " ("
        + Cosmos_tables.Synonyums_Table_Class.Syn_id + " INTEGER NOT NULL, "
        + Cosmos_tables.Synonyums_Table_Class.Synonyums + " TEXT NOT NULL, "
        + Cosmos_tables.Synonyums_Table_Class.Word_num + " INTEGER ,FOREIGN KEY ("
        + Cosmos_tables.Synonyums_Table_Class.Word_num + ") REFERENCES "
        + Cosmos_tables.Word_Table_Class.Table_Words + " ("
        + Cosmos_tables.Word_Table_Class.Word_id + "));";

public static final String Create_QuranicVerses_Table = "CREATE TABLE " + Cosmos_tables.QuranicVerses_Table_Class.Table_QuranicVerses + "("
        + Cosmos_tables.QuranicVerses_Table_Class.Ayats + " TEXT NOT NULL, "
        + Cosmos_tables.QuranicVerses_Table_Class.Ayat_References + " TEXT NOT NULL, "
        + Cosmos_tables.QuranicVerses_Table_Class.Ayat_English + " TEXT NOT NULL, "
        + Cosmos_tables.QuranicVerses_Table_Class.Ayat_Urdu + " TEXT NOT NULL, "
        + Cosmos_tables.QuranicVerses_Table_Class.col_1 + " INTEGER, "
        + Cosmos_tables.QuranicVerses_Table_Class.col_2 + " INTEGER, "
        + Cosmos_tables.QuranicVerses_Table_Class.col_3 + " INTEGER, "
        + Cosmos_tables.QuranicVerses_Table_Class.col_4 + " INTEGER);";

public static final String Create_Hadiths_Table = "CREATE TABLE " + Cosmos_tables.Hadiths_Table_Class.Table_Hadiths + "("
        + Cosmos_tables.Hadiths_Table_Class.Hadiths + " TEXT NOT NULL, "
        + Cosmos_tables.Hadiths_Table_Class.Hadith_References + " TEXT NOT NULL, "
        + Cosmos_tables.Hadiths_Table_Class.Hadith_English + " TEXT NOT NULL, "
        + Cosmos_tables.Hadiths_Table_Class.Hadith_Urdu + " TEXT NOT NULL, "
        + Cosmos_tables.Hadiths_Table_Class.col_1 + " INTEGER, "
        + Cosmos_tables.Hadiths_Table_Class.col_2 + " INTEGER, "
        + Cosmos_tables.Hadiths_Table_Class.col_3 + " INTEGER, "
        + Cosmos_tables.Hadiths_Table_Class.col_4 + " INTEGER);";

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, 4);
    Log.i(LOGTAG, "Database has opened");
    this.myContext = context;
}


/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DATABASE_NAME;
    myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

    if(myDatabase != null)
        myDatabase.close();

    super.close();

}



@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(Create_Word_Table);
    db.execSQL(Create_Synonyums_Table);
    db.execSQL(Create_QuranicVerses_Table);
    db.execSQL(Create_Hadiths_Table);
    Log.i(LOGTAG,"All Tables have been created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXIST " + Cosmos_tables.Word_Table_Class.Table_Words);
    db.execSQL("DROP TABLE IF EXIST " + Cosmos_tables.Synonyums_Table_Class.Table_Synonyums);
    db.execSQL("DROP TABLE IF EXIST " + Cosmos_tables.QuranicVerses_Table_Class.Table_QuranicVerses);
    db.execSQL("DROP TABLE IF EXIST " + Cosmos_tables.Hadiths_Table_Class.Table_Hadiths);
    onCreate(db);
    Log.i(LOGTAG,"Database has been upgraded");

}

public Cursor getData(SQLiteDatabase db) throws SQLException {
    Cursor cursor;
    String myPath = DB_PATH + DATABASE_NAME;
    SQLiteDatabase myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    String[] projections = {Cosmos_tables.QuranicVerses_Table_Class.Ayats, Cosmos_tables.QuranicVerses_Table_Class.Ayat_References, Cosmos_tables.QuranicVerses_Table_Class.Ayat_English, Cosmos_tables.QuranicVerses_Table_Class.Ayat_Urdu};
    cursor = myDatabase.query(Cosmos_tables.QuranicVerses_Table_Class.Table_QuranicVerses, projections, null, null, null, null, null);
    cursor.moveToFirst();
    myDatabase.close();
    return cursor;
}`

Aucun commentaire:

Enregistrer un commentaire