Reputation: 27
There is compilation error, when adding data to the database as follows
2019-09-25 08:29:29.332 6458-6458/com.mulazi.mymoviecatalogue E/SQLiteDatabase: Error inserting tittle =The Old Man & the Gun release_date =null overview =The true story of Forrest Tucker, from his audacious escape from San Quentin at the age of 70 to an unprecedented string of heists that confounded authorities and enchanted the public. Wrapped up in the pursuit are a detective, who becomes captivated with Forrest’s commitment to his craft, and a woman, who loves him in spite of his chosen profession. name =null poster_path =https://image.tmdb.org/t/p/w500null rating_bar =null rating =null
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: favorite.rating (code 1299 SQLITE_CONSTRAINT_NOTNULL)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:879) at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:790) at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:88) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1599) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1468) at com.mulazi.mymoviecatalogue.db.FavoriteDbHelper.addFavorite(FavoriteDbHelper.java:64) at com.mulazi.mymoviecatalogue.DetailActivity.saveFavorite(DetailActivity.java:141) at com.mulazi.mymoviecatalogue.DetailActivity$1.onClick(DetailActivity.java:52) at android.view.View.performClick(View.java:7251) at android.view.View.performClickInternal(View.java:7228) at android.view.View.access$3500(View.java:802) at android.view.View$PerformClick.run(View.java:27843) at android.os.Handler.handleCallback(Handler.java:883)
My FavDbHelper
package com.mulazi.mymoviecatalogue.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.mulazi.mymoviecatalogue.Model.Movie;
import java.util.ArrayList;
import java.util.List;
import static android.provider.BaseColumns._ID;
public class FavoriteDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME ="favorite1.db";
private static final int DATABASE_VERSION = 1;
public FavoriteDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
final String SQL_CREATE_FAVORITE_TABLE = "CREATE TABLE " + FavoriteContract.FavoriteEntry.TABLE_NAME + " (" +
FavoriteContract.FavoriteEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
FavoriteContract.FavoriteEntry.COLUMN_TITTLE + " TEXT, " +
FavoriteContract.FavoriteEntry.COLUMN_POSTERPATH + " TEXT NOT NULL," +
FavoriteContract.FavoriteEntry.COLUMN_OVERVIEW + " TEXT, " +
FavoriteContract.FavoriteEntry.COLUMN_RATING_BAR + " INTEGER NOT NULL, " +
FavoriteContract.FavoriteEntry.COLUMN_RATING +" TEXT NOT NULL ," +
FavoriteContract.FavoriteEntry.COLUMN_RELEASE_DATE + " TEXT NOT NULL, " +
FavoriteContract.FavoriteEntry.COLUMN_NAME + " TEXT " +
");";
sqLiteDatabase.execSQL(SQL_CREATE_FAVORITE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + FavoriteContract.FavoriteEntry.TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void addFavorite(Movie movie) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FavoriteContract.FavoriteEntry.COLUMN_TITTLE, movie.getTitle());
values.put(FavoriteContract.FavoriteEntry.COLUMN_POSTERPATH, movie.getPosterPath());
values.put(FavoriteContract.FavoriteEntry.COLUMN_OVERVIEW, movie.getOverview());
values.put(FavoriteContract.FavoriteEntry.COLUMN_RATING_BAR, movie.getVoteAverage());
values.put(FavoriteContract.FavoriteEntry.COLUMN_RATING, movie.getVoteCount());
values.put(FavoriteContract.FavoriteEntry.COLUMN_RELEASE_DATE, movie.getReleaseDate());
values.put(FavoriteContract.FavoriteEntry.COLUMN_NAME, movie.getName());
db.insert(FavoriteContract.FavoriteEntry.TABLE_NAME, null, values);
db.close();
}
public void deleteFavorite (String overview) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(FavoriteContract.FavoriteEntry.TABLE_NAME, FavoriteContract.FavoriteEntry.COLUMN_OVERVIEW + " = ", new String[]{overview});
}
public ArrayList<Movie> getAllFavorite () {
String[] columns ={
FavoriteContract.FavoriteEntry.TABLE_NAME,
FavoriteContract.FavoriteEntry._ID,
FavoriteContract.FavoriteEntry.COLUMN_TITTLE,
FavoriteContract.FavoriteEntry.COLUMN_POSTERPATH,
FavoriteContract.FavoriteEntry.COLUMN_OVERVIEW,
FavoriteContract.FavoriteEntry.COLUMN_RATING_BAR,
FavoriteContract.FavoriteEntry.COLUMN_RATING,
FavoriteContract.FavoriteEntry.COLUMN_RELEASE_DATE,
FavoriteContract.FavoriteEntry.COLUMN_NAME
};
String sortOrder =
FavoriteContract.FavoriteEntry._ID + " ASC";
ArrayList<Movie> favoriteList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(FavoriteContract.FavoriteEntry.TABLE_NAME,
columns,
null,
null,
null,
null,
sortOrder);
if (cursor.moveToFirst()) {
do {
Movie movie = new Movie();
movie.setName(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_NAME)));
movie.setTitle(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_TITTLE)));
movie.setPosterPath(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_POSTERPATH)));
movie.setOverview(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_OVERVIEW)));
movie.setReleaseDate(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_RELEASE_DATE)));
movie.setVoteAverage(Double.parseDouble(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COULOUMN_RATING))));
movie.setVoteCount(Integer.parseInt(cursor.getString(cursor.getColumnIndex(FavoriteContract.FavoriteEntry.COLUMN_RATING_BAR))));
favoriteList.add(movie);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return favoriteList;
}
}
The table
class FavoriteContract {
static final class FavoriteEntry implements BaseColumns {
static final String TABLE_NAME ="favorite";
static final String COLUMN_TITTLE = "tittle ";
static final String COLUMN_NAME = "name ";
static final String COLUMN_POSTERPATH = "poster_path ";
static final String COLUMN_OVERVIEW = "overview ";
static final String COLUMN_RATING ="rating ";
static final String COLUMN_RATING_BAR ="rating_bar ";
static final String COLUMN_RELEASE_DATE ="release_date ";
}
}
In my code, there are 3 null values, release date, rating bar and rating, so please tell me how to fix it, what should I do to overcome this
Upvotes: 2
Views: 8745
Reputation: 1271
As all three mentioned columns values can't be null while inserting row in table. Probably you can add default constraint to set the default values instead of explicitly passing null.
Upvotes: 1