Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use LitePal to operate the database in Android

Shulou Source: shulou.com Published: 2022-05-31 23:50:31 09月21日 Update

Most people do not understand the knowledge points of this article "how to use LitePal to operate the database in Android", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use LitePal to manipulate the database in Android" article.

First edit in the app/build.grade file

Dependencies {... conmpile'org.litepal.android:core:1.5'..}

Then create an assets directory and create a litepal.xml

Then add the code to the AndroidManifest.xml

Android:name= "org.litepal.litePalApplication"

To put it simply, the mode of object-relational mapping (ORM) in LitePal is to establish a mapping relationship between object-oriented language and object-oriented database, that is to say, you can use object-oriented thinking to operate the database instead of using the tedious steps of SQLiteOpenHelper.

Create an Abc class

Public class Abc extends DataSupport {private int id; private double price; private int pages; private String name; public int getId () {return id;} public void setId (int id) {this.id = id;} public int getPages () {return pages;} public void setPages (int pages) {this.pages = pages;} public String getName () {return name } public void setName (String name) {this.name = name;}}

Then add it to the litepal.xml

Then let's talk about the database upgrade mechanism of SQLiteOpenHelper. Each time it is executed, it will drop the database and then re-create it. While the drop database is also lost, you only need to modify what is needed to upgrade the database in LitePal, and then add the version number to one, that is,

All you need to add updated data is new an Abc and then call set

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Button createDatabase = (Button) findViewById (R.id.create_database) CreateDatabase.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Abc abc=new Abc (); abc.setname ("litepal");}});}}

To delete data, use the

DataSupport.deleteAll (Abc.class, "name=?", "litepal")

Query data

List abc=DateSupport.findAll (Abc.class); the above is about "how to use LitePal to operate the database in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Data databases content objects that is articles steps knowledge articles upgrades tedious that is to say code value at the same time most thoughts files images. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno macOS Shulou Tech Info Microsoft