Android advanced - using LitePal to operate database

Learn to use LitePal to operate database

About LitePal:
LitePal is an Android open source library, which makes it very easy for developers to use SQLite database. You can do most of the database operations without writing a single SQL statement, including creating tables, updating tables, constraining operations, aggregation functions, etc.

Basic usage of LitePal

1. Edit build.gradle file to import Jar package

dependencies {
    compile 'org.litepal.android:core:1.3.0'
}

2. Configure literal.xml

Create a literal.xml file in the assets directory and copy the following code into it:
Here, the steps to create the literal.xml file are: Project - > main Directory - > new Directory package name is assets - > new file name is literal.xml

Next, write these lines in literal.xml

<?xml version="1.0" encoding="utf-8"?>
<litepal>

    <dbname value="demo" />


    <version value="1" />


    <list>

    </list>
</litepal>

3. Create a table

Here I create two entity classes, Album and Song

Entity class Album

public class Album extends DataSupport{
    @Column(unique = true, defaultValue = "unknown")
    private String name;

    private float price;

    private Date releaseDate;

    private List<Song> songs = new ArrayList<Song>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public List<Song> getSongs() {
        return songs;
    }

    public void setSongs(List<Song> songs) {
        this.songs = songs;
    }
}

Entity class Song

public class Song extends DataSupport{
    @Column(nullable = false)
    private String name;

    private int duration;

    @Column(ignore = true)
    private String uselessField;

    private Album album;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public String getUselessField() {
        return uselessField;
    }

    public void setUselessField(String uselessField) {
        this.uselessField = uselessField;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }
}

4. Modify the literal.xml file in the assets directory

   <list>
        <mapping class="com.example.lenovo.myapplication.Album" />
        <mapping class="com.example.lenovo.myapplication.Song" />
    </list>

5. Configure LitePalApplication in Android manifest.xml

android:name="org.litepal.LitePalApplication"

Then you can add, delete, modify and check the data

First add a button to the xml

<EditText
        android:id="@+id/album_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Add album name" />

    <Button
        android:id="@+id/add_album_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Add album" />

    <Button
        android:id="@+id/delete_album_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Delete album" />

    <EditText
        android:id="@+id/update_album_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Revised album" />

    <Button
        android:id="@+id/update_album_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Revised album" />

    <Button
        android:id="@+id/query_album_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Search album" />

Add, delete, modify and query in MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText albumET;
    private Button addAlbumBtn;
    private Button deleteAlbumBtn;
    private Button updateAlbumBtn;
    private Button queryBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        SQLiteDatabase db = LitePal.getDatabase();

        bindID();
    }

    private void bindID() {

        albumET=findViewById(R.id.album_et);
        addAlbumBtn=findViewById(R.id.add_album_btn);
        deleteAlbumBtn=findViewById(R.id.delete_album_btn);
        updateAlbumBtn=findViewById(R.id.update_album_btn);
        queryBtn=findViewById(R.id.query_album_btn);
        addAlbumBtn.setOnClickListener(this);
        deleteAlbumBtn.setOnClickListener(this);
        updateAlbumBtn.setOnClickListener(this);
        queryBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //increase
            case R.id.add_album_btn:

                String name=albumET.getText().toString();
                float price=101f;

                Album album=new Album();
                album.setName(name);
                album.setPrice(price);
                album.save();
                Toast.makeText(this, "Add success", Toast.LENGTH_SHORT).show();
                break;

                //delete
            case R.id.delete_album_btn:
                int AL=DataSupport.delete(Album.class, 1);
                Toast.makeText(this, "Delete successful", Toast.LENGTH_SHORT).show();
                break;

                //modify
            case R.id.update_album_btn:
                Album albumToUpdate = DataSupport.find(Album.class, 1);
                albumToUpdate.setPrice(99.99f); // raise the price
                albumToUpdate.save();
                Toast.makeText(this, "Modified success", Toast.LENGTH_SHORT).show();
                break;

                //lookup
            case R.id.query_album_btn:
//                List < album > albumlist = datasupport. Findall (album. Class); / / query all data

                List<Album>albumList=DataSupport.where("name like ?","a%").find(Album.class);
                Toast.makeText(this, "Search success", Toast.LENGTH_SHORT).show();

                for(Album a:
                        albumList){
                    Log.e("MAIN",a.getName()+"******");
                }
                break;
        }
    }
}

Keywords: Android xml Database SQLite

Added by freeloader on Wed, 01 Apr 2020 21:23:32 +0300