• Android database SQLite
  • Employer
  • GetWritableDatabase
  • SQLiteOpenHelper yordamida ma'lumotlar bazasini yaratish




    Download 2,42 Mb.
    bet9/16
    Sana15.05.2024
    Hajmi2,42 Mb.
    #236485
    1   ...   5   6   7   8   9   10   11   12   ...   16
    Bog'liq
    Mobil 3

    SQLiteOpenHelper yordamida ma'lumotlar bazasini yaratish


    Ma'lumotlar bazasini yaratish va uning versiyasini boshqarishning eng oson usuli bu SQLiteOpenHelper kichik sinfidir. Ma'lumotlar bazasini yaratish orqali SQLite ma'lumotlar bazasini boshqarishni osonlashtiradi. Ma'lumotlar bazasini yaratish yoki yangilash uchun kerakli amalni belgilash uchun faqat onCreate () va onUpgrade () usullarini bekor qilish kerak.:
    public class SampleDBSQLiteHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "sample_database"; public SampleDBSQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(SampleDBContract.Employer.CREATE_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " +
    SampleDBContract.Employer.TABLE_NAME);
    onCreate(sqLiteDatabase);
    }
    }
    Endi, Android database SQLite misolida, biz SQLite ma'lumotlar bazasini
    nomga o'rnatdik (sample_database). Konstruktor ma'lumotlar bazasining nomi va versiyasi ko'rsatilgan superklass konstruktorini chaqiradi. OnCreate -da biz SQLiteDatabase ob'ektiga Employer CREATE_TABLE SQL bayonotini bajarishni buyuramiz. OnUpgrade orqali biz Employer jadvalini tashlaymiz va uni qayta yaratamiz:


    Employer jadvalining uchta ustuni bor: name, description и founded_date. Saqlash tugmachasini bosish saveToDB () usulini chaqiradi:


    SaveToDB () da biz SQLiteOpenHelper -dan getWritableDatabase () usuli yordamida SQLiteDatabase ob'ektiga havola olamiz. Bu usul, agar u mavjud bo'lmasa, ma'lumotlar bazasini yaratadi yoki agar u allaqachon yaratilgan bo'lsa, uni ochadi. GetWritableDatabase o'qish / yozishga ruxsat beruvchi SQLiteDatabase ob'ektini qaytaradi:
    private void saveToDB() { SQLiteDatabase database = new
    SampleDBSQLiteHelper(this).getWritableDatabase();
    ContentValues values = new ContentValues(); values.put(SampleDBContract.Employer.COLUMN_NAME,
    binding.nameEditText.getText().toString());
    values.put(SampleDBContract.Employer.COLUMN_DESCRIPTION, binding.descEditText.getText().toString());
    try {
    Calendar calendar = Calendar.getInstance(); calendar.setTime((new SimpleDateFormat("dd/MM/yyyy")).parse(
    binding.foundedEditText.getText().toString())); long date = calendar.getTimeInMillis();
    values.put(SampleDBContract.Employer.COLUMN_FOUNDED_DATE,
    date);
    }
    catch (Exception e) { Log.e(TAG, "Error", e);
    Toast.makeText(this, "Date is in the wrong format", Toast.LENGTH_LONG).show();
    return;
    }
    long newRowId = database.insert(SampleDBContract.Employer.TABLE_NAME, null, values);
    Toast.makeText(this, "The new Row Id is " + newRowId, Toast.LENGTH_LONG).show();
    }




    Download 2,42 Mb.
    1   ...   5   6   7   8   9   10   11   12   ...   16




    Download 2,42 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    SQLiteOpenHelper yordamida ma'lumotlar bazasini yaratish

    Download 2,42 Mb.