Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android/java中转换sqlite查询中的值_Java_Android_Sqlite - Fatal编程技术网

在android/java中转换sqlite查询中的值

在android/java中转换sqlite查询中的值,java,android,sqlite,Java,Android,Sqlite,我在查询具有多个表的数据库时遇到问题。有两张桌子 表1: 学生ID(主键) 名字 姓氏 学校ID(整数:表2中列出的3所学校编号为1-3) 表2: 学校ID(主键) 学校名称(字符串) 由于表2中只有3个条目,我想匹配表1中的学校ID值,以输出表2中列出的实际学校名称 public String getData(String sNumber) { SQLiteDatabase db = helper.getWritableDatabase(); String[] colu

我在查询具有多个表的数据库时遇到问题。有两张桌子

表1:
学生ID(主键)
名字
姓氏
学校ID(整数:表2中列出的3所学校编号为1-3)

表2:
学校ID(主键)
学校名称(字符串)

由于表2中只有3个条目,我想匹配表1中的学校ID值,以输出表2中列出的实际学校名称

    public String getData(String sNumber) {
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {DataBaseHelper.STUDENT_ID, DataBaseHelper.STUDENT_FIRST_NAME, DataBaseHelper.STUDENT_LAST_NAME,
                        DataBaseHelper.STUDENT_SCHOOL};
    Cursor cursor=db.query(DataBaseHelper.STUDENT_TABLE, columns, DataBaseHelper.STUDENT_ID+" = '"+sNumber+"'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while(cursor.moveToNext()){
        int index1=cursor.getColumnIndex(DataBaseHelper.STUDENT_ID);
        int index2=cursor.getColumnIndex(DataBaseHelper.STUDENT_FIRST_NAME);
        int index3=cursor.getColumnIndex(DataBaseHelper.STUDENT_LAST_NAME);
        int index4=cursor.getColumnIndex(DataBaseHelper.STUDENT_SCHOOL);
        String sText = cursor.getString(index1);
        String sFirstName = cursor.getString(index2);
        String sLastName = cursor.getString(index3);
        String sSchool = cursor.getString(index4);
        buffer.append(sText + " " + sLastName + " " +sFirstName+ " " +sSchool+ "\n" );
    }
    return buffer.toString();
}
这是我到目前为止的代码部分。。当输入学生ID时。。它将返回一个结果,如“24 Smith John 3”。我希望3读取表2中的相应值

    public String getData(String sNumber) {
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {DataBaseHelper.STUDENT_ID, DataBaseHelper.STUDENT_FIRST_NAME, DataBaseHelper.STUDENT_LAST_NAME,
                        DataBaseHelper.STUDENT_SCHOOL};
    Cursor cursor=db.query(DataBaseHelper.STUDENT_TABLE, columns, DataBaseHelper.STUDENT_ID+" = '"+sNumber+"'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while(cursor.moveToNext()){
        int index1=cursor.getColumnIndex(DataBaseHelper.STUDENT_ID);
        int index2=cursor.getColumnIndex(DataBaseHelper.STUDENT_FIRST_NAME);
        int index3=cursor.getColumnIndex(DataBaseHelper.STUDENT_LAST_NAME);
        int index4=cursor.getColumnIndex(DataBaseHelper.STUDENT_SCHOOL);
        String sText = cursor.getString(index1);
        String sFirstName = cursor.getString(index2);
        String sLastName = cursor.getString(index3);
        String sSchool = cursor.getString(index4);
        buffer.append(sText + " " + sLastName + " " +sFirstName+ " " +sSchool+ "\n" );
    }
    return buffer.toString();
}

使用内部联接

SELECT t1.idStudent, t1.firstName, t1.lastName, t2.name 
FROM Table1 t1
   INNER JOIN Table2 t2
      ON t2.id = t1.idSchool
WHERE t1.idSchool = ID_T2;
警告

table1=学生表的名称

table2=学校表的名称

idSchool=学校主键的名称

您可以使用一些ORM


我喜欢SUGAR,试试它

如果你不想使用ORM,你只需要在查询中加入一个连接即可