首页 > 解决方案 > 即使成功插入许多记录,游标也始终为空

问题描述

我正在使用sqlite数据库编写一个简单的 crud 操作。当我按下插入按钮时,数据插入成功。但是当我按下搜索按钮时,光标始终显示为空,光标计数始终为 0。我正在给出我的代码。请具体查看下面的代码btnSearch.setOnclickListenerbtnInsert.setOnclickListener

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_db2);
    btnInsert = (Button) findViewById(R.id.btnInsert);
    btnSearch = (Button) findViewById(R.id.btnSearch);
    btnDelete = (Button) findViewById(R.id.btnDelete);
    btnUpdate = (Button) findViewById(R.id.btnUpdate);

    edname = findViewById(R.id.editText1);
    edphone = findViewById(R.id.editText2);
    edaddress = findViewById(R.id.editText3);

    db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Info(Name Varchar, phone Varchar, address Varchar)");


    btnSearch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           Cursor cursor=null;
            try {if(cursor.moveToNext()) {

                    Toast.makeText(DB.this, "button pressed", Toast.LENGTH_SHORT).show();
                    int count = cursor.getCount();
                    Toast.makeText(DB.this, String.valueOf(count), Toast.LENGTH_SHORT).show();
                    Toast.makeText(DB.this, cursor.getString(cursor.getColumnIndex("Name")), Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(DB.this, "cursor is null", Toast.LENGTH_LONG).show();
                }

            }
            catch (Exception ex){
                Toast.makeText(DB.this, "ERROR: "+ ex.toString(), Toast.LENGTH_LONG).show();
            }
           }
    });

    btnInsert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = edname.getText().toString();
            String phn = edphone.getText().toString();
            String addr = edaddress.getText().toString();
            String query = "INSERT INTO Info (Name,phone,address)VALUES("+name+","+phn+","+addr+")";
            Toast.makeText(DB.this, "data inserted", Toast.LENGTH_SHORT).show();
        }
    });
}

标签: androidsqliteandroid-sqlite

解决方案


希望你有一个Database可以扩展的类SQLiteOpenHelper,请写下你所有的查询以在其中创建表。

现在您正在初始化cursorcursor = null,这将始终返回null值。

尝试使用

Cursor lCursor = null;

try {
    SQLiteDatabase lDataBase = this.getReadableDatabase();
    lCursor = lDataBase.rawQuery(pQuery, null);  // pQuery = you query for search

    if (lCursor != null && lCursor.getCount() > 0) {
        Toast.makeText(DB.this, "button pressed", Toast.LENGTH_SHORT).show();
        int count = cursor.getCount();
        Toast.makeText(DB.this, String.valueOf(count), Toast.LENGTH_SHORT).show();
        Toast.makeText(DB.this, cursor.getString(cursor.getColumnIndex("Name")), Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(DB.this, "cursor is null", Toast.LENGTH_LONG).show();
    }
} catch (Exception ex) {
    // Do something when exception occurs
}

还有一件事:您的数据没有存储在数据库中。因为您没有执行查询。用这个

btnInsert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = edname.getText().toString();
            String phn = edphone.getText().toString();
            String addr = edaddress.getText().toString();
            String query = "INSERT INTO Info (Name,phone,address)VALUES("+name+","+phn+","+addr+")";
            db.execSQL(query);
            Toast.makeText(DB.this, "data inserted", Toast.LENGTH_SHORT).show();
        }
    });

推荐阅读