How to Add an External SQLite database

How to Add an External SQLite database

API Level 1, or greater

This will add an SQLite database that is already created, and possibly populated with data.

1. You will need to add your database to the Andriod project files. You can either create your database ahead of time, or create the database on demand, in Java.

2. Create a File Reference for your database. Name it myFile, with the location below. Replace mypackagename with your package name. Replace mySQLiteDatabase with your database name.

"/data/data/com.akp.mypackagename/mySQLiteDatabase.db"

3. Check if the File does NOT exist using the myFile name. (Note: This is the 2nd one on the Check if File exists page (Step #3)). The else is not needed. Add all of the rest of the below steps inside of the if statement.

4. Create a new FileOutputStream saved to myOutputStream with the FileOutStream below. Replace mypackagename with your package name.

 "/data/data/com.akp.mypackagename/mySQLiteDatabase.db"

5. Within this try, add the following code after the new FileOutputStream line of code.
Open an InputStream with the database file: "mySQLiteDatabase.db"

6. Within this try, add the following after the this.getAssets line of code.

     a. Declare an integer named intLength.

     b. Declare and Set an Byte Array named myByteArray and set to 1024 elements.

     c. Add a try/catch with a IOException exception. The finally is not needed.

             1. Within this try, Add a while loop with the expression below.
                 This will read bytes into myInputStream.

                 ((intLength = myInputStream.read(myByteArray))>0)

                  a. Inside the while loop, Write from Byte Array Buffer, with a buffer named myByteArray,
                      start at 0, and the variable intLength for the length.

7. Compile and run!


Web hosting