简体   繁体   English

SQLite,表,联接

[英]SQLite, Tables, Joining

Android, implementing SQLite Android,实现SQLite

These are the tables i have: 这些是我有的表:

http://postimg.org/image/jafsx39h7/ http://postimg.org/image/jafsx39h7/

I have the code: 我有代码:

public String getWorkoutNameInfo() {
    // TODO Auto-generated method stub

    String[] columns = new String[] { KEY_DATE_OF_WORKOUT,
            KEY_WORKOUT_NAME, KEY_DATE };           
    Cursor c = ourDatabase.query(TABLE_DATE_WORKOUT, columns, null, null,           
            null, null, null, null);                    
    String workoutName2 = "";               
    int iWorkoutID = c.getColumnIndex(KEY_WORKOUT_NAME);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        workoutName2 = workoutName2 + c.getString(iWorkoutID);  

            }

        return workoutName2;

}

This returns workoutName2 which is '2 1 2' 这将返回值为“ 2 1 2”的executionName2

Now i need to look up the value for 2 in my WorkoutTable and return 'Back' and produce 'Back,Chest,Back' so i can out put that onto my screen instead of 2 1 2. 现在,我需要在WorkoutTable中查找2的值,然后返回“ Back”并生成“ Back,Chest,Back”,这样我就可以将其放到屏幕上而不是2 1 2了。

I understand I'll be using a JOIN statement? 我知道我将使用JOIN语句吗? However i'm having no luck implementing it. 但是我没有实现它的运气。

My Table Coding: 我的表格编码:

// WORKOUT TABLE - COLUMN NAMES
public static final String KEY_WORKOUT = "workout_id";
public static final String STRING_WORKOUT = "workout_name"; 


    // DATE OF WORKOUT TABLE - COLUMN NAMES
public static final String KEY_DATE_OF_WORKOUT = "date_of_workout_id";
public static final String KEY_DATE = "date_of_workout";
public static final String KEY_WORKOUT_NAME = "workout_id";

    //TABLE NAMES
private static final String TABLE_WORKOUT = "WorkoutTable";
private static final String TABLE_DATE_WORKOUT = "DateofWorkout";

Here is my attempt: 这是我的尝试:

public String test(String workoutSelectedNameInfo) {

    // TODO Auto-generated method stub
    String NAMEofWorkout = "";
    open();
    ourDatabase = ourhelper.getReadableDatabase();
    Cursor c = ourDatabase
            .rawQuery(
                    "SELECT * FROM WorkoutTable LEFT JOIN DateofWorkout ON (WorkoutTable.workout_id = DateofWorkout.workout_id)   WHERE workout_id = ?",
                    new String[] { "2" });



    int iDateofWorkoutsWorkoutId = c.getColumnIndex(KEY_WORKOUT_NAME);
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        NAMEofWorkout = NAMEofWorkout + c.getString(iDateofWorkoutsWorkoutId);
    }
    c.close();
    ourDatabase.close();
    System.out.println(NAMEofWorkout);
    return NAMEofWorkout;

}

HOWEVER When it output the 'NameofWorkout' which SHOULD be Chest,Back,Chest i receive nothing at all, absolutely blank. 但是,当它输出“ NameofWorkout”(应该是胸部,背部,胸部)时,我什么也收不到,绝对是空白。

When I enter your data into a SQLite instance on my machine and then execute your query, I get the following error: 当我将数据输入到计算机上的SQLite实例中并执行查询时,出现以下错误:

Error: ambiguous column name: workout_id 错误:列名称不明确:executive_id

In the query, changing WHERE workout_id to WHERE DateofWorkout.workout_id makes the error go away. 在查询中,将WHERE workout_id更改为WHERE DateofWorkout.workout_id可使错误消失。

This problem happens because there are two columns named workout_id in the result, and you must disambiguate your subsequent references. 发生此问题的原因是结果中有两列名为workout_id列,并且您必须消除后续引用的歧义。 Because of this, you probably must also change 因此,您可能还必须更改

c.getColumnIndex(KEY_WORKOUT_NAME)

to

c.getColumnIndex(TABLE_DATE_WORKOUT + "." + KEY_WORKOUT_NAME)

when you make use of the results later. 当您稍后使用结果时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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