首页 > 解决方案 > 在 Android Studio 中将项目添加到第二个表 SQLite 什么都不做

问题描述

我认为我的问题很简单,我看不到解决方案,但是,当我将数据添加到我的第二个表时,它什么也不做,只是显示 Toast 它不起作用。

这是我的 DatabaseHelper.java:

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2 + " TEXT);";

    String createTable2 = "CREATE TABLE " + TABLE_NAME2 + " ( " + COL3 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL4 + " TEXT,"
            + COL5 + " INTEGER," + COL1 + " INTEGER PRIMARY KEY," + COL6 + " TEXT, " + " FOREIGN KEY (" + COL1 + ") REFERENCES "
            + TABLE_NAME + "(" + COL1 + "));";

    db.execSQL(createTable);
    db.execSQL(createTable2);
}

public boolean addData2(String newEntry1, String newEntry2, String horarios, Integer id_ppl) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL4, newEntry1);
    contentValues.put(COL5, newEntry2);
    contentValues.put(COL1, id_ppl);
    contentValues.put(COL6, horarios);


    long result = db.insert(TABLE_NAME2, null, contentValues);

    if (result == -1) {
        return false;
    } else {
        return true;
    }

这是我的 mainactivity.java

buttonNewPill.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newEntry1 = editText2.getText().toString();
            String newEntry2 = editText3.getText().toString();
            String textSp1 = spinner2.getSelectedItem().toString();
            String textSp2 = spinner3.getSelectedItem().toString();

            String horario = textSp1 + ":" + textSp2;

            Integer id_ppl = 1;

            Toast.makeText(getApplicationContext(), "horario is "+ horario, Toast.LENGTH_LONG).show();

        /*

            Toast.makeText(getApplicationContext(), "text is "+ textSp, Toast.LENGTH_LONG).show();
        */

            if (editText2.length() > 0 && editText3.length() > 0){

                AddData2(newEntry1, newEntry2, horario, id_ppl);

                editText2.setText("");
                editText3.setText("");

            } else {
                toastMessage("Isn't your field empty?");
            }

        }
    });

private void AddData2(String newEntry1, String newEntry2, String horario, Integer id_ppl) {

    boolean insertData = mDatabaseHelper.addData2(newEntry1, newEntry2, horario, id_ppl);

    if (insertData){
        toastMessage("Data Succesfully entered");
    } else {
        toastMessage("Oops! Something went wrong");
    }

}

所有的类都很好,构造函数是制作的,它没有任何语法错误(我认为),因为它让我可以在我的手机中执行我的应用程序。但我遇到了上述问题。

非常感谢您在这件事上的时间和帮助。

标签: sqliteandroid-studio

解决方案


我相信您的问题是您正在尝试为第二个表定义多个主索引。

那就是你有: -

  • 用于 COL3INTEGER PRIMARY KEY AUTOINCREMENT
  • 对于 COL1INTEGER PRIMARY KEY

你只能有一个 PRIMARY INDEX(你可以有多个非主索引)。

如果您查看日志,您会得到类似于(当您第一次运行时)的内容:-

05-11 01:38:10.133 1215-1215/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{soanswers.soanswers/soanswers.soanswers.MainActivity}: android.database.sqlite.SQLiteException: table "tbl002" has more than one primary key (code 1): , while compiling: CREATE TABLE tbl002 ( column3 INTEGER PRIMARY KEY AUTOINCREMENT, column4 TEXT,column5 INTEGER,column1 INTEGER PRIMARY KEY,column6 TEXT,  FOREIGN KEY (column1) REFERENCES tbl001(column1));

所以猜测你会想要: -

String createTable2 = "CREATE TABLE " + TABLE_NAME2 + " ( " + 
    COL3 + " INTEGER PRIMARY KEY, " + 
    COL4 + " TEXT,"
            + 
    COL5 + " INTEGER," + 
    COL1 + " INTEGER," + 
    COL6 + " TEXT, " + 
    " FOREIGN KEY (" + COL1 + ") REFERENCES " + TABLE_NAME + "(" + COL1 + "));";
  • 列 1 作为主索引被删除,尽管您可能希望在 COL1 上添加一个附加索引,因为它引用了另一个表。
  • 你很可能真的不想要自动增量,根据

    • AUTOINCREMENT 关键字强加了额外的 CPU、内存、磁盘空间和磁盘 I/O 开销,如果不是严格需要,应避免使用。通常不需要它。SQLite 自动增量

请注意,在进行更改后,您需要删除应用程序的数据或卸载应用程序(只要您能承受丢失任何现有数据),以应用更改 - 还有其他方法,但这是最简单的。

添加索引(可选):-

以下将根据 table2 col1 添加一个附加索引:-

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL2 + " TEXT);";

    /*
    String createTable2 = "CREATE TABLE " + TABLE_NAME2 + " ( " + COL3 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL4 + " TEXT,"
            + COL5 + " INTEGER," + COL1 + " INTEGER PRIMARY KEY," + COL6 + " TEXT, " + " FOREIGN KEY (" + COL1 + ") REFERENCES "
            + TABLE_NAME + "(" + COL1 + "));";
    */

    String createTable2 = "CREATE TABLE " + TABLE_NAME2 + " ( " +
            COL3 + " INTEGER PRIMARY KEY, " +
            COL4 + " TEXT,"
            +
            COL5 + " INTEGER," +
            COL1 + " INTEGER," +
            COL6 + " TEXT, " +
            " FOREIGN KEY (" + COL1 + ") REFERENCES " + TABLE_NAME + "(" + COL1 + "));";

    String crtTable2Col1Index = "CREATE INDEX IF NOT EXISTS col1index ON " +
            TABLE_NAME2 + "(" + COL1 + ")"; //<<<< ADDED


    db.execSQL(createTable);
    db.execSQL(createTable2);
    db.execSQL(crtTable2Col1Index); //<<<< ADDED
}
  • col1index将是索引的名称

额外的

请注意,当您打算使用外键时,您需要启用外键(如果您没有)。您可以通过覆盖onConfigure方法来做到这一点,例如:-

@Override
public void onConfigure (SQLiteDatabase db) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        db.setForeignKeyConstraintsEnabled(true);
    } else {
        db.execSQL("pragma foreign_keys = ON");
    }
}

推荐阅读