我正在使用sqlite数据库编写一个简单的crud操作。当我按“插入”按钮时,数据将被成功插入。但是,当我按下“搜索”按钮时,光标总是显示为空,光标计数始终为0。我给我的代码.Please,特别是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();
}
});
}发布于 2018-05-12 07:11:15
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);
}发布于 2018-05-12 04:41:20
希望您有一个扩展了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();
}
});发布于 2018-05-12 04:50:40
https://stackoverflow.com/questions/50302879
复制相似问题