SQLite - select * from table返回0行

时间:2014-12-11 07:34:35

标签: android sqlite

我将虚拟数据插入表中然后检索它。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    mDbAdapter = DatabaseHelper.get(getApplicationContext()).getDbAdapter();

    insertDummyData();

    recentPostsArrayList = mDbAdapter
            .getRecords(Const.RECENT_POSTS_TABLE_NAME);

    listViewRecentPosts = (ListView) findViewById(R.id.record_listView_recent_posts);
    listViewRecentPosts.setAdapter(new RecentPostsAdapter());
}

insertDummyData():

private void insertDummyData() {

    Item item;

    mDbAdapter.beginTransaction();
    mDbAdapter.clearTable(Const.RECENT_POSTS_TABLE_NAME);

    item = new Item();
    item.setAuthor("Priyanka");
    item.setTitle("Priyanka");
    item.setComments("Awesome");
    item.setDate("11-12-2014");
    item.setId("1");
    item.setImagepath("image1");
    item.setLink("");
    item.setRatings("5");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);

    item = new Item();
    item.setAuthor("Jackson");
    item.setTitle("Jackson");
    item.setComments("Super");
    item.setDate("11-12-2014");
    item.setId("2");
    item.setImagepath("image2");
    item.setLink("");
    item.setRatings("4");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);

    item = new Item();
    item.setAuthor("Sami");
    item.setTitle("Sami");
    item.setComments("Good");
    item.setDate("11-12-2014");
    item.setId("3");
    item.setImagepath("image3");
    item.setLink("");
    item.setRatings("3");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);


    mDbAdapter.endTransaction();

}

getRecords():

public List<Item> getRecords(String tableName) {

    Item item;
    List<Item> items = new ArrayList<Item>();

    String query = "SELECT * FROM " + tableName;
    c = super.query(query);

    c.moveToFirst();

    do {

        item = new Item();
        item.setAuthor(c.getString(c
                .getColumnIndex(Const.TableColumns.AUTHOR_KEY)));
        item.setComments(c.getString(c
                .getColumnIndex(Const.TableColumns.COMMENTS_KEY)));
        item.setDate(c.getString(c
                .getColumnIndex(Const.TableColumns.DATE_KEY)));
        item.setId(c.getString(c.getColumnIndex(Const.TableColumns.ID_KEY)));
        item.setImagepath(c.getString(c
                .getColumnIndex(Const.TableColumns.IMAGEPATH_KEY)));
        item.setLink(c.getString(c
                .getColumnIndex(Const.TableColumns.LINK_KEY)));
        item.setRatings(c.getString(c
                .getColumnIndex(Const.TableColumns.RATINGS_KEY)));
        item.setTitle(c.getString(c
                .getColumnIndex(Const.TableColumns.TITLE_KEY)));

        items.add(item);

    } while (c.moveToNext());

    return items;

}

这是我的Logcat:

12-11 12:49:29.687: D/xx(23290): Table recentposts cleared
12-11 12:49:29.688: D/xx(23290): Row inserted into table recentposts
12-11 12:49:29.689: D/xx(23290): Row inserted into table recentposts
12-11 12:49:29.689: D/xx(23290): Row inserted into table recentposts
12-11 12:49:29.690: D/xx(23290): End Transaction
12-11 12:49:29.695: D/xx(23290): Query 'SELECT * FROM recentposts' returned 0 rows

我做错了什么?

3 个答案:

答案 0 :(得分:2)

mDbAdapter.setTransactionSuccessful();

之前添加此mDbAdapter.endTransaction();

答案 1 :(得分:2)

您正在使用交易而不会调用setTransactionSuccessful()

private void insertDummyData() {

    Item item;

    mDbAdapter.beginTransaction();
    mDbAdapter.clearTable(Const.RECENT_POSTS_TABLE_NAME);

    item = new Item();
    item.setAuthor("Priyanka");
    item.setTitle("Priyanka");
    item.setComments("Awesome");
    item.setDate("11-12-2014");
    item.setId("1");
    item.setImagepath("image1");
    item.setLink("");
    item.setRatings("5");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);

    item = new Item();
    item.setAuthor("Jackson");
    item.setTitle("Jackson");
    item.setComments("Super");
    item.setDate("11-12-2014");
    item.setId("2");
    item.setImagepath("image2");
    item.setLink("");
    item.setRatings("4");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);

    item = new Item();
    item.setAuthor("Sami");
    item.setTitle("Sami");
    item.setComments("Good");
    item.setDate("11-12-2014");
    item.setId("3");
    item.setImagepath("image3");
    item.setLink("");
    item.setRatings("3");
    mDbAdapter.insertRow(Const.RECENT_POSTS_TABLE_NAME, item);

    mDBAdapter.setTransactionSuccessfull();  //Mark the transaction as successfull 
    mDbAdapter.endTransaction();

}

答案 2 :(得分:1)

您是否可能忘记了;

我在日志中看到:

  Query 'SELECT * FROM recentposts'

您的代码是:

String query = "SELECT * FROM " + tableName;

您可以尝试:

String query = "SELECT * FROM " + tableName + ";";

我希望它有所帮助。