在 SQLite 数据库 Android 中选择 [英] Selecting in SQLite Database Android

查看:39
本文介绍了在 SQLite 数据库 Android 中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我创建一个新数据库并向其中插入数据时,我都可以选择插入的数据,但是第二次插入表中并尝试选择它时,它会显示第一个数据本身.

Whenever I Create a new database and insert a data into it, i am able to select the inserted data but the second time I insert into the table and try to select it, it shows me the first data itself.

这是onClickListener(从表中选择)的代码:

This is the code of the onClickListener(that selects from table):

mydb = openOrCreateDatabase("Countz_DB",MODE_PRIVATE,null);

mnew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            c = mydb.rawQuery("Select * from Countz",null);
            if (c.moveToFirst()){
                name = c.getString(0);
                desc = c.getString(1);
                Snackbar.make(v, "Name:"+name+" Desc:"+desc, Snackbar.LENGTH_LONG).show();
            }
        }
    });

这是我用来向表中插入值的代码:

This is the code that I use to insert values into the table:

public void InsertintoCount(){
    String sqlc = "INSERT INTO Countz VALUES('"+name+"','"+desc+"','"+x+"','"+cDT+"');";
    newdb.execSQL(sqlc);
    }

推荐答案

试试这个

if (c.moveToFirst()){
    do {
            name = c.getString(0);
            desc = c.getString(1);
            Snackbar.make(v, "Name:"+name+" Desc:"+desc, Snackbar.LENGTH_LONG).show();
    } while(c.moveToNext());
}

这篇关于在 SQLite 数据库 Android 中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