gpt4 book ai didi

java - 如何将数组从 TMDB 保存到 SQLite

转载 作者:行者123 更新时间:2023-12-01 23:19:30 29 4
gpt4 key购买 nike

让我们直说吧。我想构建一个流行且适合 child 的电影应用程序,当数据被喜欢时,它存储在 SQLite 中。所以在最喜欢的布局中,我做了两种布局,流行的和 child 的。为了分发数据,我使用了子句类别:电影和流派_ids:16/not。但是当我测试我的应用程序(之前我重新安装了它,就像本网站中的建议一样)并且喜欢这部电影时,在 logcat 中显示如下内容:

    2019-10-11 09:24:05.941 5582-5582/com.example.apkfin5 E/SQLiteDatabase: Error inserting overview=blablabla. backdroppath=/n6bUvigpRFqSwmPp1m2YADdbRBc.jpg release_date=2019-10-02 [genre_ids]=null posterpath=/udDclJoHjfjb8Ekgsd4FDteOkCU.jpg id=475557 title=Joker category=movie
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: favorite.genre_ids (code 1299 SQLITE_CONSTRAINT_NOTNULL)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.example.apkfin5.db.FavHelper.insert(FavHelper.java:58)
at com.example.apkfin5.provider.FavProvider.insert(FavProvider.java:69)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:266)
at android.content.ContentResolver.insert(ContentResolver.java:1603)
at com.example.apkfin5.ui.activity.DetailFilmActivity.onOptionsItemSelected(DetailFilmActivity.java:170)
at android.app.Activity.onMenuItemSelected(Activity.java:3608)

你可以看到genre_ids为空,我设置了我的应用程序,当数据为空时,数据不会分发到SQLite。那么为什么我无法获取 Genre_ids ?

为了支持诊断,这是我的 dbHelper :

public class DbHelper extends SQLiteOpenHelper {

private static String DATABASE_NAME = "dbmovieapp";
private static final int DATABASE_VER = 1;
private static final String SQL_CREATE_FAVORITE = String.format(
"CREATE TABLE %s" + " (%s INTEGER PRIMARY KEY AUTOINCREMENT," +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s TEXT NOT NULL, " +
" %s NULL)",

TABLE_FAVORITE,
DbContract.Columns._ID,
DbContract.Columns.FAVID,
DbContract.Columns.BACKDROPPATH,
DbContract.Columns.POSTERPATH,
DbContract.Columns.TITLE,
DbContract.Columns.RELEASE_DATE,
DbContract.Columns.OVERVIEW,
DbContract.Columns.CATEGORY,
DbContract.Columns.GENRE

);

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VER);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_FAVORITE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE);
onCreate(sqLiteDatabase);
}
}

我最喜欢的 helper :

public class FavHelper {
private static final String DATABASE_TABLE = TABLE_FAVORITE;
private static DbHelper dbHelper;
private static FavHelper INSTANCE;

private static SQLiteDatabase database;

public FavHelper(Context context) {dbHelper = new DbHelper(context);}

public static FavHelper getInstance(Context context) {
if(INSTANCE == null) {synchronized (SQLiteOpenHelper.class){
if(INSTANCE == null) {INSTANCE = new FavHelper(context);}
}}
return INSTANCE;
}

public void open() throws SQLException {database = dbHelper.getWritableDatabase();}

public void close() {dbHelper.close();
if (database.isOpen())
database.close();
}

public Cursor queryById(String id) {
return database.query(DATABASE_TABLE,null,_ID + " = ?"
,new String[]{id}, null, null, null, null);}

public Cursor query() {
return database.query(DATABASE_TABLE, null,null,null,null,null,_ID + " DESC");}

public long insert(ContentValues contentValues) {return database.insert(DATABASE_TABLE,null,contentValues); }

public int update(String id, ContentValues contentValues) {return database.update(DATABASE_TABLE,contentValues,_ID + " = ?", new String[] {id});}

public int delete(String id) {return database.delete(DATABASE_TABLE, _ID + " = ?", new String[]{id});}

public static ArrayList<Favorite> getFilmFavorite(Cursor cursor) {
ArrayList<Favorite> arrayList = new ArrayList<>();

while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(_ID));
int favId = cursor.getInt(cursor.getColumnIndexOrThrow(FAVID));
String backdrop = cursor.getString(cursor.getColumnIndexOrThrow(BACKDROPPATH));
String posterpath = cursor.getString(cursor.getColumnIndexOrThrow(POSTERPATH));
String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
String overview = cursor.getString(cursor.getColumnIndexOrThrow(OVERVIEW));
String release = cursor.getString(cursor.getColumnIndexOrThrow(RELEASE_DATE));
String category = cursor.getString(cursor.getColumnIndexOrThrow(CATEGORY));
List genre = Collections.singletonList(cursor.getString(cursor.getColumnIndexOrThrow(String.valueOf(GENRE))));

if(!genre.equals("16") && category.equals("movie")) {
arrayList.add(new Favorite(id, favId, title, backdrop, posterpath, overview, release, category, genre));
}}

