Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.0 kB
4
Indexable
Never
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class KiloDatabase {

    private static final String DATABASE_NAME = "kilo.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "kilo_table";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_TARIH = "tarih";
    private static final String COLUMN_KILO = "kilo";
    private static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + COLUMN_TARIH + " TEXT,"
                    + COLUMN_KILO + " REAL"
                    + ")";

    private SQLiteDatabase db;
    private Context context;

    public KiloDatabase(Context context) {
        this.context = context;
    }

    public void open() {
        db = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
        db.execSQL(CREATE_TABLE);
    }

    public void close() {
        db.close();
    }

    public void addKilo(String tarih, double kilo) {
        String insert = "INSERT INTO " + TABLE_NAME
                + " (" + COLUMN_TARIH + ", " + COLUMN_KILO + ")"
                + " VALUES ('" + tarih + "', " + kilo + ")";
        db.execSQL(insert);
    }

    public void listKilo() {
        String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COLUMN_TARIH + " ASC";
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            do {
                String tarih = cursor.getString(cursor.getColumnIndex(COLUMN_TARIH));
                double kilo = cursor.getDouble(cursor.getColumnIndex(COLUMN_KILO));
                Log.d("Kilo", "Tarih: " + tarih + ", Kilo: " + kilo);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }
}