1

I have several tables to be inserted into a database and have created separate java classes for each files. I am new to sqllite. SO had the following queries. Please help from your expertise

1) When i run create and open database, it catches the exception, but when i change the version number, it runs normally. What does this version signify??

2) For each creation of the new table, should i have to increment the version??

3) Also, if i have to create tables to the same database, then i have to keep the database name, helper name same in all the classes i create??

4) Can i delete a version so that i can reuse them?? Or drop this database entirely?? So that i can recreate everything from the beginning?

5) Lastly where can i see the database files?? many forums suggested it will be in data/.. folder but my data/.. folder remains empty always.

Please share your expertize and oblige.

1 Answer 1

1

1) The version number signifies a newer version of the database, i.e. a modification to it's schema.

2) The creation of new tables changes the database schema, so you should increment the version.

4) From my experience, you can delete a database by using the 'adb uninstall your package', this is especially helpful if you have a content provider. The uninstall also removes the content provider. If you want to restart from a given point and say add tables to your database and re-test until you get it right you would do the adb uninstall ... and put a copy of your starting database in your projects asset folder and then use the supplied code to re-create the database if it doesn't exist.

5) There area several ways you can see your database files. have used 2 stand alone programs on my Windows 7 platform; SQLite Database Browser 2.0 b1 and sqlitestudio. If you are using eclipse you and add the plugin SQLLIteManager, com.questoid.sqlitemanager_1.0.0.jar.

Hope this helps.

Here's my source code from my dbhelper where I copy a database for the project if it doesn't exist.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TDAdbHelper extends SQLiteOpenHelper {

     private static String DATABASE_PATH;
 private static final String DATABASE_NAME = "tda.db";
 private static final int DATABASE_VERSION = 1;
 private Context context;
 private SQLiteDatabase db;

 TDAdbHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  this.context = context;
  String packageName = context.getPackageName();
  DATABASE_PATH = String.format("//data//data//%s//databases//", packageName);
  Log.i(this.getClass().toString(), "... before calling openDatabase ");
openDataBase();
  Log.i(this.getClass().toString(), "... after return openDatabase ");
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
     Log.i(this.getClass().toString(), "... Starting TDAdb.onCreate ");
  TDAdb.onCreate(db);
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  TDAdb.onUpgrade(db, oldVersion, newVersion);
 }
 //Performing a database existence check
 private boolean checkDataBase() {
     Log.i(this.getClass().toString(), "... Starting checkDatabase ");
     SQLiteDatabase checkDb = null;
     try {
         String path = DATABASE_PATH + DATABASE_NAME;
         checkDb = SQLiteDatabase.openDatabase(path, null,
                       SQLiteDatabase.OPEN_READONLY);
     } catch (SQLException e) {
         Log.e(this.getClass().toString(), "Error while checking db");
     }
     //Android doesn’t like resource leaks, everything should 
     // be closed
     if (checkDb != null) {
         checkDb.close();
     }
     return checkDb != null;
 }

 //Method for copying the database
 private void copyDataBase() throws IOException {
     //Open a stream for reading from our ready-made database
     //The stream source is located in the assets
     Log.i(this.getClass().toString(), "... in copyDataBase ");
     InputStream externalDbStream = context.getAssets().open(DATABASE_NAME);

      //Path to the created empty database on your Android device
     String outFileName = DATABASE_PATH + DATABASE_NAME;

      //Now create a stream for writing the database byte by byte
     OutputStream localDbStream = new FileOutputStream(outFileName);

      //Copying the database
     byte[] buffer = new byte[1024];
     int bytesRead;
     while ((bytesRead = externalDbStream.read(buffer)) > 0) {
         localDbStream.write(buffer, 0, bytesRead);
     }
     //Don’t forget to close the streams
     localDbStream.close();
     externalDbStream.close();
 }
 //This piece of code will create a database if it’s not yet created
 public void createDataBase() {
     Log.i(this.getClass().toString(), "... in createDataBase ");
     boolean dbExist = checkDataBase();
     if (!dbExist) {
         this.getReadableDatabase();
         try {
             copyDataBase();
         } catch (IOException e) {
             Log.e(this.getClass().toString(), "Copying error");
             throw new Error("Error copying database!");
         }
     } else {
         Log.i(this.getClass().toString(), "Database already exists");
     }
 }

 public SQLiteDatabase openDataBase() throws SQLException {
     String path = DATABASE_PATH + DATABASE_NAME;
     Log.i(this.getClass().toString(), "Starting openDatabase " + path);
     if (db == null) {
        createDataBase();
        db = SQLiteDatabase.openDatabase(path, null,
             SQLiteDatabase.OPEN_READWRITE);
     }

     return db;  
}

}

Notice the modification I've made in the db.java

*

/*   private static final String DATABASE_CREATE =
      "CREATE TABLE if not exists " + CHAPTER_TABLE + " (" +
       KEY_ROWID + " integer PRIMARY KEY autoincrement," +
       COL_CHAPTER + "," +
       COL_CHAPTERTITLE + "," +
    //   KEY_CONTINENT + "," +
       " UNIQUE (" + COL_CHAPTER +"));"; */

     public static void onCreate(SQLiteDatabase db) {
          Log.i(LOG_TAG, "onCreate");
     }

     public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to "
        + newVersion + ", which will destroy all old data");
      onCreate(db);
     }
}

*

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.