faylida biz ma'lumotlar bazasi bilan o'zaro aloqani bog’laymiz:
package com.example.android.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
SQLiteDatabase db = getBaseContext().openOrCreateDatabase("app.db",
MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS users (ismi TEXT, yoshi
INTEGER)");
db.execSQL("INSERT INTO users VALUES ('Alisher', 24);");
db.execSQL("INSERT INTO users VALUES ('Bahrom', 30);");
Cursor query = db.rawQuery("SELECT * FROM users;", null);
TextView textView = findViewById(R.id.text_view);
if (query.moveToFirst()) {
do {
String ismi = query.getString(0);
int yoshi = query.getInt(1);
textView.append("Ismi: " + ismi + " Yoshi: " + yoshi + "\n");
}
while (query.moveToNext());
}
query.close();
db.close();
}
}
Bu ilovadagi tugmani bosish bilan avval app.db ma'lumotlar bazasida users yangi
jadvali yaratiladi, so'ngra unga INSERT yordamida ma'lumotlar bazasiga ikkita
ob'ekt qo'shiladi.
Keyinchalik, SELECT yordamida
barcha
qo'shilgan
foydalanuvchilarni
ma'lumotlar bazasidan Cursor kursori ko'rinishida olamiz.