[英]Can't write to preloaded database using SQLite Asset Helper
我正在使用SQLite Asset Helper访问预加载的数据库。 我可以成功读取它( getAllAuthors()
方法按预期工作),但是在写入时遇到了麻烦。
Log.i("insert", insertQuery)
和Log.i("select", selectQuery)
返回预期的结果,并且在执行insertQuery
或selectQuery
时没有错误。 但是,当我在用于SQLite的外部数据库浏览器中打开数据库时,数据库没有任何变化。
数据库助手
public class DatabaseHelper extends SQLiteAssetHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database9.db";
private static final String BOOKS = "books";
private static final String AUTHORS = "authors";
public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// THIS WORKS FINE
public ArrayList<Author> getAllAuthors() {
ArrayList<Author> authorList = new ArrayList<>();
// Select all query
String selectQuery = "SELECT id, name FROM " + AUTHORS + " ORDER BY name_alphabetic";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
// create new author object
Author author = new Author();
// set ID and name of author object
author.setID(Integer.parseInt(cursor.getString(0)));
author.setName(cursor.getString(1));
// pass author object to authorList array
authorList.add(author);
} while (cursor.moveToNext());
}
// return author list
return authorList;
}
public int setScrollPosition(int scrollY, int storyID) {
String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = " + scrollY + " WHERE id = " + storyID;
Log.i("insert", insertQuery);
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(insertQuery);
return scrollY;
}
public int getScrollPosition(int storyID) {
int scrollPosition = 0;
String selectQuery = "SELECT scroll_position FROM " + BOOKS + " WHERE id = " + storyID;
Log.i("select", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
scrollPosition = cursor.getInt(0);
} while (cursor.moveToNext());
}
return scrollPosition;
}
}
StoryBodyActivity
public class StoryBodyActivity extends AppCompatActivity {
private TextView storyBodyTextView;
private ScrollView storyBodyScrollView;
public int storyID;
Parcelable state;
int scrollY;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_story_body, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_body);
Bundle extras = getIntent().getExtras();
String story = extras.getString("story");
storyID = extras.getInt("story_id");
Log.i("stories", Integer.toString(storyID));
storyBodyTextView = (TextView) findViewById(R.id.story_body_text_view);
storyBodyScrollView = (ScrollView) findViewById(R.id.story_body_scroll_view);
DatabaseHelper db = new DatabaseHelper(this);
scrollY = db.getScrollPosition(storyID);
Log.i("scrolly", Integer.toString(scrollY));
storyBodyScrollView.scrollTo(0, scrollY);
String storyBody = db.getStoryBody(storyID);
storyBodyTextView.setText(Html.fromHtml(storyBody));
if(state != null) {
Log.d("pause", "trying to restore textview state..");
storyBodyTextView.onRestoreInstanceState(state);
}
int scroll = storyBodyScrollView.getScrollY();
Log.i("onCreate scroll", Integer.toString(scroll));
}
@Override
public void onPause() {
// Log.d("pause", "saving listview state @ onPause");
// state = storyBodyTextView.onSaveInstanceState();
scrollY = storyBodyScrollView.getScrollY();
Log.i("onPause scroll", Integer.toString(scrollY));
// Log.i("insert", Integer.toString(storyID));
DatabaseHelper db = new DatabaseHelper(this);
db.setScrollPosition(scrollY, storyID);
super.onPause();
}
@Override
public void onResume(){
super.onResume();
Log.i("onResume scroll", Integer.toString(scrollY));
storyBodyScrollView.scrollTo(0, scrollY);
}
}
解决了
使用下面接受的答案的组合,并解决ScrollView的问题( 此处为解决方案)。
资产是只写的/受保护的,如果您需要更新数据库,则必须确保将其复制到可以更新的位置。 例如默认的/ data / data / your_package / databases /文件夹。
SQLiteAssethelper将简单地做到这一点。
但是,当我在用于SQLite的外部数据库浏览器中打开数据库时,数据库没有任何变化。
您是否希望setScrollPosition
插入新行或更新现有行? 按照编码,它将执行后者。
建议不要使用execSQL执行更新,而SQLiteDatabase update
方法的优点是返回执行的更新次数。 因此,我建议改变:-
public int setScrollPosition(int scrollY, int storyID) {
String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = " + scrollY + " WHERE id = " + storyID;
Log.i("insert", insertQuery);
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(insertQuery);
return scrollY;
}
至 :-
public int setScrollPosition(int scrollY, int storyID) {
Contentvalues cv = new Contentvalues();
cv.put("scroll_position", scrolly);
String whereclasue = "id=?";
String[] whereargs = new String[]{storyID};
SQLiteDatabase db = this.getWritableDatabase();
int updates = db.update(BOOKS,cv,whereclause,whereargs);
if (updates > 0) {
Log.i("update","Updated " + Integer.toString(updates) + " rows.";
} else {
log.i("update","No Updates performed");
}
return scrollY;
}
您可能希望更改方法以返回更新的行数。
如果使用上述方法,并且日志中未执行任何更新 ,则这可能会突出显示有关数据库未更改的原因。
但是要澄清一下,更新的数据库不是资产文件夹中的数据库,而是位于data/data/your_package/databases/database9.db
您可能希望对此有所了解。 这包括允许您检查数据库和表的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.