简体   繁体   中英

Issue with database producing incorrect results for foreign key

I'm currently working on implementing a way to add, edit, update and delete information from my database through an interface within my Android Studio project, I currently have it adding correctly when it involves a table with no foreign key.

The issue I am having is when I try to add data to my database with a table that has a foreign key.

The method I would achieve this would be through imputing the relevant information as well as the ID of the foreign key. However, when doing this it will randomly change whatever ID I insert into ID : 2131165349. I am unsure where this ID comes from and this ID does not exist in the table where the foreign key originally is.

I believe my issue is in the code I have used to create the tables with reference to how I have created tables which have primary keys.

Below is my code for the onCreate method where this belongs.

public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String sqlBook = "CREATE TABLE book(id INTEGER PRIMARY KEY AUTOINCREMENT, bookName VARCHAR, bookAuthor VARCHAR);";
    String sqlChapter = "CREATE TABLE chapter(id INTEGER PRIMARY KEY AUTOINCREMENT, chapterName VARCHAR, book_id INTEGER, FOREIGN KEY(book_id) REFERENCES book(id))";
    String sqlSection ="CREATE TABLE section(id INTEGER PRIMARY KEY AUTOINCREMENT, sectionName VARCHAR, sectionInfo VARCHAR, book_id INTEGER, chapter_id INTEGER, FOREIGN KEY(book_id) REFERENCES book(id), FOREIGN KEY (chapter_id) REFERENCES chapter(id))";

    sqLiteDatabase.execSQL(sqlBook);
    sqLiteDatabase.execSQL(sqlChapter);
    sqLiteDatabase.execSQL(sqlSection);

AddChapterActivity

public class AddChapterActivity extends AppCompatActivity {

DatabaseHelper db;
Button btnAddChapter, btnViewChapter, btnUpdateChapter, btnDeleteChapter;
EditText textEditChapterName, textEditBookID, textEditChapterID;



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

    db = new DatabaseHelper(this);
    btnAddChapter = findViewById(R.id.btnAddChapter);
    btnViewChapter = findViewById(R.id.btnViewChapter);
    btnUpdateChapter = findViewById(R.id.btnUpdateChapter);
    btnDeleteChapter = findViewById(R.id.btnDeleteChapter);
    textEditBookID = findViewById(R.id.textEditBookID);
    textEditChapterName = findViewById(R.id.textEditChapterName);
    textEditChapterID = findViewById(R.id.textEditChapterID);
    addChapter();
}
  public void addChapter(){
    btnAddChapter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String chapterName = textEditChapterName.getText().toString();
            int book_id =  textEditBookID.getId();

            boolean insertChapter = db.addChapter(chapterName, book_id);

            if(insertChapter == true) {
                Toast.makeText(AddChapterActivity.this, "Chapter added successfully!", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(AddChapterActivity.this, "Something went wrong.", Toast.LENGTH_LONG).show();
            }
        }
    });
        }

DatabaseHelper

    public boolean addChapter(String chapterName, Integer book_id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("chapterName", chapterName);
    contentValues.put("book_id", book_id);
    long result = db.insert("chapter", null, contentValues);
        if(result == -1) {
            return false;
        }else{
            return true;
        }

In the onClick() method of the listener in AddChapterActivity class, change this line:

int book_id =  textEditBookID.getId();

with

int book_id =  Integer.parseInt(textEditBookID.getText().toString());

You were getting the EditText 's id and not the text with your code.
I guess (by its name) you have in textEditBookID the id of the book, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM