我正在使用 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();
}
});
}
答案 0 :(得分:1)
希望您有一个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();
}
});
答案 1 :(得分:0)
好的,首先,您需要应用一些常见的面向对象编程原则,如 Single Responsibility Principle 。
您需要隔离数据库,以便对应用的业务规则一无所知。这是一个很好的starting point。
关于您的问题:
额外 btnInsert = (Button) findViewById(R.id.btnInsert);
Android Studio 3 +中不再需要强制转换,因此请移除强制转换以使其更简单= btnInsert = findViewById(R.id.btnInsert);
答案 2 :(得分:0)
self.d := dL/dX