You are on page 1of 6

TUGAS PRAKTIKUM

SQLite
Untuk memenuhi tugas matakuliah Pengembangan Aplikasi Mobile
yang dibina oleh
Alif Akbar Fitrawan

Oleh
Ryan Frizqi Hidayat
361755401067

Kelas
2C

POLITEKKNIK NEGERI BANYUWANGI

JURUSAN TEKNIK INFORMATIKA

PRODI TEKNIK INFORMATIKA

2019
1. Source code DatabaseHelper
package com.example.ryan;

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

import java.util.ArrayList;

public class DatabaseHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME = "Mahasiswa";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_STUDENTS = "students";
private static final String KEY_ID = "id";
private static final String KEY_FIRSTNAME = "name";
private static final String CREATE_TABLE_STUDENTS = "CREATE TABLE " +
TABLE_STUDENTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_FIRSTNAME + " TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("table", CREATE_TABLE_STUDENTS);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENTS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS'" + TABLE_STUDENTS + "'");
onCreate(db);
}
public long addStudentDetail(String student) {
SQLiteDatabase db = this.getWritableDatabase();
// membuat kontent values
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, student);
long insert = db.insert(TABLE_STUDENTS, null, values);
return insert;
}
public ArrayList<String> getAllStudentList() {
ArrayList<String> studenstArrayList = new ArrayList<String>();
String name = "";
String selectQuery = "SELECT * FROM " + TABLE_STUDENTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
name = c.getString(c.getColumnIndex(KEY_FIRSTNAME));
studenstArrayList.add(name);
}
while (c.moveToNext());
Log.d("array",studenstArrayList.toString());
}
return studenstArrayList;
}
}
2. Source MainActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.41"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<EditText
android:id="@+id/enterData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Enter name" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="28dp"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
tools:layout_editor_absoluteX="27dp">

<Button
android:id="@+id/Store"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Store" />

<Button
android:id="@+id/getData"
android:layout_width="200dp"
android:layout_height="40dp"
android:text="Get All Name From Sqlite" />
</LinearLayout>

<TextView
android:id="@+id/tampil"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="44dp"
android:text=""
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
tools:layout_editor_absoluteX="0dp" />

<TextView
android:id="@+id/textView"
android:layout_width="276dp"
android:layout_height="57dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="7dp"
android:text="Enter Name to Store in SQLite"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/tampil"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.085" />

</android.support.constraint.ConstraintLayout>

package com.example.ryan;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


private Button Store, getData;
private EditText enterData;
private DatabaseHelper databaseHelper;
private TextView tampil;
private ArrayList<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
tampil = (TextView) findViewById(R.id.tampil);
Store = (Button) findViewById(R.id.Store);
getData = (Button) findViewById(R.id.getData);
enterData = (EditText) findViewById(R.id.enterData);
Store.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.addStudentDetail(enterData.getText().toString());
enterData.setText("");
Toast.makeText(MainActivity.this, "Stored Succesfully",
Toast.LENGTH_SHORT).show();
}
});
getData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList = databaseHelper.getAllStudentList();
tampil.setText("");
for (int i=0;i<arrayList.size();i++){
tampil.setText(tampil.getText().toString()+", "+arrayList.get(i));
}
}
});
}
}

Hasil
Jalankan program

You might also like