return arrayList;
}
public static ArrayList<Favorite> getFilmKidFavorite(Cursor cursor) {
ArrayList<Favorite> arrayList = new ArrayList<>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(_ID));
int favId = cursor.getInt(cursor.getColumnIndexOrThrow(FAVID));
String backdrop = cursor.getString(cursor.getColumnIndexOrThrow(BACKDROPPATH));
String posterpath = cursor.getString(cursor.getColumnIndexOrThrow(POSTERPATH));
String title = cursor.getString(cursor.getColumnIndexOrThrow(TITLE));
String overview = cursor.getString(cursor.getColumnIndexOrThrow(OVERVIEW));
String release = cursor.getString(cursor.getColumnIndexOrThrow(RELEASE_DATE));
String category = cursor.getString(cursor.getColumnIndexOrThrow(CATEGORY));
List genre = Collections.singletonList(cursor.getString(cursor.getColumnIndexOrThrow(String.valueOf(GENRE))));

if(genre.equals("16") && category.equals("movie")) {
arrayList.add(new Favorite(id, favId, title, backdrop, posterpath, overview, release, category, genre));
}
}
return arrayList;
}

我的数据库契约(Contract):

public class DbContract {


private static String SCHEME = "content";
public static String AUTHORITY = "com.example.apkfin5";

public static final class Columns implements BaseColumns {

public static String TABLE_FAVORITE = "favorite";
public static String FAVID = "id";
public static String BACKDROPPATH = "backdroppath";
public static String POSTERPATH = "posterpath";
public static String TITLE = "title";
public static String OVERVIEW = "overview";
public static String RELEASE_DATE = "release_date";
public static String CATEGORY = "category";
public static List GENRE = Collections.singletonList("genre_ids");

public static Uri C_URI = new Uri.Builder().scheme(SCHEME).authority(AUTHORITY)
.appendPath(TABLE_FAVORITE).build();
}

public static String getColumnString(Cursor cursor, String column) {return cursor.getString(cursor.getColumnIndex(column));}

public static int getColumnInt(Cursor cursor, String column) {return cursor.getInt(cursor.getColumnIndex(column));}

}

我的细节电影的一半,就像并将其存储在最喜欢的模型中:

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(cekFav()) {
Uri uri = Uri.parse(C_URI + "/" + id);
getContentResolver().delete(uri,null,null);
item.setIcon(R.drawable.ic_favorite_border_24dp);
Toast.makeText(this,getString(R.string.unFav), Toast.LENGTH_LONG).show();}
else {
favorite.setId(Favid);
favorite.setTitle(tittle);
favorite.setPosterPath(poster);
favorite.setBackdropPath(backdrop);
favorite.setReleasedate(releasedate);
favorite.setOverView(overView);
favorite.setGenre(Collections.singletonList(genre));
favorite.setCategoty("movie");

ContentValues contentValues = new ContentValues();
contentValues.put(FAVID,Favid);
contentValues.put(TITLE,tittle);
contentValues.put(OVERVIEW,overView);
contentValues.put(BACKDROPPATH,backdrop);
contentValues.put(RELEASE_DATE,releasedate);
contentValues.put(POSTERPATH,poster);
contentValues.put(String.valueOf(GENRE), genre);
contentValues.put(CATEGORY,"movie");

if (getContentResolver().insert(C_URI,contentValues) != null) {
Toast.makeText(this,tittle + " " + getString(R.string.Fav), Toast.LENGTH_LONG).show();
item.setIcon(R.drawable.ic_favorite);
} else { Toast.makeText(this, tittle + " " + getString(R.string.favError), Toast.LENGTH_LONG).show();}
}
sendRefreshBroadcast(getApplicationContext());
return super.onOptionsItemSelected(item);
}

用于存储 Api 数据的电影模型:

public class Film {
@SerializedName("id")
@Expose
private int id;

@SerializedName("backdrop_path")
@Expose
private String backdrop;

@SerializedName("title")
@Expose
private String title;

@SerializedName("poster_path")
@Expose
private String posterPath;

@SerializedName("release_date")
@Expose
private String releaseDate;

@SerializedName("vote_average")
@Expose
private float rating;

@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds;
@SerializedName("overview")
@Expose
private String overView;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBackdrop() {return backdrop;}

public void setBackdrop(String backdrop) {this.backdrop = backdrop;}


public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getPosterPath() {
return posterPath;
}

public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}

