android.database.sqlite.SQLiteException:模糊列名:movie_id

时间:2017-11-20 17:22:26

标签: android sqlite

我有以下表格。

    final String SQL_CREATE_MOVIE__SETTING_TABLE = "CREATE TABLE " + 
    MovieContract.MovieSettings.TABLE_NAME + " (" +
            MovieContract.MovieEntry._ID + " INTEGER PRIMARY KEY, " +
            MovieContract.MovieSettings.COL_MOVIE_SETTING + " TEXT 
    UNIQUE NOT NULL " +
            " );";

    final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + MovieContract.MovieEntry.TABLE_NAME + " (" +
            MovieContract.MovieEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            MovieContract.MovieEntry.COL_MOVIE_KEY + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_MOVIE_ID + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_BACKDROP_PATH + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_ORIGINAL_LANGUAGE + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_ORIGINAL_TITLE + " TEXT NOT NULL," +
            MovieContract.MovieEntry.COL_OVERVIEW + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_TITLE + " TEXT NOT NULL, " +
            MovieContract.MovieEntry.COL_VOTE_AVERAGE + " REAL NOT NULL, " +
            MovieContract.MovieEntry.COL_VOTE_COUNT + " INTEGER NOT NULL, " +
            MovieContract.MovieEntry.COL_POSTER_PATH + " TEXT NOT NULL, "  +
            MovieContract.MovieEntry.COL_DATE + " TEXT NOT NULL, " +

            // Set up the location column as a foreign key to location table.
            " FOREIGN KEY (" + MovieContract.MovieEntry.COL_MOVIE_KEY + ") REFERENCES " +
            MovieContract.MovieSettings.TABLE_NAME + " (" + MovieContract.MovieSettings._ID + "), " +

            // To assure the application have just one weather entry per day
            // per location, it's created a UNIQUE constraint with REPLACE strategy
            " UNIQUE (" + MovieContract.MovieEntry.COL_DATE + ", " +
            MovieContract.MovieEntry.COL_MOVIE_KEY + ") ON CONFLICT REPLACE);";

    final String SQL_CREATE_TRAILER_TABLE = "CREATE TABLE " + MovieContract.TrailerEntry.TABLE_NAME + " (" +
            MovieContract.TrailerEntry._ID + " INTEGER PRIMARY KEY," +
            MovieContract.TrailerEntry.COL_MOVIE_ID + " INTEGER NOT NULL, " +
            MovieContract.TrailerEntry.COL_TRAILER_ID + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_ISO_369_1 + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_KEY + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_NAME + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_SITE + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_SIZE + " TEXT NOT NULL, " +
            MovieContract.TrailerEntry.COL_TYPE + " TEXT NOT NULL, " +


            // Set up the movie_id column as a foreign key to movie table.
            " FOREIGN KEY (" + MovieContract.TrailerEntry.COL_MOVIE_ID + ") REFERENCES " +
            MovieContract.MovieEntry.TABLE_NAME + " (" + MovieContract.MovieEntry.COL_MOVIE_ID + ") " +

            " UNIQUE (" + MovieContract.TrailerEntry.COL_MOVIE_ID + ", " +
            MovieContract.TrailerEntry.COL_TRAILER_ID + ") ON CONFLICT REPLACE);";

我内心加入他们

        //This is an inner join which looks like
        //movies INNER JOIN settings ON movies.setting_id = 
        settings._id
        sMovieBySettingQueryBuilder.setTables(
                MovieContract.MovieEntry.TABLE_NAME + " INNER JOIN "+
                MovieContract.MovieSettings.TABLE_NAME + " ON " +
                MovieContract.MovieEntry.TABLE_NAME  + "." + 
                MovieContract.MovieEntry.COL_MOVIE_KEY + " = " +
                MovieContract.MovieSettings.TABLE_NAME + "." + 
                MovieContract.MovieSettings._ID);


        sTrailerByMovieIdQueryBuilder.setTables(
                MovieContract.TrailerEntry.TABLE_NAME + " INNER JOIN " +
                        MovieContract.MovieEntry.TABLE_NAME +
                        " ON " + MovieContract.TrailerEntry.TABLE_NAME +
                        "." + MovieContract.TrailerEntry.COL_MOVIE_ID +
                        " = " + MovieContract.MovieEntry.TABLE_NAME +
                        "." + MovieContract.MovieEntry.COL_MOVIE_ID
        );

