似乎 SQLite table 由于未知原因已停止填充
It seems that the SQLite table had stopped being populated for unknown reason
应用程序工作正常,但方法 Cursor.moveToFirst()
开始返回 false,并且应用程序的列表已停止出现在片段中。
我想这一切都是从我改变了一个constat的名字开始的:IMAGE_URI
到IMAGE_URL
,imageUri
到imageUrl
。虽然不确定。
我尝试调试和搜索类似案例,但原因多种多样。调试时,我无法到达 SQLiteHelper
class 的 onCreate
以了解在 table 填充期间发生了什么(似乎 table 存在,因为正在创建 table 的空数据库),因此我无法调试或搜索类似问题,因为在 Logcat
.[=22= 中找不到相关输出]
获取数据库
此class用于与SQLiteOpenHelper
class的一些通信。
public class GetDatabase {
private static final int DB_VERSION = 1; //required for the constructor
public static final String dbName = "moviesList";
private SQLiteOpenHelper sqLiteOpenHelper;
private SQLiteDatabase db ;
public GetDatabase(Context context) {
Log.d("GetDatabase", "Cont.");
this.sqLiteOpenHelper = new SQLiteHelper(context, dbName, null, DB_VERSION);
}
public void open() {
db = sqLiteOpenHelper.getWritableDatabase();
}
public void close() {
if (sqLiteOpenHelper != null) {
sqLiteOpenHelper.close();
}
}
public ArrayList<Movie> getMovies() {
String[] columns = {
Movie.ID,
Movie.TITLE,
Movie.IMAGE_URL,
Movie.RATING,
Movie.RELEASE_YEAR,
Movie.GENRE
};
// sorting orders
String sortOrder =
Movie.RELEASE_YEAR + " ASC";
ArrayList<Movie> moviesList = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME, //Table to query
columns,
null,
null,
null,
null,
sortOrder);
if (cursor.moveToFirst()) {
do {
Movie movie = new Movie();
movie.setMovieId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Movie.ID))));
movie.setTitle(cursor.getString(cursor.getColumnIndex(Movie.TITLE)));
movie.setImageUrl(cursor.getString(cursor.getColumnIndex(Movie.IMAGE_URL)));
movie.setRating(cursor.getDouble(cursor.getColumnIndex(Movie.RATING)));
movie.setReleaseYear(cursor.getInt(cursor.getColumnIndex(Movie.RELEASE_YEAR)));
movie.setGenre(cursor.getString(cursor.getColumnIndex(Movie.GENRE)));
// Adding a movie to the list
moviesList.add(movie);
} while (cursor.moveToNext());
}
Log.d(TAG, "The movies list from sqlite: " + moviesList);
cursor.close();
db.close();
return moviesList;
}
private Cursor getData(int id) {
return db.rawQuery( "select * from " + TABLE_NAME + " where id="+id+"", null );
}
public Cursor getMovieDetails(int movieId) {// For viewing details of a specific item
Cursor cursor = getData(movieId);
cursor.moveToFirst();
return cursor;
}
}
SQliteHelper
SQLiteOpenHelper class
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "movies.db";
private static final int DB_VERSION = 1; //required for the constructor
private static final String TABLE_NAME = "movies";
private String fileName = "movies.json";
private String appName = "MoviesList";
private String path = getExternalStorageDirectory() + "/" + appName + "/" + fileName;
private String jsonString = null;
private SQLiteDatabase db;
// The cont. for saving to SQLite .Called from SplashActivity after downloading the JSON
public SQLiteHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "At SQLiteHelper");
createSQLIteTable(db);
if(tableIsEmpty(db)) {
try {
parseJsonAndInsertToSQLIte(db);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void createSQLIteTable(SQLiteDatabase db) {
//creating a table for SQLite
String CREATE_SQL_TABLE_STRING = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME
+ " ("
+ Movie.ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ Movie.TITLE + " TEXT,"
+ Movie.IMAGE_URL + " TEXT,"
+ Movie.RATING + " TEXT,"
+ Movie.RELEASE_YEAR + " TEXT,"
+ Movie.GENRE + " TEXT "
+ ")";
Log.i(TAG,"created sql table: "+CREATE_SQL_TABLE_STRING);
db.execSQL(CREATE_SQL_TABLE_STRING);
}
private void parseJsonAndInsertToSQLIte(SQLiteDatabase db) throws JSONException {
// parsing the json
String jsonString = getJsonFileData();
JSONArray moviesArray = new JSONArray(jsonString);
ContentValues insertValues;
for (int i = 0; i < moviesArray.length(); i++) {
JSONObject jsonObject = moviesArray.getJSONObject(i);
String title = jsonObject.getString("title");
String imageUrl = jsonObject.getString("imageUrl");
String rating = jsonObject.getString("rating");
String releaseYear = jsonObject.getString("releaseYear");
String genre = jsonObject.getString("genre");
insertValues = new ContentValues();
insertValues.put(Movie.TITLE, title);
insertValues.put(Movie.IMAGE_URL, imageUrl);
insertValues.put(Movie.RATING, rating);
insertValues.put(Movie.RELEASE_YEAR, releaseYear);
insertValues.put(Movie.GENRE, genre);
long res = db.insert(TABLE_NAME, null, insertValues);
Log.i(TAG, "parsed and inserted to sql - row: " + res);
}
}
private String getJsonFileData() {
//loading the jsonString
try {
InputStream in = new FileInputStream(new File(path));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder output = new StringBuilder();
while ((jsonString = reader.readLine()) != null) {
output.append(jsonString);
}
System.out.println(output.toString()); //Prints the string content read from input stream
jsonString = output.toString();
Log.d(TAG, "the jsonString was loaded");
} catch (IOException ex) {
ex.printStackTrace();
}
return jsonString;
}
}
如果我不解决这个问题,我认为整个应用程序毫无价值,因为我不知道需要更正什么错误。
谢谢!
从设置中清除您的应用程序数据并重新运行您的应用程序。由于您在数据库列中进行了更改,除非您清除数据,否则它不会反映出来。
或者
正确处理数据库版本。每次更改数据库结构时,删除 onUpgrade 方法中的所有表并增加数据库版本。
编辑:清除数据window
应用程序工作正常,但方法 Cursor.moveToFirst()
开始返回 false,并且应用程序的列表已停止出现在片段中。
我想这一切都是从我改变了一个constat的名字开始的:IMAGE_URI
到IMAGE_URL
,imageUri
到imageUrl
。虽然不确定。
我尝试调试和搜索类似案例,但原因多种多样。调试时,我无法到达 SQLiteHelper
class 的 onCreate
以了解在 table 填充期间发生了什么(似乎 table 存在,因为正在创建 table 的空数据库),因此我无法调试或搜索类似问题,因为在 Logcat
.[=22= 中找不到相关输出]
获取数据库
此class用于与SQLiteOpenHelper
class的一些通信。
public class GetDatabase {
private static final int DB_VERSION = 1; //required for the constructor
public static final String dbName = "moviesList";
private SQLiteOpenHelper sqLiteOpenHelper;
private SQLiteDatabase db ;
public GetDatabase(Context context) {
Log.d("GetDatabase", "Cont.");
this.sqLiteOpenHelper = new SQLiteHelper(context, dbName, null, DB_VERSION);
}
public void open() {
db = sqLiteOpenHelper.getWritableDatabase();
}
public void close() {
if (sqLiteOpenHelper != null) {
sqLiteOpenHelper.close();
}
}
public ArrayList<Movie> getMovies() {
String[] columns = {
Movie.ID,
Movie.TITLE,
Movie.IMAGE_URL,
Movie.RATING,
Movie.RELEASE_YEAR,
Movie.GENRE
};
// sorting orders
String sortOrder =
Movie.RELEASE_YEAR + " ASC";
ArrayList<Movie> moviesList = new ArrayList<>();
Cursor cursor = db.query(TABLE_NAME, //Table to query
columns,
null,
null,
null,
null,
sortOrder);
if (cursor.moveToFirst()) {
do {
Movie movie = new Movie();
movie.setMovieId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Movie.ID))));
movie.setTitle(cursor.getString(cursor.getColumnIndex(Movie.TITLE)));
movie.setImageUrl(cursor.getString(cursor.getColumnIndex(Movie.IMAGE_URL)));
movie.setRating(cursor.getDouble(cursor.getColumnIndex(Movie.RATING)));
movie.setReleaseYear(cursor.getInt(cursor.getColumnIndex(Movie.RELEASE_YEAR)));
movie.setGenre(cursor.getString(cursor.getColumnIndex(Movie.GENRE)));
// Adding a movie to the list
moviesList.add(movie);
} while (cursor.moveToNext());
}
Log.d(TAG, "The movies list from sqlite: " + moviesList);
cursor.close();
db.close();
return moviesList;
}
private Cursor getData(int id) {
return db.rawQuery( "select * from " + TABLE_NAME + " where id="+id+"", null );
}
public Cursor getMovieDetails(int movieId) {// For viewing details of a specific item
Cursor cursor = getData(movieId);
cursor.moveToFirst();
return cursor;
}
}
SQliteHelper
SQLiteOpenHelper class
public class SQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "movies.db";
private static final int DB_VERSION = 1; //required for the constructor
private static final String TABLE_NAME = "movies";
private String fileName = "movies.json";
private String appName = "MoviesList";
private String path = getExternalStorageDirectory() + "/" + appName + "/" + fileName;
private String jsonString = null;
private SQLiteDatabase db;
// The cont. for saving to SQLite .Called from SplashActivity after downloading the JSON
public SQLiteHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "At SQLiteHelper");
createSQLIteTable(db);
if(tableIsEmpty(db)) {
try {
parseJsonAndInsertToSQLIte(db);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void createSQLIteTable(SQLiteDatabase db) {
//creating a table for SQLite
String CREATE_SQL_TABLE_STRING = "CREATE TABLE IF NOT EXISTS "+TABLE_NAME
+ " ("
+ Movie.ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ Movie.TITLE + " TEXT,"
+ Movie.IMAGE_URL + " TEXT,"
+ Movie.RATING + " TEXT,"
+ Movie.RELEASE_YEAR + " TEXT,"
+ Movie.GENRE + " TEXT "
+ ")";
Log.i(TAG,"created sql table: "+CREATE_SQL_TABLE_STRING);
db.execSQL(CREATE_SQL_TABLE_STRING);
}
private void parseJsonAndInsertToSQLIte(SQLiteDatabase db) throws JSONException {
// parsing the json
String jsonString = getJsonFileData();
JSONArray moviesArray = new JSONArray(jsonString);
ContentValues insertValues;
for (int i = 0; i < moviesArray.length(); i++) {
JSONObject jsonObject = moviesArray.getJSONObject(i);
String title = jsonObject.getString("title");
String imageUrl = jsonObject.getString("imageUrl");
String rating = jsonObject.getString("rating");
String releaseYear = jsonObject.getString("releaseYear");
String genre = jsonObject.getString("genre");
insertValues = new ContentValues();
insertValues.put(Movie.TITLE, title);
insertValues.put(Movie.IMAGE_URL, imageUrl);
insertValues.put(Movie.RATING, rating);
insertValues.put(Movie.RELEASE_YEAR, releaseYear);
insertValues.put(Movie.GENRE, genre);
long res = db.insert(TABLE_NAME, null, insertValues);
Log.i(TAG, "parsed and inserted to sql - row: " + res);
}
}
private String getJsonFileData() {
//loading the jsonString
try {
InputStream in = new FileInputStream(new File(path));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder output = new StringBuilder();
while ((jsonString = reader.readLine()) != null) {
output.append(jsonString);
}
System.out.println(output.toString()); //Prints the string content read from input stream
jsonString = output.toString();
Log.d(TAG, "the jsonString was loaded");
} catch (IOException ex) {
ex.printStackTrace();
}
return jsonString;
}
}
如果我不解决这个问题,我认为整个应用程序毫无价值,因为我不知道需要更正什么错误。
谢谢!
从设置中清除您的应用程序数据并重新运行您的应用程序。由于您在数据库列中进行了更改,除非您清除数据,否则它不会反映出来。
或者
正确处理数据库版本。每次更改数据库结构时,删除 onUpgrade 方法中的所有表并增加数据库版本。
编辑:清除数据window