Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio-Room持久性库-确保用户输入的主键在插入数据库之前是唯一的_Android_Database_Android Studio_Primary Key_Android Database - Fatal编程技术网

Android Studio-Room持久性库-确保用户输入的主键在插入数据库之前是唯一的

Android Studio-Room持久性库-确保用户输入的主键在插入数据库之前是唯一的,android,database,android-studio,primary-key,android-database,Android,Database,Android Studio,Primary Key,Android Database,我正在学习如何使用androidstudio中的Room持久性库,它基本上是SQLite之上的一个抽象层,可以简化数据库 在我的示例应用程序中,我允许用户输入非常基本的学生信息: 我为数据库创建了以下三个必要组件: 1。实体(学生) 2。数据访问对象API package com.aleks.firstdatabaseproject; import android.arch.persistence.room.Dao; import android.arch.persistence.room.

我正在学习如何使用androidstudio中的Room持久性库,它基本上是SQLite之上的一个抽象层,可以简化数据库

在我的示例应用程序中,我允许用户输入非常基本的学生信息:

我为数据库创建了以下三个必要组件:

1。实体(学生)

2。数据访问对象API

package com.aleks.firstdatabaseproject;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;

import com.aleks.firstdatabaseproject.Student;

import java.util.List;

/* A Data Access Object (DAO) provides an abstracted interface that allows
 * a user program to request information from the database. The DAO provides
 * methods for insertion, deletion, and querying in the database.
 */

@Dao
public interface StudentDAO {

    @Insert
    void addStudent(Student student);

    @Delete
    void removeStudent(Student student);

    @Query("SELECT * FROM Student")
    List<Student> getAllStudents();

    @Query("SELECT * FROM Student WHERE id IS :studentID")
    Student findStudentByID(int studentID);

    @Query("SELECT * FROM Student WHERE first_name LIKE :first AND last_name LIKE :last")
    List<Student> findStudentByName(String first, String last);
}
以下是主要活动供参考:

主要活动

package com.aleks.firstdatabaseproject;

import android.arch.persistence.room.Room;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static StudentDatabase mDatabase;
    private StudentDAO mStudentDAO;
    private TextView mFirstName;
    private TextView mLastName;
    private TextView mId;
    private TextView mEmail;
    private TextView mNotes;

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

        mDatabase = Room.databaseBuilder(getApplicationContext(),
                StudentDatabase.class, "database-name").build();
        mStudentDAO = mDatabase.studentDAO();

        mFirstName = (TextView) findViewById(R.id.editText_firstName);
        mLastName = (TextView) findViewById(R.id.editText_lastName);
        mId = (TextView) findViewById(R.id.editText_id);
        mEmail = (TextView) findViewById(R.id.editText_email);
        mNotes = (TextView) findViewById(R.id.editText_notes);
    }

    /* Uses the information in the app's main fields to build and
     * return a new Student object for use by other methods.
     */
    public Student buildStudentFromFields() {
        String firstName = mFirstName.getText().toString();
        String lastName = mLastName.getText().toString();
        String email = mEmail.getText().toString();
        String notes = mNotes.getText().toString();
        return new Student(firstName, lastName, email, notes);
    }

    /* Called when the user clicks the "ADD" button. Inserts
     * the given Student into the database.
     */
    public void addStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mStudentDAO.addStudent(buildStudentFromFields());
            }
        }).start();
    }

    /* Called when the user clicks the "REMOVE" button. Removes
     * the given Student from the database.
     */
    public void removeStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mStudentDAO.removeStudent(buildStudentFromFields());
            }
        }).start();
    }

    /* Called when the user clicks the "FIND" button. Returns
     * information about the given Student from the database.
     */
    public void findStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Student student = mStudentDAO.findStudentByID(Integer.parseInt(mId.getText().toString()));
            }
        }).start();
    }
}

我想做什么

您会注意到,在entity类中,我包含了注释
@PrimaryKey(autogenerate=true)
。这将
id
字段指定为
Student
表的主键,
autogenerate
选项强制数据库生成自己的主键,而不是用户有意识地记住传递唯一键。但是用户很容易出错,例如,他们可能会意外地按两次
ADD
按钮

问题是:我想允许用户输入自己的自定义学生ID,就像我在屏幕截图中显示的那样,但如果用户尝试(非法)输入同一个学生ID两次,则会出现问题。在这种情况下,应用程序会崩溃

一个可能的解决方案是保留用户迄今为止输入的所有ID的集合/哈希表,并确保在尝试
将学生添加到数据库之前,他们输入的ID是唯一的。但这只会占用更多的内存(对于这样一个小应用程序来说不是很多,但存储额外的“垃圾”信息似乎不合理)


我有没有办法解释重复的主键,以防止当用户意外输入同一个学生ID两次时应用程序崩溃?

您可以尝试在插入方法中定义OnConflictStrategy来处理错误,请选中此项

package com.aleks.firstdatabaseproject;

import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;

import com.aleks.firstdatabaseproject.Student;
import com.aleks.firstdatabaseproject.StudentDAO;

@Database(entities = {Student.class}, version = 1)
public abstract class StudentDatabase extends RoomDatabase {

    /* Used by apps to get the data access object from the database */
    public abstract StudentDAO studentDAO();
}
package com.aleks.firstdatabaseproject;

import android.arch.persistence.room.Room;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static StudentDatabase mDatabase;
    private StudentDAO mStudentDAO;
    private TextView mFirstName;
    private TextView mLastName;
    private TextView mId;
    private TextView mEmail;
    private TextView mNotes;

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

        mDatabase = Room.databaseBuilder(getApplicationContext(),
                StudentDatabase.class, "database-name").build();
        mStudentDAO = mDatabase.studentDAO();

        mFirstName = (TextView) findViewById(R.id.editText_firstName);
        mLastName = (TextView) findViewById(R.id.editText_lastName);
        mId = (TextView) findViewById(R.id.editText_id);
        mEmail = (TextView) findViewById(R.id.editText_email);
        mNotes = (TextView) findViewById(R.id.editText_notes);
    }

    /* Uses the information in the app's main fields to build and
     * return a new Student object for use by other methods.
     */
    public Student buildStudentFromFields() {
        String firstName = mFirstName.getText().toString();
        String lastName = mLastName.getText().toString();
        String email = mEmail.getText().toString();
        String notes = mNotes.getText().toString();
        return new Student(firstName, lastName, email, notes);
    }

    /* Called when the user clicks the "ADD" button. Inserts
     * the given Student into the database.
     */
    public void addStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mStudentDAO.addStudent(buildStudentFromFields());
            }
        }).start();
    }

    /* Called when the user clicks the "REMOVE" button. Removes
     * the given Student from the database.
     */
    public void removeStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                mStudentDAO.removeStudent(buildStudentFromFields());
            }
        }).start();
    }

    /* Called when the user clicks the "FIND" button. Returns
     * information about the given Student from the database.
     */
    public void findStudent(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Student student = mStudentDAO.findStudentByID(Integer.parseInt(mId.getText().toString()));
            }
        }).start();
    }
}