13-Ma’ruza Mavzu: Ma’lumotlar bazasi. Reja: sqlite ma'lumotlar bazasiga ulanish



Download 83,29 Kb.
bet8/10
Sana23.06.2023
Hajmi83,29 Kb.
#953137
1   2   3   4   5   6   7   8   9   10
Bog'liq
13-maruza

activity_user.xml faylida ma'lumotlarni qo'shish/yangilash/o'chirish uchun umumiy shaklni belgilang:


xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Введите имя"
app:layout_constraintBottom_toTopOf="@+id/year"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

android:id="@+id/year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Введите год рождения"
app:layout_constraintTop_toBottomOf="@+id/name"
app:layout_constraintBottom_toTopOf="@+id/saveButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

android:id="@+id/saveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Сохранить"
android:onClick="save"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="@+id/year"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/deleteButton"
/>

android:id="@+id/deleteButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Удалить"
android:onClick="delete"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="@+id/year"
app:layout_constraintLeft_toRightOf="@+id/saveButton"
app:layout_constraintRight_toRightOf="parent"
/>

Shuningdek, UserActivity kodini o'zgartiring:


package com.example.sqliteapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class UserActivity extends AppCompatActivity {
EditText nameBox;
EditText yearBox;
Button delButton;
Button saveButton;
DatabaseHelper sqlHelper;
SQLiteDatabase db;
Cursor userCursor;
long userId=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
nameBox = (EditText) findViewById(R.id.name);
yearBox = (EditText) findViewById(R.id.year);
delButton = (Button) findViewById(R.id.deleteButton);
saveButton = (Button) findViewById(R.id.saveButton);
sqlHelper = new DatabaseHelper(this);
db = sqlHelper.getWritableDatabase();
Bundle extras = getIntent().getExtras();
if (extras != null) {
userId = extras.getLong("id");
}
// agar 0 bo'lsa, qo'shiladi
if (userId > 0) {
// ma'lumotlar bazasidan id bo'yicha elementni oling
userCursor = db.rawQuery("select * from " + DatabaseHelper.TABLE + " where " +
DatabaseHelper.COLUMN_ID + "=?", new String[]{String.valueOf(userId)});
userCursor.moveToFirst();
nameBox.setText(userCursor.getString(1));
yearBox.setText(String.valueOf(userCursor.getInt(2)));
userCursor.close();
} else {
// o'chirish tugmachasini yashirish
delButton.setVisibility(View.GONE);
}
}
public void save(View view){
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.COLUMN_NAME, nameBox.getText().toString());
cv.put(DatabaseHelper.COLUMN_YEAR, Integer.parseInt(yearBox.getText().toString()));
if (userId > 0) {
db.update(DatabaseHelper.TABLE, cv, DatabaseHelper.COLUMN_ID + "=" + String.valueOf(userId), null);
} else {
db.insert(DatabaseHelper.TABLE, null, cv);
}
goHome();
}
public void delete(View view){
db.delete(DatabaseHelper.TABLE, "_id = ?", new String[]{String.valueOf(userId)});
goHome();
}
private void goHome(){
// ulanishni yoping
db.close();
// asosiy activity ga o'tish
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
}

Ob'ektni ro'yxatdagi asosiy activitydan yangilash yoki o'chirish paytida ob'ektning id UserActivity ga o'tkaziladi:


long userId=0;
//.................
Bundle extras = getIntent().getExtras();
if (extras != null) {
userId = extras.getLong("id");
}

Agar id MainActivity dan o'tmagan bo'lsa, biz uning qiymatini 0 ga o'rnatdik, shuning uchun biz tahrir/o'chirishni emas, balki qo'shishni olamiz.


Agar id aniqlangan bo'lsa, u holda biz ma'lumotlar bazasidan tahrir qilish/o'chirish uchun ob'ekt olamiz:
if (id < 0) {
userCursor = db.rawQuery("select * from " + DatabaseHelper.TABLE + " where " +
DatabaseHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)});
userCursor.moveToFirst();
nameBox.setText(userCursor.getString(1));
yearBox.setText(String.valueOf(userCursor.getInt(2)));
userCursor.close();
}
Aks holda, o'chirish tugmachasini yashiring.

SQLiteDatabase da ma'lumotlarni kiritish, yangilash va o'chirish bo'yicha operatsiyalarni bajarish uchun insert(), update() va delete() usullari mavjud. Ushbu usullar tugma ishlovchilarida chaqiriladi:


db.delete(DatabaseHelper.TABLE, "_id = ?", new String[]{String.valueOf(id)});

Jadval nomi delete () usuliga, shuningdek o'chiriladigan ustun va uning qiymatiga o'tkaziladi. Siz mezon sifatida bir nechta ustunlarni tanlashingiz mumkin, shuning uchun uchinchi parametr - bu massiv. So'roq belgisi ? uchinchi parametrning qiymati bilan almashtirilgan parametrni bildiradi.



Download 83,29 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish