4-topshiriq. Dastur yoki loyiha asosida ma'lumotlar bazasini shakllantirish sqliteOpenHelper yordamida ma'lumotlar bazasini yaratish


Download 63.68 Kb.
Sana15.12.2020
Hajmi63.68 Kb.
#167884
Bog'liq
ikut 1 4 topshiriq
6-lab jadval, 6-lab jadval, 3-seminar, оралиқ назорат саволлари, 28-may 5-variant, Homidov M.A. [AB 215] Analog signallarni raqamli signallarga o'zgartirgichlar., Homidov M.A. [AB 215] Analog signallarni raqamli signallarga o'zgartirgichlar., Homidov M.A. [AB 215] Analog signallarni raqamli signallarga o'zgartirgichlar., investment, “IQTISODIYOT NAZARIYASI” fanidan uslubiy korsatma (kurs ishi yozish), Mamurova Gulzira kurs ishi, Mamurova Gulzira kurs ishi, manav5, manav5, manav5



4-topshiriq.Dastur yoki loyiha asosida ma'lumotlar bazasini shakllantirish
SQLiteOpenHelper yordamida ma'lumotlar bazasini yaratish

Ma'lumotlar bazasini yaratish va versiyasini boshqarishning eng oson usuli bu SQLiteOpenHelper subklassidir. Agar ular mavjud bo'lmasa ma'lumotlar bazalarini yaratish orqali SQLite ma'lumotlar bazasini boshqarishni soddalashtiradi. Ma'lumotlar bazasini yaratish yoki yangilash uchun kerakli harakatni ko'rsatish uchun faqat onCreate () va onUpgrade () usullarini bekor qilishingiz 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 bizning Android database SQLite misolida biz SQLite ma'lumotlar bazasini nomga o'rnatdik (sample_database). Konstruktor ma'lumotlar bazasi nomi va versiyasi bilan 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 jadvali uchta ustundan iborat: ismi, tavsifi va Found_date. Saqlash tugmachasini bosish saveToDB () usulini chaqiradi:

SaveToDB () da biz SQLiteOpenHelper-dan getWritableDatabase () usuli yordamida SQLiteDatabase ob'ektiga havola olamiz. Ushbu usul ma'lumotlar bazasini mavjud bo'lmagan taqdirda yaratadi yoki allaqachon yaratilgan bo'lsa 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 63.68 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2022
ma'muriyatiga murojaat qiling