我一直被困在外键问题上,因为通常我可以检索数据,但是不能用外键检索数据。
数据库助手
public static final String TBL_NAME="users";
public static final String COL_ID="id";
public static final String COL_NAME="name";
public static final String COL_PASS="password";
public static final String COL_EMAIL="email";
public static final String COL_PIC="picture";
public static final String TABLE_NAME = "tasks";
public static final String C_ID = "_id";
public static final String TITLE = "title";
public static final String TYPE = "type";
public static final String DETAIL = "description";
public static final String TIME = "time";
public static final String DATE = "date";
public static final String KEY_TODO_USER_ID_FK = "userId";
public static final
String TBL_QUERY="create table "+TBL_NAME+"("+COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_NAME+" text,"+COL_PASS+" text,"+COL_EMAIL+" text,"+COL_PIC+" blob)";
public static final String Task_Query = "create table if not exists " + TABLE_NAME + " ( "
+ C_ID + " integer primary key autoincrement, "
+ TITLE + " text, "
+ DETAIL + " text, "
+ TYPE + " text, "
+ TIME + " text, "
+ KEY_TODO_USER_ID_FK + " INTEGER REFERENCES " + TBL_NAME + ","
+ DATE + " text)";
ViewActivity
Cursor cursor = sb.rawQuery("select * from " + Constants.TABLE_NAME + " where " + Constants.C_ID + "=" + id, null);
TextView title = (TextView) findViewById(R.id.title);
TextView detail = (TextView) findViewById(R.id.detail);
TextView notetype = (TextView) findViewById(R.id.note_type_ans);
TextView time = (TextView) findViewById(R.id.alertvalue);
TextView date = (TextView) findViewById(R.id.datevalue);
if (cursor != null) {
if (cursor.moveToFirst()) {
title.setText(cursor.getString(cursor.getColumnIndex(Constants.TITLE)));
detail.setText(cursor.getString(cursor.getColumnIndex(Constants.DETAIL)));
notetype.setText(cursor.getString(cursor.getColumnIndex(Constants.TYPE)));
time.setText(cursor.getString(cursor.getColumnIndex(Constants.TIME)));
date.setText(cursor.getString(cursor.getColumnIndex(Constants.DATE)));
}
cursor.close();
}
因此,请帮助我如何仅查看用户任务而不查看所有用户任务。
最佳答案
好吧,仅检索特定用户的数据,您需要在查询中包括这些数据。所以这:
Cursor cursor = sb.rawQuery("select * from " + Constants.TABLE_NAME + " where " + Constants.C_ID + "=" + id, null);
应该看起来像这样:
Cursor cursor = sb.rawQuery("select * from " + Constants.TABLE_NAME + " where " + Constants.C_ID + "=" + id + " and " + Constants.KEY_TODO_USER_ID_FK + "=" + userid, null);
但我不知道您从何处获得“ userid”变量的值。