-1

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??

Animesh Sinha
  • 1,045
  • 9
  • 16
user1568346
  • 29
  • 1
  • 7
  • When You are inserting data in the `database`? Is there any error log prints in logcat? – Animesh Sinha Feb 17 '13 at 12:39
  • Nup, i created database in pc. No error was there.entries were added successfully – user1568346 Feb 18 '13 at 18:01
  • From the code, you mentioned above it shows that you are creating database in your device directly, And you are trying to copy it in SDCARD after creation, My question is when you are inserting values in that database? You said that you can find the database in SDcard after copy, but it is Empty. – Animesh Sinha Feb 18 '13 at 19:29
  • I have created the database in computer and i connected my cell phone to the computer through usb cable. Nw whn the aplication is executed on my android cell phone its nt showing any database. Somtimes its show force close or sometime index(0) and then force close. And i didnt inserted any values in database while it is connected to my cel phone. Complete database of questions is created in computer only. – user1568346 Feb 20 '13 at 04:19
  • How you are importing database in CellPhone which is created on your computer? And where is the code to access the database in cell phone? Because I have already said the code mentioned above will create a fresh database on Cellphone internal memory, which is not accessible from *DDMS or Monitor* i.e. is in data/data/InYourPackage. – Animesh Sinha Feb 20 '13 at 09:42
  • Thankyou sir for the information. This is what the problem is. Is there any way of accessing the database, created in computer,in my cell phone or a code that help me solve my problem???? – user1568346 Feb 21 '13 at 18:50
  • Check the below thread in which it is explain how to access database from SDcard. what you need to do is copy the created database from computer to the desired path in the CellPhone SdCard. Note the path and database file name should be same.(http://stackoverflow.com/questions/7229450/sqliteopenhelper-creating-database-on-sd-card) – Animesh Sinha Feb 22 '13 at 10:50
  • Thanx alot sir i just try this code. Will let u know if it works 4 me or not. – user1568346 Feb 24 '13 at 04:23

1 Answers1

0

You don't need to copy database on your sd card. it works fine on the device as well.

Uninstall the application from your device. clear all data.

Delete the database from you asset folder and copy again.

If does not work. change the database name and copy again.

user1154390
  • 2,331
  • 3
  • 25
  • 32
  • While copying the database in asset folder again and connecting the device through usb do i have to copy the database file in the sdcard as i am using my cel as an emulator so the file must be copy sumwhere in it????? – user1568346 Feb 18 '13 at 18:06