1. playing with sqlite database sqlite : database specific name for android application for...

22
ANDROID DATABASE

Upload: asher-clark

Post on 18-Jan-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

ANDROID DATABASE

Page 2: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Android Database

1. Playing with SQLite Database SQLite : Database specific name for Android

Application For windows there are several kind of database

name : Mysql, SQL server, Oracle, Microsoft Access, etc. (Microsoft excel is not database!)

a. Create SQLite Database File b. Database Query (Scripting)c. Database Query (Interface)

2. Steps to create Database application in Android

Page 3: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Playing with SQLite Database

Page 4: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

1. Create SQLite Database File1. Open “SQLite Database Browser

2.0 b1.exe”2. Ctrl+N (File-New Database)3. Select directory and give name you

want to save database file (file name for database, example : data)

Page 5: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

2. Basic Database Query (Scripting)1. Create Database (Create New Database)

unavailable in SQLite2. Drop Database (Delete Existing Database)

unavailable in SQLite3. Create Table and its Field (Create New Table

and its Field)4. Drop Table (Delete Existing Table)5. Insert Data (Add New Data)6. Update Data (Modify Existing Data)7. Select Data (Read Existing Data)8. Delete Data (Delete Existing Data)

Page 6: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 3. CreateTable Name and Field Name

CREATE TABLE <namatabel> (namafield1 tipedatafield1, namafield2 tipedatafield2 , namafield3 tipedatafield3,…, namafieldN tipedatafieldN)Example :Assumption :Table name : kelasmobappField names : NIM (integer primary key), Nama (Text)CREATE TABLE kelasmobapp (nim int,nama text)

Page 7: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 4. Drop Name

DROP TABLE <namatabel>Example :

Assumption :Table name : kelasmobapp

DROP TABLE kelasmobapp

Page 8: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 5. Add new dataINSERT INTO <namatabel> VALUES (datafield1,datafield2)Example :

Assumption :Table name : kelasmobappField names : NIM (integer primary key), Nama (Text)Data inserted : NIM= 111100001, Nama=’Heri Suprapto’

INSERT INTO kelasmobapp VALUES (111100001,’Heri Suprapto’)

Page 9: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 6. Update DataUPDATE <namatabel> SET <namafield>=<isidatafield> WHERE <namafield>=<nilaitertentu>Example :

Assumption :Table name : kelasmobappField names : NIM (integer primary key), Nama (Text)Data updated : NIM= 111100001, Nama=’Heri Suprapto’ will be updated to be ’Herawati Suprapti’

UPDATE kelasmobapp SET Nama=‘Herawati Suprapti’ WHERE NIM=111100001

Page 10: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 7. Read DataSELECT <namafield1>,<namafield2>,<namafield3>,….,<namafieldN> FROM <namatabel>Example :

Assumption :Table name : kelasmobappField names : NIM (integer primary key), Nama (Text)Data read : NIM

SELECT nim FROM kelasmobappAll field :SELECT * From kelasmobapp

Page 11: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

Basic Query Script : 8. Delete DataDELETE FROM <namatabel> WHERE <namafield>=<nilaitertentu>Example :

Assumption :Table name : kelasmobappField names : NIM (integer primary key), Nama (Text)Data deleted : NIM= 111100001, Nama=’Heri Suprapto’ will be deleted

DELETE FROM kelasmobapp WHERE NIM=111100001

Page 12: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

STEPS TO MAKE ANDROID DATABASE

Page 13: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

A. Preparing the SQLite Database file

1. Open “SQLite Database Browser 2.0 b1.exe”2. Ctrl+N (File-New Database)3. Select directory and give name you want to save

database file (file name for database example : data)

4. Make a name for the database table, and klik add5. Add minimum a field name and type, click create6. Add other field as you want7. After finish, you can click create8. Save the database (File-Save Database), you now

have a database file : data (without extension) 9. Make new data to all field, use GUI and query script.10. Close “SQLite Database Browser 2.0 b1.exe”

Page 14: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

B. Prepare the interface xml file

Page 15: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

C. Prepare DatabaseHelper.java : 1st Alternative1. Create Class DataBaseHelper.java to your project2. Set DB_NAME in DataBaseHelper.java to database file name of

your project which you have created when preparing the SQLite Database file (data)

3. Load your database file in DDMS , steps :a. Click File Explorer tabb. Find your package (if not existing, run your project in the first time then try

to find again)c. Create folder databasesd. In databases folder, load your database filee. You can view the table and the content by clicking on the database file and

click SQLite Browser4. Create your xml interface file5. Create your own script

Page 16: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

C. Prepare DatabaseHelper.java : 2nd Alternative

1. Create Class DataBaseHelper.java to your project

2. Set DB_NAME in DataBaseHelper.java to database file name of your project which you have created when preparing the SQLite Database file (data)

3. Make new class for activity, example testing.java:

4. Make your database file based on script (in onCreate method):try {datavar.execSQL("create table tabel(no

int,status teks, alamat teks,nim teks,nama teks);");

} catch (Exception exception) {Toast.makeText(Cobadb1Activity.th

is, "Database sudah ada",Toast.LENGTH_SHORT).show();}

Note about datavar :

SQLiteDatabase datavar; //before onCreate

DataBaseHelper db1 = new DataBaseHelper(this); // inside onCreate

datavar = db1.getWritableDatabase(); // inside onCreate

5. Create your own script

Page 17: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

D. Prepare .java File to access (Write) Database

1. CREATEdatavar.execSQL("create table tabel(no

int,status teks, alamat teks,nim teks,nama teks);");

2. INSERT3. SELECT4. UPDATE5. DELETE

Page 18: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

INSERT (1ST ALTERNATIVE)

datavar.execSQL("insert into

tabel(Status,no,Alamat,NIM,Nama) values ('"

+ Status+ "','"+ no+ "','"+ Alamat+ "','" + NIM + "','" + Nama + "'); ");

Page 19: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

INSERT (2ND ALTERNATIVE)

ContentValues CV = new ContentValues();

CV.put("Status", Status);CV.put("No", no);CV.put("Alamat", Alamat);CV.put("NIM", NIM);CV.put("Nama", Nama);datavar.insert("tabel", null, CV);

Page 20: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

SELECT

final Cursor db;db = datavar.rawQuery("select * from

tabel;", null);db.moveToFirst();nama.setText(db.getString(4));nim.setText(db.getString(3));alamat.setText(db.getString(2));

Page 21: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

UPDATE

datavar.execSQL("update tabel set nama='" + Nama + "',alamat='"

+ Alamat + "',status='" + Status + "' where NIM='"

+ NIM + "';");

Page 22: 1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,

DELETE

datavar.execSQL("delete from tabel where nama='"

+ Nama + "';");