SQLite数据库错误(两个表之间桥接中的故障)

时间:2013-12-09 07:27:57

标签: android database sqlite

我有两个表作为USER_TABLE,其中我将所有用户详细信息设置为用户名,名字等,同样我还有另一个名为RESUME_TABLE的表,其中我有目标,度,大学等 两者共有的列是KEY_USERID(动态创建),我使用此id来匹配这两个表。

问题:

我注册了一个用户,他的详细信息存储在USER_TABLE中,并说他的KEY_USERID自动生成(主键)为1,然后我在RESUME_TABLE使用相同的{KEY_USERID 1为他创建了一个简历{1}}。在从KEY_USERID插入和检索简历内容时,我将从第一个表中获取的RESUME_TABLE作为参数传递。

说,我再次为另一个用户执行此操作,从而生成KEY_USERID 2,但我只填写了USER_TABLE RESUME_TABLE RESUME_TABLE IS EMPTY

当我再次重复填充这两个表时,RESUME_TABLE无法获取数据,因为第二个用户没有填写KEY_USERID中的详细信息,并且在检索时会出现故障第三个用户的public String get_user_id(String name) { String[] column = new String[]{KEY_USERID}; String where = KEY_USERNAME + "=?"; Cursor c = mDB.query(DATABASE_TABLE, column, where,new String[]{""+ name + ""}, null, null,null); String user_id = ""; int iuser_id = c.getColumnIndex(KEY_USERID); if (c != null) c.moveToFirst(); user_id = user_id + c.getString(iuser_id) + "\t"; c.close(); return user_id; }

我如何纠正这个?


代码:

DBAdapter.java:

if(get_from_create!=null)
        {
            if(get_from_create.hasExtra("UserId_Create"))
            {
                String user_id_create = get_from_create.getExtras().getString("UserId_Create"); // This is where i get the user id from the first table which is created dynamically.
                dbAdapter.open();
                Log.i("In_View Resume_from_create",user_id_create);
                objective.setText(dbAdapter.getObjective(user_id_create));
                degree.setText(dbAdapter.getDegree(user_id_create));
                passed_out.setText(dbAdapter.getPassedOut(user_id_create));
                university.setText(dbAdapter.getUniversity(user_id_create));
                field.setText(dbAdapter.getField(user_id_create));
                years_of_experience.setText(dbAdapter.getyears_of_experience(user_id_create));
                areas_of_interest.setText(dbAdapter.getareas_of_interest(user_id_create));
                dbAdapter.close();
            }

ViewResume.java

KEY_USERID

注意:

USER_TABLE仅针对RESUME_TABLE自动递增,而不会针对{{1}}自动递增。

1 个答案:

答案 0 :(得分:1)

在不同的表上不能有两个引用相同元素的自动键。

您应该只在USER_TABLE中拥有自动主键。 KEY_USERID不能是自动的,必须与USER_TABLE.KEY_USERID匹配 - 实际上是foreign key

此外,要了解代码中ID错误的原因,您必须发布get_from_create函数。