i am a beginner in android. i have developed a quiz application.The database i created is working fine in emulator and after searching on this site i get to know its stored in data/package name/database location. when i connected a device and use it as emulator its not accessing database. its showing an empty database. i searched through the site and found from highly rated question to move database file to sdcard of the device so i used the following code:
public class DatabaseFile extends SQLiteOpenHelper {
public static final String DATABASE_NAME="Quiz.db";
public static final String QUES="ques";
public static final String OP1="op1";
public static final String OP2="op2";
public static final String OP3="op3";
public static final String ANS="ans";
public static final String NAME="name";
public static final String CANS="cans";
public static final String SCORE="score";
public DatabaseFile(Context context)
{
super(context, DATABASE_NAME, null, 1);
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath ="/data/com.example.quizproject.StartQuiz/databases/Quiz.db";
String backupDBPath = "/backup/"+"Quiz.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
} else {
String TAG = null;
Log.e(TAG, "File does not exist: " + currentDBPath);
}
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE quiztable (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"ques TEXT, op1 TEXT, op2 TEXT, op3 TEXT, ans TEXT);");
}
private Context getBaseContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
i have also added the permission in the manifest file to write on sdcard. the above code is not working for me. it is still showing an empty database and the copy of the database is also not created in sdcard. As an added information i am accessing the database in emulator using
DatabaseFile dbfile;
SQLiteDatabase sqldb;
dbfile=new DatabaseFile(this);
sqldb=dbfile.getWritableDatabase();
will the code remain same when i conned the app to a real device????device i connected is galaxy y.
plzzz help me .....i hav searched everywhere bt not be able to find any solution where am geting wrong??? why database file is not transfered to sdcard??