SQLite异常:找不到这样的列(id主键)

时间:2019-01-29 21:12:32

标签: java android sqlite

我的数据库中有两个表,我使用内部联接将满足条件的数据打印到LogCat。 我敢肯定,这只是一个愚蠢的错误。

原因:

Caused by: android.database.sqlite.SQLiteException: no such column: position.id (code 1 SQLITE_ERROR): , while compiling: select people.name as Name, position.name as Position, salary as Salary from people inner join position on people.posid = position.id where salary > ?

我如何创建两个表:

public static final String TABLE_POSITION = "position";
public static final String TABLE_PEOPLE = "people";

public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";

public static final String KEY_SALARY = "salary";
public static final String KEY_POSID = "posid";


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_POSITION + "(" + KEY_ID + " integer primary key," + KEY_NAME + " text," + KEY_SALARY + " integer" + ");");
    db.execSQL("create table " + TABLE_PEOPLE + "(" + KEY_ID + " integer primary key autoincrement," + KEY_NAME + " text," + KEY_POSID + " integer" + ");");
}

我如何填写位置表:

cursor = sqLiteDatabase.query(databaseHelper.TABLE_POSITION,null,null,null,null,null,null);
    if(cursor.getCount() == 0){
        for(int i = 0; i < position_id.length; i++){
            contentValues.put(databaseHelper.KEY_ID,position_id[i]);
            contentValues.put(databaseHelper.KEY_NAME,position_name[i]);
            contentValues.put(databaseHelper.KEY_SALARY,position_salary[i]);
            sqLiteDatabase.insert(databaseHelper.TABLE_POSITION,null,contentValues);
        }
    }
    cursor.close();

位置表数据:

    int[] position_id = { 1, 2, 3, 4 };
    String[] position_name = { "CEO", "Programmer", "Teacher", "Policeman" };
    int[] position_salary = { 15000, 13000, 10000, 8000 };

人员数据表:

String[] people_name = { "Ivan", "Maria", "Petr", "Anton", "Daria", "Boris", "John", "Suzy" };
int[] people_posid = { 2, 3, 2, 2, 3, 1, 2, 4 };

此行错误:

String sqlQuery = "select people.name as Name, position.name as Position, salary as Salary from people inner join position on people.posid = position.id where salary > ?";
        cursor = sqLiteDatabase.rawQuery(sqlQuery,new String[]{"12000"});
        logCursor(cursor); //priting out cursor to LOG.d
        cursor.close();

我知道没有自动增量的id也是自动增量的,但是我建议我必须知道位置表中用于内部联接的id。那么,如何解决这个错误呢?我知道这只是主键/自动递增错误

1 个答案:

答案 0 :(得分:0)

解决了people.posid = position._id ..但表中没有数据..(经过两个小时的思考)

相关问题