SQLite UPDATE命令不更新数据库

时间:2017-11-10 22:13:57

标签: android sqlite android-sqlite

我想在用户离开View后保存用户的ScrollView位置。

我这样做的方法是尝试捕获getScrollY onPause的位置并将其保存到数据库中,然后在用户返回时检索它。

我可以成功添加和检索位置(Log.i("scrolly", Integer.toString(scrollY))按预期返回),但ScrollView没有跳到正确的位置。

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("scroll", Integer.toString(scroll));

    }

    @Override
    public void onPause() {

        scrollY = storyBodyScrollView.getScrollY();
        Log.i("scroll", Integer.toString(scrollY));
        Log.i("insert", Integer.toString(storyID));
        DatabaseHelper db = new DatabaseHelper(this);
        db.setScrollPosition(scrollY, storyID);
        super.onPause();
    }

}

我的DatabaseHelper的一部分:

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);
        setForcedUpgrade();
    }

    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 0;

    }

    public int getScrollPosition(int storyID) {

        int scrollPosition = 0;

        String selectQuery = "SELECT scroll_position FROM " + BOOKS + " WHERE id = '" + storyID + "'";

        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;

       }
}

更新

我的架构:

    CREATE TABLE "books" (
`id` INTEGER NOT NULL,
`title` TEXT,
`author_id` INTEGER,
`collection` TEXT,
`body` TEXT,
`scroll_position` INTEGER,
PRIMARY KEY(`id`) )

1 个答案:

答案 0 :(得分:0)

在INSERT命令中,您传递字符串作为ID,而它需要整数

String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = '" + scrollY + "' WHERE id = '" + storyID + "'";
你知道吗? storyID作为字符串传递("' WHERE id = '" + storyID + "'")!!
单引号将id标记为字符串。

如果您创建一个整数列,它将永远不会匹配字符串。

CREATE TABLE "books" (
`id` INTEGER NOT NULL,
`title` TEXT,
`author_id` INTEGER,
`collection` TEXT,
`body` TEXT,
`scroll_position` INTEGER,
PRIMARY KEY(`id`) )

id字段创建为整数(这很好) 但是你不能在命令中传递一个字符串(也不能在你的查询中)。

修改

您还将字符串传递给scroll_position字段 这是整数。 这也行不通。