Erkan KESER

SQlite ile Local Kayıt - Java ile Android Studio

Android studio ve java kullanılarak Sqlite veritabanında ekleme, silme, listeleme ve güncelleme işlemleri basit haliyle anlatılmaktadır.

Adım 1: Uygulama Önizleme 

Basit bir görev listesi oluşturarak sqlite ile local kayıt yapılacak. Basit görevler tablosu(id, gorev, durum) olacak ve listelemek için textview kullanılacak.

 


Adım 2: Tasarımı Yap

activity_main.xml Dosyası içeriği kontrol edilmesi için paylaşılmıştır. Oluşturduğunuz proje adına ve sürümüne göre değişiklik gösterebilir. 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="20dp"
   tools:context=".MainActivity">
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="12dp"
       android:gravity="center"
       android:text="Görev Listesi"
       android:textSize="30sp" />
   <EditText
       android:id="@+id/etGorev"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="12dp"
       android:ems="10"
       android:hint="Görev Giriniz"
       android:inputType="text" />
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="12dp"
       android:orientation="horizontal">
       <Button
           android:id="@+id/btnEkle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="Ekle" />
       <Button
           android:id="@+id/btnSil"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="Sil" />
       <Button
           android:id="@+id/btnGoster"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="Göster" />
       <Button
           android:id="@+id/btnGuncelle"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="Güncelle" />
   </LinearLayout>
   <TextView
       android:id="@+id/tvDurum"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="12dp"
       android:gravity="center"
       android:padding="8dp"
       android:text="işlem Durumu"
       android:textSize="16sp" />
   <TextView
       android:id="@+id/tvSonuc"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginBottom="12dp"
       android:padding="8dp"
       android:text="Sonuç Alanı"
       android:textSize="20sp" />
</LinearLayout>

Adım 3: Java Kodlamasını Yap.

MainActivity.java Dosyası içeriği kontrol edilmesi için class içeriği paylaşılmıştır. PAKETADI kısmı sizin oluşturduğunuz proje adı şeklinde olmalıdır.  Oluşturduğunuz proje adına ve sürümüne göre değişiklik gösterebilir. 

 

package PAKETADI;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
   EditText gorev; TextView islemDurum, sonuc; Button btnEkle, btnSil, btnGoster, btnGuncelle;
   SQLiteDatabase db;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       EdgeToEdge.enable(this);
       setContentView(R.layout.activity_main);
       ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
           Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
           v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
           return insets;
       });
       gorev = findViewById(R.id.etGorev);
       islemDurum = findViewById(R.id.tvDurum);
       sonuc = findViewById(R.id.tvSonuc);
       btnEkle = findViewById(R.id.btnEkle);
       btnSil = findViewById(R.id.btnSil);
       btnGoster = findViewById(R.id.btnGoster);
       btnGuncelle = findViewById(R.id.btnGuncelle);
       db = this.openOrCreateDatabase("GorevListesi", MODE_PRIVATE, null);
       String tablo = "CREATE TABLE IF NOT EXISTS gorevler(id INTEGER PRIMARY KEY, gorev TEXT, durum Integer)";
       db.execSQL(tablo);
       listele();
       btnEkle.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               String grv = gorev.getText().toString();
               String sorgu="INSERT INTO gorevler(gorev, durum) VALUES(?, 0)";
               SQLiteStatement sql = db.compileStatement(sorgu);
               sql.bindString(1,grv);
               sql.execute();
               islemDurum.setText(grv +" Eklendi.");
               listele();
           }
       }); // btnEkle
       btnSil.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               int id = Integer.parseInt(gorev.getText().toString());
               String sorgu="DELETE FROM gorevler WHERE id=?";
               SQLiteStatement sql = db.compileStatement(sorgu);
               sql.bindLong(1,id);
               sql.execute();
               islemDurum.setText(id +". Kayıt Silindi.");
               listele();
           }
       }); // btnSil
       btnGoster.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               int id = Integer.parseInt(gorev.getText().toString());
               String sorgu="SELECT * FROM gorevler WHERE id=?";
               Cursor cursor = db.rawQuery(sorgu,new String[]{String.valueOf(id)});
               if (cursor.getCount() > 0)
               {
                   islemDurum.setText(id +". Kayıt Gösterildi.");
                   if(cursor.moveToNext()) {
                       String durum = (cursor.getInt(2) == 1)? "Tamamlandı": "Tamamlanmadı";
                       sonuc.setText(
                               "ID = "+cursor.getInt(0) + "\n" +
                               "Görev = "+cursor.getString(1) + "\n"+
                                       "Durum = " +  durum
                       );
                   }
               }
               else
               {
                   islemDurum.setText(id +". Kayıt Bulunamadı.");
                   listele();
               }
           }
       }); // btnGoster
       btnGuncelle.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               int id = Integer.parseInt(gorev.getText().toString());
               String sorgu="UPDATE gorevler SET durum = 1 WHERE id=?";
               SQLiteStatement sql = db.compileStatement(sorgu);
               sql.bindLong(1,id);
               sql.execute();
               islemDurum.setText(id +". Kayıt Güncellendi.");
               listele();
           }
       });

   }// OnCreate
   public void listele() {
       String sorgu = "SELECT * FROM gorevler";
       Cursor cursor = db.rawQuery(sorgu,null);
       // int kolonNo = cursor.getColumnIndex("gorev");
       sonuc.setText("");
       while(cursor.moveToNext()) {
           sonuc.setText(sonuc.getText().toString()+"\n" +
                   cursor.getString(0) +" - " + cursor.getString(1));
       }
   } // listele
} // Main