Android development operation database

Android's database is SQLite, and the sql statements used are all interoperable. Let's not talk about it here. Look at the code:
static ArrayAdapter adapter;
    ImageView img;
    Button add;
    Button delete;
    Button updata;
    Button select;
    ListView liv;
    SQLiteDatabase db;
    ArrayList<User> users = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day06);
//        img = findViewById(R.id.img);
//        new MyAsyncTask().execute("http://upload.jianshu.io/users/upload_avatars/6416344/5e103cde-c77d-49f9-8a47-2ed4418c564a.jpg");

        add = findViewById(R.id.add);
        delete = findViewById(R.id.delete);
        updata = findViewById(R.id.updata);
        select = findViewById(R.id.select);
        liv = findViewById(R.id.liv);
        //Create database object first
        MyDataBase base = new MyDataBase(this);
        db = base.getWritableDatabase();
        //increase
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAdd();
                users.clear();
                getSelect();
            }
        });
        //delete
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDelete();
                users.clear();
                getSelect();
            }
        });
        //modify
        updata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUpData();
                users.clear();
                getSelect();
            }
        });
        //query
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                users.clear();
                getSelect();
            }
        });
    }
The defined database class inherits SQLiteOpenHelper
public class MyDataBase extends SQLiteOpenHelper {
    public MyDataBase(Context context) {
        super(context, "mydb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //New only once
        db.execSQL("create table jiao (_id integer primary key autoincrement,name varchar(10),age integer)");
        db.execSQL("insert into jiao values(null,'Jiao Zi Han',20)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Update version number
    }
}
Encapsulate sql statements of various operations into various methods
/**
     * Add data method
     */
    public void getAdd(){
//        Db.execsql ("insert into Jian values (null, 'jiaoziao', 20)");
        ContentValues values = new ContentValues();
        values.put("name","Jiao Zi o");
        values.put("age",20);
        db.insert("jiao",null,values);
    }
    /**
     * Deletion method
     */
    public  void getDelete(){
//        Db.execsql ("delete from Jiang where name = 'Jiao Zihan'");
        db.delete("jiao","name = ?",new String[]{"Jiao Zi Han"});
    }
    /**
     * Modification method
     */
    public void getUpData(){
//        Db.execsql ("update Jian set name = 'Jiao Xue' where name =?", new string [] {"Jiao Zi Ao"});
        ContentValues values = new ContentValues();
        values.put("name","Jiao Xue");
        db.update("jiao",values,"name = ?",new String[]{"Jiao Zi o"});
    }
    /**
     * Query method
     */
    public void getSelect(){
//        Cursor cursor = db.rawQuery("select * from jiao", null);
//        while(cursor.moveToNext()){
//            String name = cursor.getString(cursor.getColumnIndex("name"));
//            int age = cursor.getInt(cursor.getColumnIndex("age"));
//            int id = cursor.getInt(cursor.getColumnIndex("_id"));
//            users.add(new User(name,age,id));
//        }
        Cursor cursor = db.query("jiao", null, null, null, null, null, null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            users.add(new User(name,age,id));
        }
        adapter = new ArrayAdapter(Activity_day06.this,R.layout.support_simple_spinner_dropdown_item,users);
        liv.setAdapter(adapter);
    }
There are two methods for adding, deleting, modifying and querying, both of which can be used. The second method has different parameters, which depends on your own situation;

If you want to add to the ListView, create a collection and put the data into the collection.

Classes that store adapter collection data
public class User{
        String name;
        int age;
        int id;

        public User(String name, int age, int id) {
            this.name = name;
            this.age = age;
            this.id = id;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", id=" + id +
                    '}';
        }
    }

Tags: Database SQL Android SQLite

Posted on Sun, 10 Nov 2019 14:58:36 -0500 by pesale86