0% found this document useful (0 votes)
5 views

SQLiteDemo01 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQLiteDemo01 Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.

sqlitedemo01;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.sql.SQLData;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//Khai báo biến giao diện
EditText edtmalop, edttenlop, edtsiso;
Button btninsert, btndelete, btnupdate, btnquery;
// khai báo ListView
ListView lv;
ArrayList<String> mylist;//Data chứa thông tin lớp
ArrayAdapter<String> myadapter;
SQLiteDatabase mydatabase;
protected long backpressTime;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Tham chiếu id cho các biên giao diện
edtmalop = findViewById(R.id.edtmalop);
edttenlop = findViewById(R.id.edttenlop);
edtsiso = findViewById(R.id.edtsiso);
btninsert = findViewById(R.id.btninsert);
btndelete = findViewById(R.id.btndelete);
btnupdate = findViewById(R.id.btnupdate);
btnquery = findViewById(R.id.btnquery);
// Tạo ListView
lv = findViewById(R.id.lv);

mylist = new ArrayList<String>();


myadapter=new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,mylist);
lv.setAdapter(myadapter);

// Tạo và mở Cơ sở dữ liệu Sqlite


mydatabase = openOrCreateDatabase("qlsinhvien.db", MODE_PRIVATE,null);
// Tạo Table để chứa dữ liệu
try {
String sql = "CREATE TABLE tbllop(malop TEXT primary key,tenlop
TEXT, siso INTEGER)";
mydatabase.execSQL(sql);
} catch (Exception e) {
Log.e("Lỗi", "Table đã tồn tại trong ứng dụng");
}
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String malop = edtmalop.getText().toString();
String tenlop = edttenlop.getText().toString();
int siso = Integer.parseInt(edtsiso.getText().toString());
ContentValues myvalue = new ContentValues();
myvalue.put("malop", malop);
myvalue.put("tenlop", tenlop);
myvalue.put("siso", siso);
String msg = "";
if (mydatabase.insert("tbllop", null, myvalue) == -1) {
msg = "Fail to Insert Record!";
} else {
msg = "Insert record Sucessfully";
}
Toast.makeText(MainActivity.this, msg,
Toast.LENGTH_SHORT).show();
}
});
// Xử lý sự kiện trên các Button
btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String malop = edtmalop.getText().toString();
int n = mydatabase.delete("tbllop", "malop = ?", new String[]
{malop});
String msg = "";
if (n == 0) {
msg = "No record to Delete";
} else {
msg = n + " record is deleted";
}
Toast.makeText(MainActivity.this, msg,
Toast.LENGTH_SHORT).show();
}
});
btnupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int siso = Integer.parseInt(edtsiso.getText().toString());
String malop = edtmalop.getText().toString();
ContentValues myvalue = new ContentValues();
myvalue.put("siso", siso);
int n = mydatabase.update("tbllop", myvalue, "malop = ?", new
String[]{malop});
String msg = "";
if (n == 0) {
msg = "No record to Update";
} else {
msg = n + " record is updated";
}
Toast.makeText(MainActivity.this, msg,
Toast.LENGTH_SHORT).show();
}
});
btnquery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mylist.clear();
Cursor c = mydatabase.query("tbllop", null, null, null, null,
null, null);
c.moveToFirst();
String data = "";
while (c.isAfterLast() == false) {
data = c.getString(0) + " - " + c.getString(1) + " - " +
c.getString(2);
c.moveToNext();
mylist.add(data);
}
c.close();
myadapter.notifyDataSetChanged();
}
});

You might also like