Android Sqlite内部联接不起作用

时间:2013-09-09 16:57:35

标签: android sqlite

我正在尝试在Sqlite

上的两个表之间进行内部连接

这是我的内部联接代码:

public PicoEvent getEvent(String eventCode)
{
    //Get the readable database instance
    SQLiteDatabase db = getReadableDatabase();      
    //create string for user table inner join
    String userTable1 = UsersTable.TABLE_NAME + "1";
    String userTable2 = UsersTable.TABLE_NAME + "2";
    //create query for getting the event's details
    String sql1 = "Select * From " + EventsTable.TABLE_NAME
    //set the user tables once as users1 and once as users2 in the inner join process
    + " Inner Join " + UsersTable.TABLE_NAME + " As " + userTable1 + " On "
    + userTable1 + "." + UsersTable.USER_ID + " = " + EventsTable.TABLE_NAME + "." + EventsTable.SIGNED_IN_USER_ID
    + " Inner Join " + UsersTable.TABLE_NAME + " As " + userTable2 + " On "
    + userTable2 + "." + UsersTable.USER_ID + " = " + EventsTable.TABLE_NAME + "." + EventsTable.OWNER_USER_ID 
    + " Where " + EventsTable.TABLE_NAME + "." + EventsTable.EVENT_CODE + " = ?";               

    //selection arguments
    String[] selectionArgs = new String[] {eventCode}; 

    //Get the cursor for event's details query
    Cursor cursor = db.rawQuery(sql1, selectionArgs);
    Log.d("query", sql1.replace("?", eventCode));
    Log.d("cursor", String.valueOf(cursor.getCount()));
}

它创建了这个查询:

Select * From Events 
Inner Join Users As Users1 On Users1.User_Id = Events.Signed_In_User_Id 
Inner Join Users As Users2 On Users2.User_Id = Events.Owner_User_Id 
Where Events.Event_Code = 10231009

光标始终返回0结果...

当我放弃内连接时 - 我得到了带有事件的光标..

任何想法?

更新 - 添加了创建语句

/**
 * Get the query to create the Users table
 * @return the query for the creation of the table ready to be executed
 */
private String getCreateUsersTableQuery()
{
    //Create the create query string
    String createQuery = "Create Table If Not Exists " + UsersTable.TABLE_NAME + "("
            + UsersTable.USER_ID + " Varchar(50) Primary Key Not Null,"
            + UsersTable.NAME + " Varchar(50) Not Null,"
            + UsersTable.FIRST_NAME + " Varchar(50),"
            + UsersTable.PROFILE_PICTURE + " Blob"
            + ")";
    return createQuery; 
}

给我创建查询:

Create Table If Not Exists Users(
     User_Id Varchar(50) Primary Key Not Null,
     Name Varchar(50) Not Null,
     First_Name Varchar(50),
     Profile_Picture Blob)


/**
 * Get the query to create the Events table
 * @return the query for the creation of the table ready to be executed
 */
private String getCreateEventsTableQuery()
{
    //Create the create query string
    String createQuery = "Create Table If Not Exists " + EventsTable.TABLE_NAME + "("
            + EventsTable.EVENT_CODE + " Varchar(50)Not Null,"
            + EventsTable.SIGNED_IN_USER_ID + " Varchar(50)Not Null,"
            + EventsTable.NAME + " Varchar(50) Not Null,"
            + EventsTable.PLACE + " Varchar(100),"
            + EventsTable.START_TIME + " Integer Not Null,"
            + EventsTable.END_TIME + " Integer Not Null,"    
            + EventsTable.OWNER_USER_ID + " Varchar(50) Not Null,"
            + EventsTable.IS_SHARING_PICTURES + " Integer Default 1 Not Null,"
            + EventsTable.IS_ASK_BEFORE_SHARING + " Integer  Default 1 Not Null,"
            + "Primary Key(" + EventsTable.EVENT_CODE + "," + EventsTable.SIGNED_IN_USER_ID + "),"
            + "FOREIGN KEY(" + EventsTable.SIGNED_IN_USER_ID + ") REFERENCES " + UsersTable.TABLE_NAME + "(" + UsersTable.USER_ID + "),"
            + "FOREIGN KEY(" + EventsTable.OWNER_USER_ID + ") REFERENCES " + UsersTable.TABLE_NAME + "(" + UsersTable.USER_ID + ")"
            + ");";
    return createQuery;
}

给我查询:

 Create Table If Not Exists Events(
     Event_Code Varchar(50)Not Null,
     Signed_In_User_Id Varchar(50)Not Null,
     Name Varchar(50) Not Null,Place Varchar(100),
     Start_Time Integer Not Null,
     End_Time Integer Not Null,
     Owner_User_Id Varchar(50) Not Null,
     Is_Sharing_Pictures Integer Default 1 Not Null,
     Is_Ask_Before_Share Integer  Default 1 Not Null,
     Primary Key(Event_Code,Signed_In_User_Id),
     FOREIGN KEY(Signed_In_User_Id) REFERENCES Users(User_Id),
     FOREIGN KEY(Owner_User_Id) REFERENCES Users(User_Id));

我正在尝试在事件Owner_User_Id和Signed_In_User_Id之间根据他们的Id

与Users表中的匹配用户进行内部联接

1 个答案:

答案 0 :(得分:1)

我希望你仍然可以使用答案。 事实证明,使用AS无效。 我自己使用了一些内连接,只有在完全忽略AS时它们才能工作。

"Inner Join " + UsersTable.TABLE_NAME + " " + userTable1...

所以这应该成功,至少对我而言:

Select * From Events 
Inner Join Users Users1 On Users1.User_Id = Events.Signed_In_User_Id 
Inner Join Users Users2 On Users2.User_Id = Events.Owner_User_Id 
Where Events.Event_Code = 10231009