Muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti qarshi filiali “Kompyuter injiniringi”


onCreate — database yaratish uchun   onUpgrade



Yüklə 1,12 Mb.
səhifə5/5
tarix03.06.2023
ölçüsü1,12 Mb.
#124257
1   2   3   4   5
FpdnhpoVYXwgez6keMBioGzUWosvz5b2

onCreate database yaratish uchun  
onUpgrade — yaratilgan database ni yangilash uchun  
onCreate metodini ichiga quydagilarni yoziladi:
@Override public void onCreate(SQLiteDatabase db)
{ // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); }
Endi quyidagi o’zgaruvchilarni yaratamiz, bu o’zgaruvchilar database bilan ishlash uchun har doim kerak bo’ladigan o’zgaruvchilar bo’lganligi uchun hususiyatini public qilamiz
public static final String MYDATABASE_NAME = "opennet";
public static final String MYDATABASE_TABLE = "opennetchilar";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String ism = "ism";
public static final String familya = "familya";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ MYDATABASE_TABLE
+ " (" + KEY_ID + " integer primary key autoincrement, " + ism + " text not null, " + familya + " text not null);";
private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;
 private Context context;
Endi database yaratish funksiyasiga kelsak, quyidagikodni yozamiz
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
SQLiteHelper klassiga murojaat qiladi va onCreate metodi ishga tushadi, va bizning databaseimizni yaratib beradi.
Eslatma:database ni 1 marta yaratadi,agar shunday nomli database bor bo’lsa yaratmaydi.
SqliteAdapter klassimizni ichida quyidagi funksiyalarni yaratamiz:
openToRead() — nomidan ham ma’lumki, databaseni o’qishga ruxsat olish uchun public SQLiteAdapter openToRead() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; }
openToWrite() — nomidan ham ma’lumki, databaseni tahrirlash uchun ruxsat olish
public SQLiteAdapter openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this; }
Va nihoyat databaseni ishlatib bo’lganimizdan so’ng, uni yopish.
public void close(){ sqLiteHelper.close();
 }
Database ga ma’lumot qo’shish funksiyasi
public long insert(String content1, String content2){ ContentValues contentValues = new ContentValues(); contentValues.put(ism, content1); contentValues.put(familya, content2);  
return
sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);  
}
Database dagi barcha ma’lumotlarni o’chirish funksiyasi
 public int deleteAll(){ return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); }
 Databasidagi barcha ma’lumotlarni olish uchun ishlatiladigan funksiya
public Cursor queueAll(){ String[] columns = new String[]{KEY_ID, ism, familya};  
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null);
return cursor; }
Endi SqliteAdapter imiz dan yangi object yaratamiz va database ga yozish uchun ruxsat beruvchi funksiyani ishga tushiramiz:
mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite();
 Database dagi barcha ma’lumotlarni oluvchi funksiyani ishga tushiramiz:  
cursor = mySQLiteAdapter.queueAll(); SimpleCursorAdapter — bu Adapter bizga databasedagi malumotlarni ro’yhatda ko’rsatishga yordam beradi. Databasedan barcha ma’lumotlarni olganimizdan keyin uni ListView ga joylaymiz:
String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.ism, SQLiteAdapter.familya}; int[] to = new int[]{R.id.id, R.id.text1, R.id.text2}; cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); opennetchilar.setAdapter(cursorAdapter);
Endi Button tugmalari bilan ishlash qoldi yani yangi ma’lumot qo’shish tugmasi va barcha ma’lumotlarni o’chirish tugmasi:
Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0)
{ // TODO Auto-generated method stub String data1 = ism.getText().toString();
String data2 = familya.getText().toString();
mySQLiteAdapter.insert(data1, data2);
updateList(); }
};
Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0)
{ // TODO Auto-generated method stub mySQLiteAdapter.deleteAll();
updateList(); }
 };
Endi Dasturni ishga tushiramiz va ma’lumot kiritib Add tugmasini bosamiz:


Yüklə 1,12 Mb.

Dostları ilə paylaş:
1   2   3   4   5




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin