android - 即使成功插入许多记录,游标也始终为空
问题描述
我正在使用sqlite数据库编写一个简单的 crud 操作。当我按下插入按钮时,数据插入成功。但是当我按下搜索按钮时,光标始终显示为空,光标计数始终为 0。我正在给出我的代码。请具体查看下面的代码btnSearch.setOnclickListener
和btnInsert.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();
}
});
}
解决方案
希望你有一个Database
可以扩展的类SQLiteOpenHelper
,请写下你所有的查询以在其中创建表。
现在您正在初始化cursor
为cursor = 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();
}
});
推荐阅读
- reactjs - React - 问题渲染组件嵌套在函数中
- r - 在 R 中,创建一列用引号括起来并用逗号分隔的标签名称
- python - Jupyter notebook 部分情节中的 Matplotlib 笔记本魔法
- python - 无法弄清楚如何停止在列表中返回列表(leetcode 问题)
- android - 如何在 Kotlin 中通过名称动态调用类
- javascript - 如果捕获的匹配为空,如何避免捕获组?
- bash - 组合多个参数扩展的正确语法是什么?
- flutter - How can I query another collection inside a streambuilder that queries a different collection?
- ruby - Shopify script to allow checkout only when all products contain same tag
- reactjs - 具有多个参数的反应路由器