2024年3月11日第十一篇
1 | 2 | 3 | 4 | 5 | |
代码量 | 200 | ||||
博客 | 1 | ||||
花费时间 | 2 | ||||
知识点 | 安卓本地数据库的增删改查及登录的实现 |
今天主要学习了安卓本地数据库SQLite的Room增删改查操作以及登录页面的实现,学习了一些安卓java开发的中级控件。
下面是我整理的一些代码
# 1.创建entity然后创建实体类 ``` package com.example.anew.entity; import androidx.room.Entity; import androidx.room.PrimaryKey; @Entity public class Book { @PrimaryKey(autoGenerate = true) private int id; private String name; // 书籍名称 private String author; // 作者 private String press; // 出版社 private String price; // 价格 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } @Override public String toString() { return "BookInfo{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", press='" + press + '\'' + ", price=" + price + '}'; } } ``` # 2.然后创建database包,创建对应的database类 ``` package com.example.anew.database; import androidx.room.Database; import androidx.room.RoomDatabase; import com.example.anew.Dao.BookDao; import com.example.anew.entity.Book; @Database(entities = {Book.class}, version = 1, exportSchema = true) public abstract class BookDatabase extends RoomDatabase { // 获取该数据库中某张表的持久化对象 public abstract BookDao bookDao(); } ``` # 3.创建Dao类,创建对应的Dao包 ``` package com.example.anew.Dao; import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; import androidx.room.Update; import com.example.anew.entity.Book; import java.util.List; @Dao public interface BookDao { @Insert void insert(Book book); @Delete void delete(Book book); // 删除所有书籍信息 @Query("DELETE FROM Book") void deleteAll(); @Update() int update(Book book); // 加载所有书籍信息 @Query("SELECT * FROM Book") List<Book> queryAll(); // 根据名字加载书籍 @Query("SELECT * FROM Book WHERE name = :name ORDER BY id DESC limit 1") Book queryByName(String name); } ``` # 4.最后创建对应的activity ``` package com.example.anew; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import com.example.anew.entity.Book; import com.example.anew.util.Toastutil; import com.example.anew.Dao.BookDao; import java.util.List; public class RoomWriteActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_name; private EditText et_author; private EditText et_press; private EditText et_price; private BookDao bookDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_room); et_name = findViewById(R.id.et_name); et_author = findViewById(R.id.et_author); et_press = findViewById(R.id.et_press); et_price = findViewById(R.id.et_price); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_query).setOnClickListener(this); findViewById(R.id.btn_update).setOnClickListener(this); findViewById(R.id.btn_delete).setOnClickListener(this); bookDao = MyApplication.getInstance().getBookDB().bookDao(); } @Override public void onClick(View v) { String name = et_name.getText().toString(); String author = et_author.getText().toString(); String press = et_press.getText().toString(); String price = et_price.getText().toString(); if(v.getId() == R.id.btn_save){ Book b1 = new Book(); b1.setName(name); b1.setAuthor(author); b1.setPress(press); b1.setPrice(price); bookDao.insert(b1); Toastutil.showMsg(this, "保存成功"); } if(v.getId() == R.id.btn_query){ List<Book> list = bookDao.queryAll(); for (Book b : list) { Log.d("ning", b.toString()); } } if(v.getId() == R.id.btn_update){ Book b2=bookDao.queryByName(name); Book b1=new Book(); b1.setId(b2.getId()); b1.setName(name); b1.setAuthor(author); b1.setPress(press); b1.setPrice(price); bookDao.update(b1); Toastutil.showMsg(this, "更新成功"); } if(v.getId() == R.id.btn_delete){ Book b1 = bookDao.queryByName(name); bookDao.delete(b1); Toastutil.showMsg(this, "删除成功"); } }} ``` # 在与之对应的xml文件下写如下代码: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text=" 书名:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入书籍名称" android:inputType="text" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_author" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text=" 作者:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_author" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入作者姓名" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_press" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="出版社:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_press" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入出版社名称" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text=" 价格:" android:textColor="@color/black" android:textSize="17sp" /> <EditText android:id="@+id/et_price" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginTop="3dp" android:layout_marginBottom="3dp" android:layout_weight="1" android:background="@drawable/editext_selector" android:hint="请输入书籍价格" android:inputType="numberDecimal" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改" android:textColor="@color/black" android:textSize="17sp" /> <Button android:id="@+id/btn_query" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询" android:textColor="@color/black" android:textSize="17sp" /> </LinearLayout> ``` # 还有与之对应的myapplication ``` package com.example.anew; import android.app.Application; import android.util.Log; import androidx.room.Room; import com.example.anew.database.BookDatabase; import java.util.HashMap; public class MyApplication extends Application { private static MyApplication mApp; // 声明一个公共的信息映射对象,可当作全局变量使用 public HashMap<String, String> infoMap = new HashMap<>(); // 声明一个书籍数据库对象 private BookDatabase bookDatabase; // 购物车中的商品总数量 public int goodsCount; public static MyApplication getInstance() { return mApp; } //在App启动时调用 @Override public void onCreate() { super.onCreate(); mApp = this; Log.d("ning", "MyApplication onCreate"); // 构建书籍数据库的实例 bookDatabase = Room.databaseBuilder(mApp, BookDatabase.class, "book") // 允许迁移数据库(发生数据库变更时,Room默认删除原数据库再创建新数据库。如此一来原来的记录会丢失,故而要改为迁移方式以便保存原有记录) .addMigrations() // 允许在主线程中操作数据库(Room默认不能在主线程中操作数据库) .allowMainThreadQueries() .build(); } @Override public void onTerminate() { super.onTerminate(); Log.d("ning", "onTerminate"); } // 获取书籍数据库的实例 public BookDatabase getBookDB() { return bookDatabase; } } ```