首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游标始终为空,甚至成功插入了许多记录。

问游标始终为空,甚至成功插入了许多记录。
EN

Stack Overflow用户
提问于 2018-05-12 04:35:10
回答 3查看 87关注 0票数 0

我正在使用sqlite数据库编写一个简单的crud操作。当我按“插入”按钮时,数据将被成功插入。但是,当我按下“搜索”按钮时,光标总是显示为空,光标计数始终为0。我给我的代码.Please,特别是btnSearch.setOnclickListener和btnInsert.setOnclickListener下面的代码

代码语言:javascript
复制
 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();
        }
    });
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-12 07:11:15

代码语言:javascript
复制
Try This: To insert data into Info Table      

  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();
                insertData(name,addr,phn);

            }
        }); 


    public void insertData(String name, String address, String phoneNumber) {
                SQLiteDatabase database = dbHelper.getWritableDatabase();

                    ContentValues contentValues = new ContentValues();
                    contentValues.put("Name", name);
                    contentValues.put("phone", address);
                    contentValues.put("address", phoneNumber);
                    database.insert("Info", null, contentValues);

            }
票数 0
EN

Stack Overflow用户

发布于 2018-05-12 04:41:20

希望您有一个扩展了Database的SQLiteOpenHelper类,并在其中编写创建表的所有查询。

现在将cursor初始化为cursor = null,这将始终返回null值。

试着使用

代码语言:javascript
复制
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
}

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

代码语言:javascript
复制
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();
        }
    });
票数 1
EN

Stack Overflow用户

发布于 2018-05-12 04:50:40

好的,首先,您需要应用一些常见的面向对象编程原则,比如单一责任原则。

您需要隔离您的数据库,以便不了解应用程序的业务规则。这是一个很好的起点。

关于你的问题的:

  • 您没有正确地实现SQLite。SQLite需要扩展一个助手类,以便在需要时创建、更新和删除DB。检查这个文章,让您开始。

Extra: btnInsert = (Button) findViewById(R.id.btnInsert);强制转换在Android 3+中不再需要,因此删除强制转换以使其简单得多= btnInsert = findViewById(R.id.btnInsert);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50302879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档