public String getReleaseDate() {
return releaseDate;
}

public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}

public float getRating() {
return rating;
}

public void setRating(float rating) {
this.rating = rating;
}

public List<Integer> getGenreIds() {
return genreIds;
}

public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}

public String getOverView() {return overView;}

public void setOverView(String overView) {this.overView = overView;}
}

然后是最喜欢的模型,用于存储数据,如下所示:

public class Favorite implements Parcelable {
private int id;
private int mId;
private String backdropPath;
private String posterPath;
private String title;
private String overview;
private String releasedate;
private String category;
private List genre;

public int getId() { return id; }

public void setId(int id) { this.id = id; }

public int getmId() { return mId; }

public void setmId(int mId) { this.mId = mId; }

public String getBackdropPath() {
return backdropPath;
}

public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}

public String getPosterPath() {
return posterPath;
}

public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getOverView() {
return overview;
}

public void setOverView(String overview) {
this.overview = overview;
}

public String getReleasedate() { return releasedate; }

public void setReleasedate(String releasedate) { this.releasedate = releasedate; }

public String getCategory() {return category;}
public void setCategoty(String categoty) {
this.category = categoty;
}

public List getGenre() {return genre;}
public void setGenre(List genre) {this.genre = genre;}

public Favorite() {
}
public Favorite(int id, int mId, String title, String backdrop, String posterpath, String overview, String release, String category, List genre) {
this.id = id;
this.mId = mId;
this.backdropPath = backdrop;
this.posterPath = posterpath;
this.overview = overview;
this.title = title;
this.releasedate = release;
this.category = category;
this.genre = genre;
}

protected Favorite(Parcel in) {
id = in.readInt();
mId = in.readInt();
backdropPath = in.readString();
posterPath = in.readString();
title = in.readString();
overview = in.readString();
releasedate = in.readString();
category = in.readString();
if (in.readByte() == 0x01) {
genre = new ArrayList<>();
in.readList(genre, Parcelable.class.getClassLoader());
} else {
genre = null;
}
}

public Favorite(Cursor cursor) {
this.id = getColumnInt(cursor,_ID);
this.mId = getColumnInt(cursor,FAVID);
this.title = getColumnString(cursor,TITLE);
this.category = getColumnString(cursor,CATEGORY);
this.posterPath = getColumnString(cursor,POSTERPATH);
this.overview = getColumnString(cursor, OVERVIEW);
this.backdropPath = getColumnString(cursor, BACKDROPPATH);
this.genre = Collections.singletonList(getColumnString(cursor, String.valueOf(GENRE)));
}

public static final Creator<Favorite> CREATOR = new Creator<Favorite>() {
@Override
public Favorite createFromParcel(Parcel in) {
return new Favorite(in);
}

@Override
public Favorite[] newArray(int size) {
return new Favorite[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeInt(mId);
parcel.writeString(backdropPath);
parcel.writeString(posterPath);
parcel.writeString(title);
parcel.writeString(overview);
parcel.writeString(releasedate);
parcel.writeString(category);
if (genre == null) {
parcel.writeByte((byte) (0x00));
} else {
parcel.writeByte((byte) (0x01));
parcel.writeList(genre);
}
}
}

这是我第一次分配使用 1 个 API 的两个不同 View 进行构建。所以,我在这里有点困惑。感谢您的回答。

我已经意识到我需要反序列化这个列表:genre_id,所以伙计们,你能告诉我如何反序列化“genre_ids”:[ 99、 10402 ]是否使用 Class List,然后值只是 Public int id ?或者我需要更多身份证件?因为某些genre_ids有多个int。

最佳答案

所以,我尝试用反序列化来解决,但我不明白如何将这些数据实现到数据库。

我能做的最好的事情就是创建另一个数据库,用于在单击时保存数据。在此之前,我从网络上构建了另一个请求,这样我只能从 child 那里获取数据。确保其他请求没有获取数据,将请求设置为“without_genres”,我正在使用 tmdb 顺便说一句。这是因为您不希望在选项卡 Collection 夹中的调节器电影和 child 节目中重复 child 的数据。此外,我使用该数据库来制作小部件。所以你不希望你的 2 个小部件(调节器和 child )都有类似的电影 child 。

关于java - 如何将数组从 TMDB 保存到 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58333549/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com