martes, 7 de enero de 2014

Connect DB SQLite to Android With Android Studio

Connect DB SQLite to Android With Android Studio 


DOWNLOAD ALL THE PROYECT HERE

One of the first and more important information are: How to connect SQLite with Android app?
For this tutorial I am using Android Studio. You can download this from HERE

First we need to create we database. I am using SQLite Manager (this is a plugin for Firefox).
I create a Data Base named "example" and a table named "exampleTable".

CREATE TABLE exampleTable ( name VARCHAR(200) NULL DEFAULT NULL, lastname VARCHAR(200) NULL DEFAULT NULL, ID VARCHAR(20) NULL DEFAULT NULL )

INSERT INTO exampleTable VALUES('Jhon', 'Sweet', '1');
INSERT INTO exampleTable VALUES('Audrey', 'Owen', '2');
INSERT INTO exampleTable VALUES('Michelle', 'Peters', '3');
INSERT INTO exampleTable VALUES('Helen', 'Black', '4');
INSERT INTO exampleTable VALUES('Jessica', 'Sweet', '5');
When the database are create, we need to put in the assent folder, in the Android Studio Proyect.


Then we create a Connection class.
In this class are two important lines in this class:
    private static String DB_PATH = "/data/data/com.example.databaseconnect/databases/";
    private static String DB_NAME = "example.sqlite";


HOW TO CREATE APPS. GO HERE

The DB_PATH is the folder where the Data Bare are. The path of this are "/data/data/Pakage Name/databases/"
And DB_NAME is the Data Base name.

You can see the Data Base in the "Android Debug Monitor". To open the Android Debug Monitor you need go to the Android icon ( ).
Then show this:
We need to use the DDMS. This right up.
Then we select in the left the "Package name", in my case it would be "com.example.databaseconnect"

And then in the right go to: data -> data -> package name -> database, in this folder are the Data Base.

Return to the code, you need to create, copy, check, open and then connect the Data Base to the Device, for this in the Connect.java class create the nexts functions.



Then create another class named "DataDB.java" to connect with "Connect.java" class


And to finish in the MainActivity.java, only call the getNameDB and DONE.

DOWNLOAD THE FULL PROYECT HERE
TRAFFIC FOR YOUR APPS

5 comentarios:

  1. Thanks mate, I'll try this out with a sqlitedb that already exists.

    ResponderEliminar
  2. thanks this will assist me to develop my own app

    ResponderEliminar
  3. thanks this will assist me to develop my own app

    ResponderEliminar
  4. thanks, but do u know how to insert data and make changes to the sqlite database file in assets folder?

    ResponderEliminar