0

我是 Android Sqlite 数据库的新手。我在 Android 中使用 SQLite 创建了一个数据库,并且我有一个Student_details使用表的外键的表:Student,我将 id 设置为AUTOINCREMENT. 我试图从表中返回AUTOINCREMENTId的值。Student

但我坚持使用作为外键值return values插入。COLUMN_FR_KEY_USER_ID = "student_id";

如何使用ContentValuesintoinsetDataIntoStudentDetails方法使用另一个表中的外键添加值?

数据库助手.java

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "finalStudent.db";
    private static final int VERSION_NUMBER = 16;
    private static final String TABLE_STUDENT = "Student";
    private static final String TABLE_DETAILS_NAME = "Student_details";

    private static final String COLUMN_STUDENT_ID = "id";
    private static final String COLUMN_DETAILS_ID = "id";
    private static final String COLUMN_FR_KEY_USER_ID = "student_id";
    private static final String COLUMN_STUDENT_NAME = "name";
    private static final String COLUMN_DETAILS_NAME = "name";
    private static final String COLUMN_STUDENT_EMAIL = "email";
    private static final String COLUMN_STUDENT_PASSWORD = "password";

    private static final String CREATE_STUDENT_TABLE = "CREATE TABLE "+TABLE_STUDENT+"( "+COLUMN_STUDENT_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_STUDENT_NAME+" TEXT, "+COLUMN_STUDENT_EMAIL+" TEXT, "+COLUMN_STUDENT_PASSWORD+" TEXT)";
    private static final String CREATE_DETAILS_TABLE = "CREATE TABLE "+TABLE_DETAILS_NAME+"( "+COLUMN_DETAILS_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_DETAILS_NAME+" TEXT, "+COLUMN_FR_KEY_USER_ID+" INTEGER, FOREIGN KEY("+COLUMN_FR_KEY_USER_ID+") REFERENCES "+TABLE_STUDENT+" ("+COLUMN_STUDENT_ID+") ON UPDATE CASCADE ON DELETE CASCADE)";

    private Context context;
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_STUDENT;
    private static final String DROP_DETAILS_TABLE = "DROP TABLE IF EXISTS "+TABLE_DETAILS_NAME;


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION_NUMBER);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            Toast.makeText(context,"Table created successfully and called onCreate method",Toast.LENGTH_SHORT).show();
            db.execSQL(CREATE_STUDENT_TABLE);
            db.execSQL(CREATE_DETAILS_TABLE);
        }catch (Exception e){
            Toast.makeText(context,"Exception : "+e,Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {

            Toast.makeText(context,"Table created successfully and called onCreate method",Toast.LENGTH_SHORT).show();
            db.execSQL(DROP_TABLE);
            db.execSQL(DROP_DETAILS_TABLE);
            onCreate(db);
        }catch (Exception e){
            Toast.makeText(context,"Exception : "+e,Toast.LENGTH_SHORT).show();
        }

    }

    public long insertDataIntoStudent( String name, String email, String password){
        // to write or read data in database , we have to call getWritableDatabase
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        // we have to call contentValues class to store data in the data

        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_STUDENT_NAME,name);
        contentValues.put(COLUMN_STUDENT_EMAIL,email);
        contentValues.put(COLUMN_STUDENT_PASSWORD,password);

        // Insert the new row, returning the primary key value of the new row
        long newRowId = sqLiteDatabase.insert(TABLE_STUDENT,null,contentValues);
        return newRowId;
    }


    public void insetDataIntoStudentDetails(String name){

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
       // TODO: insert the foreign key's value 
        contentValues.put(name,name);
    }


    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //enable foreign key constraints like ON UPDATE CASCADE, ON DELETE CASCADE
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private final AppCompatActivity activity = MainActivity.this;
    DatabaseHelper databaseHelper;

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

        databaseHelper = new DatabaseHelper(activity);
        databaseHelper.insertDataIntoStudent("Bappy","abcd@gmail.com","145456");
    }
    }
4

1 回答 1

0

我将回答一般的数据库,而不是专门针对 Sqlite 或它在 Android 中的使用。

所以你有学生的名字(不是主键),但你需要为这个学生保存一些额外的数据到另一个表。步骤是:

  1. 执行SELECT id FROM students WHERE name = ?以查找相关标识符。
  2. INSERT INTO student_details使用检索id作为键为您的辅助表执行。

额外提示:

  1. 名称不是一个好的唯一标识符。最好有一个更多的标准——比如一些组名,或者组号,甚至是出生日期。
  2. 当您的数据库足够大时,您可能会受益index于您的搜索条件来优化第一个查询的执行。但通常情况下,客户端(应用内)数据库并非如此。
  3. 在这里,我们假设 name 不是可以由其他人(或某物)在搜索id和执行之间更新的数据INSERT。否则,明智的做法是使用事务并“锁定”您的学生,直到您完成更新。
于 2020-02-23T18:32:46.200 回答