Showing posts with label SQLite. Show all posts
Showing posts with label SQLite. Show all posts

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

Download and Install SQLite Database Browser

How to Download and Install SQLite Database Browser

1. Download the free SQLite Database Browser .zip file to your computer. (It is roughly 7 MB.)

2. Extract the .zip file contents.
     If using Windows XP, you can use WinRAR to extract the files.

3. Open the SQLite Database Browser 2.0 b1.exe to open the SQLite Databas Browser.



Create a SQLite database using SQLiteDatabase Browser, to include in your Android project

How to Create a SQLite database using SQLite Database Browser

1. If you don't already have an Android project, create an Android project.

2. Download and install SQLite Database Browser.

3. Create a new directory named assets in your Android app. Example: MyAppName\assets
The new database will need to be located here for Android to include it in your project.

4. Open the SQLite Database Browser software.


5. Click File - New Database.  Navigate to your App directory, then the assets folder. Type the name of your database you'd like to create and click the Save button. I will use the name mySQLiteDatabase here.



6. Another pop-up screen will display for you to enter a Table name. A database can contain many tables. I will call the table myTable1.



7. Now we will need to add our fields to the new Table called myTable1. Click the Add button below the Field name section. The first field that we need to create is the mandatory _id field. Add a field called _id and the select the Field Type of Integer Primary Key then click the Create button. (As I said, this field is mandatory. If it is not included, it will not work correctly in Android.)



8. Let's create another field named myField1 with the Field Type as Text  by clicking the Add button typing the information, and clicking the Create button. 


9. Click the Create button on the Create table window to create the table and field.



10. To add some test data, click the Browse data tab at the top. Then click the New Record button on the right.



11. A new record box will appear on the screen. Double-click the new box to add the new data. Type "This is a new record." Then click the Apply Changes button. (If you just click the Close button, your data will not be saved.)



12. For Android to recognize your new SQLite database, you will need to add a few more items. Click the Execute SQL tab. Copy the following line into the SQL string, and click the Execute query button.

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')


  1. 13. Under the Execute query button, the Error message from database engine: should display No error.
  2.  


    14. Now we need to add a single row to this new table. Copy the following line into the SQL string, and click the Execute query button.

    INSERT INTO "android_metadata" VALUES ('en_US')



  3. 15. Again, No error, should display under the Execute query button under the Error message from database engine.



16. You can now close the SQLite Database Browser software. Click yes to save any changes, if asked.

Next Recommended Article: How to Add an External SQLite database

Resources:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/



Web hosting