欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

step2:使用LitePal操作数据库,基于AndroidStudio。练习

程序员文章站 2022-06-23 09:44:47
  step1:建好项目后先配置LitePal!!!(链接)step2:UI布局step3:建库建表step4:增删查改操作另外:使用adb shell观察数据可和表的情况。UI界面:...

此项目参考郭霖《第一行代码》中使用LitePal操作数据库。

  
step1:建好项目后先配置LitePal!!!(链接)

step2:UI布局、创建和升级数据库、使用LitePal对数据进行增删查改操作

另外:使用adb shell观察数据可和表的情况。
step2:使用LitePal操作数据库,基于AndroidStudio。练习


①UI界面

布局如下:
step2:使用LitePal操作数据库,基于AndroidStudio。练习
布局xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dn.library_management_zxy.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LitePal的BookStore"
        android:textSize="30sp" />

    <Button
        android:text="创建数据库"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:id="@+id/button1"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="24sp" />

    <Button
        android:text="添加数据"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="24sp"
        android:layout_marginTop="20dp" />

    <Button
        android:text="更新数据"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button3"
        android:textSize="24sp"
        android:layout_marginTop="20dp" />

    <Button
        android:text="删除数据"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:layout_marginTop="20dp" />

    <Button
        android:text="查询数据"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:id="@+id/button5"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="24sp"
        android:layout_marginTop="20dp" />

</RelativeLayout>

②创建和升级数据库

创建Book表和Category表;
将两个类添加到映射关系中,修改litepal.xml中文件;(step1中以修改)
在MainActivity中建库。
Book 类

package com.example.dn.library_management_zxy;

import org.litepal.crud.DataSupport;

/**
 * Created by ZXY on 2020/6/16.
 */

public class Book extends DataSupport{
    private int id;
    private String author;
    private double price;
    private int pages;
    private String name;
    private String press;

    public String getPress() {
        return press;
    }
    public void setPress(String press) {
        this.press = press;
    }

    public int getId(){
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }

    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;
    }
}

Category 类

package com.example.dn.library_management_zxy;

/**
 * Created by ZXY on 2020/6/16.
 */

public class Category {

    private  int id;
    private String categoryName;
    private int categoryCode;

    public void setId(int id) {
        this.id = id;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public void setCategoryCode(int categoryCode) {
        this.categoryCode = categoryCode;
    }
}

创建数据库

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.button1);
        createDatabase.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                LitePal.getDatabase();
            }
        });
}



③使用LitePal对数据进行增删查改操作

MainActivity类

package com.example.dn.library_management_zxy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.List;

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.button1);
        createDatabase.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                LitePal.getDatabase();
            }
        });



        //添加数据!!!!!!!!!!!!!!!!!!!
        Button addData=(Button) findViewById(R.id.button2);
        addData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Book book=new Book();
                book.setName("第一行代码Android");
                book.setAuthor("郭霖");
                book.setPages(570);
                book.setPrice(79.00);
                book.setPress("人民邮电出版社");
                book.save();
            }
        });


        //更新数据!!!!!!!!!!!!!!!!!!!
        Button updateData=(Button) findViewById(R.id.button3);
        updateData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                //更新价格—只能对已存储的数据进行更新
                Book book=new Book();
                book.setName("昆虫记");
                book.setAuthor("达尔文");
                book.setPages(314);
                book.setPrice(28.80);
                book.setPress("内蒙古人民出版社");
                book.save();
                book.setPrice(20.00);
                book.save();

                /*Book book2=new Book();
                book2.setName("Android开发与应用");
                book2.setAuthor("张荣");
                book2.setPages(275);
                book2.setPrice(45.00);
                book2.setPress("未知");
                book2.save();
                //更新价格和出版社
                Book book=new Book();
                book.setPrice(40.00);
                book.setPress("人民邮电出版社");
                book.updateAll("name=? and author=?","Android开发与应用","张荣");  //类似于where*/



            }
        });




        //删除数据!!!!!!!!!!!!!!!!!!!
        Button deleteData=(Button) findViewById(R.id.button4);
        deleteData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                DataSupport.deleteAll(Book.class,"price < ?","50");
            }
        });


        //查询数据!!!!!!!!!!!!!!!!!!!
        Button queryData=(Button) findViewById(R.id.button5);
        queryData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                List<Book> books= DataSupport.findAll(Book.class);
                //查询所有book,并打印信息
                for(Book book:books){
                    Log.d("MainActivity","book name is"+book.getName());
                    Log.d("MainActivity","book author is"+book.getAuthor());
                    Log.d("MainActivity","book pages is"+book.getPages());
                    Log.d("MainActivity","book price is"+book.getPrice());
                    Log.d("MainActivity","book press is"+book.getPress());
                }
            }
        });


    }


}

本文地址:https://blog.csdn.net/weixin_43660497/article/details/106821569