我存储了从电影db api中获得的所有json数据,没有任何问题。我还可以在gridview中显示每部电影的图像。接下来,我点击一部电影,并希望在列表视图中显示所有预告片(以及稍后的评论)。但是,当我从gridview中点击一部电影时,我收到了这个错误。

  Caused by: android.database.sqlite.SQLiteException: ambiguous 
  column name: movie_id (code 1): , while compiling: SELECT 
  trailers._id, movie_id, id, iso_639_1, key, name, site, size, type 
  FROM trailers INNER JOIN movies ON trailers.movie_id = 
  movies.movie_id WHERE (trailers.movie_id = ? )

我所有的内心联盟都是正确的。所以我不知道为什么会出现这个错误。我已将项目上传到github,以便您查看。

这是我用于预告片的预测。

public static final String[] MOVIE_TRAILER_COLUMNS = {
        MovieContract.TrailerEntry.TABLE_NAME + "." +
        MovieContract.TrailerEntry._ID,
        MovieContract.TrailerEntry.COL_MOVIE_ID,
        MovieContract.TrailerEntry.COL_TRAILER_ID,
        MovieContract.TrailerEntry.COL_ISO_369_1,
        MovieContract.TrailerEntry.COL_KEY,
        MovieContract.TrailerEntry.COL_NAME,
        MovieContract.TrailerEntry.COL_SITE,
        MovieContract.TrailerEntry.COL_SIZE,
        MovieContract.TrailerEntry.COL_TYPE
};

public static final int COL_MOVIE_PK_ID = 0;
public static final int COL_MOVIE_TR_ID = 1;
public static final int COL_TRAILER_ID = 2;
public static final int COL_ISO_369_1 = 3;
public static final int COL_KEY = 4;
public static final int COL_NAME = 5;
public static final int COL_SITE = 6;
public static final int COL_SIZE = 7;
public static final int COL_TYPE = 8;

1 个答案:

答案 0 :(得分:3)

你有: - SELECT trailers._id, movie_id, id, iso_639_1, key, name, site, size, type FROM trailers INNER JOIN movies ON trailers.movie_id = movies.movie_id WHERE (trailers.movie_id = ? )

歧义是首次出现 movie_id (即trailers._id, movie_id ),其中一个是什么? 是 trailers.movi​​e_id 还是 movies.movi​​e_id

因此,如果您想在结果游标(* 1)

中同时使用trailers.movie_idmovies.movie_id或两者,则需要使用它们

您可能也会遇到与 id 列相同的问题,即哪个ID列。

* 1 如果您同时使用trailers.movie_idmovies.movie_id,则结果游标中列的名称都将为movie_id。然后,除非使用 AS 重命名列,否则必须使用列偏移量。

您可以使用: -

SELECT trailers._id, trailers.movie_id, trailers.id, movies.movie_id AS movies_movie_id, movies._id AS movies_id, iso_639_1, key, name, site, size, type FROM trailers INNER JOIN movies ON trailers.movie_id = movies.movie_id WHERE (trailers.movie_id = ? )

评论后添加

  

但是如何对列进行更改,即trailers.movi​​e_id和   trailers.id。我使用了我更新的问题中显示的预测。

public static final String[] MOVIE_TRAILER_COLUMNS = {
        MovieContract.TrailerEntry.TABLE_NAME + "." +
        MovieContract.TrailerEntry._ID,
        MovieContract.TrailerEntry.COL_MOVIE_ID,
        MovieContract.TrailerEntry.COL_TRAILER_ID,
        MovieContract.TrailerEntry.COL_ISO_369_1,
        MovieContract.TrailerEntry.COL_KEY,
        MovieContract.TrailerEntry.COL_NAME,
        MovieContract.TrailerEntry.COL_SITE,
        MovieContract.TrailerEntry.COL_SIZE,
        MovieContract.TrailerEntry.COL_TYPE
};

可能(假设没有其他后果): -

public static final String[] MOVIE_TRAILER_COLUMNS = {
        MovieContract.TrailerEntry.TABLE_NAME + "." +
        MovieContract.TrailerEntry._ID,
        MovieContract.TrailerEntry.TABLE_NAME + "." +
        MovieContract.TrailerEntry.COL_MOVIE_ID,
        MovieContract.TrailerEntry.TABLE_NAME + "." +
        MovieContract.TrailerEntry.COL_TRAILER_ID,
        MovieContract.TrailerEntry.COL_ISO_369_1,
        MovieContract.TrailerEntry.COL_KEY,
        MovieContract.TrailerEntry.COL_NAME,
        MovieContract.TrailerEntry.COL_SITE,
        MovieContract.TrailerEntry.COL_SIZE,
        MovieContract.TrailerEntry.COL_TYPE
};

应该给你: -

SELECT trailers._id, trailers.movie_id, trailers.id, iso_639_1, key, name, site, size, type FROM trailers INNER JOIN movies ON trailers.movie_id = movies.movie_id WHERE (trailers.movie_id = ? )