我在Android中创建了一个简单的数据库。但现在我遇到了问题。 我尝试了很多次,但没有解决这个问题。 问题是当我想要插入新记录时,如果此记录已经输入或已经存在,那么它应该向我显示该记录已经存在的祝酒词。 DataBase class.java:
public class DatabaseOperation extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PASS";
public static final String COL_4 = "CONTACT";
public static final String COL_5 = "NIC";
public static final String COL_6= "CONFIRM";
public DatabaseOperation(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT,NAME TEXT,PASS TEXT,CONFIRM TEXT,NIC INTEGER,CONTACT
INTEGER)");}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String pass,String confrim,String
contact,String nic)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(COL_2,name);
cv.put(COL_3,pass);
cv.put(COL_6,confrim);
cv.put(COL_4,contact);
cv.put(COL_5,nic);
long result= db.insert(TABLE_NAME,null,cv);
if(result==-1)
return false;
else
return true;
}
public Cursor getAllData()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
public boolean updateData(String id,String name,String pass,String
confrim,String contact,String nic)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(COL_1,id);
cv.put(COL_2,name);
cv.put(COL_3,pass);
cv.put(COL_6,confrim);
cv.put(COL_4,contact);
cv.put(COL_5,nic);
db.update(TABLE_NAME,cv, "ID= ?",new String[]{id});
return true;
}
public Integer deletData(String id)
{
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
}
}
这是我的注册课程
registr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isInserted = myDb.insertData(name.getText().toString(), pass.getText().toString(),
confirm.getText().toString(), cont.getText().toString(), nic.getText().toString());
if (isInserted == true)
Toast.makeText(getBaseContext(), "INSERTED", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "not inserted", Toast.LENGTH_SHORT).show();
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayout.setVisibility(View.INVISIBLE);
}
});
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res= myDb.getAllData();
if(res.getCount()==0)
{
showMessage("error","no data found");
//Toast.makeText(getBaseContext(),"NO daTA found",Toast.LENGTH_SHORT).show();
}
StringBuffer buffer=new StringBuffer();
while(res.moveToNext())
{
buffer.append("id:"+res.getString(0)+"\n");
buffer.append("name:"+res.getString(1)+"\n");
buffer.append("nic:"+res.getString(4)+"\n");
buffer.append("contact:"+res.getString(5)+"\n\n");
}
showMessage("DATA",buffer.toString());
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean isUpdated=myDb.updateData(id.getText().toString(),name.getText().toString(),pass.getText().toString(),confirm.getText().toString(),
cont.getText().toString(),nic.getText().toString());
if(isUpdated==true)
Toast.makeText(getBaseContext(),"updated",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(),"not updated",Toast.LENGTH_SHORT).show();
}
});
delet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Integer deletedRows=myDb.deletData(id.getText().toString());
if(deletedRows > 0)
Toast.makeText(getBaseContext(),"data
deleted",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(),"not
deleted",Toast.LENGTH_SHORT).show();
}
});
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
答案 0 :(得分:2)
以下一段代码(方法)对于可理解的参数非常方便,请尝试检查某些记录是否已存在... 当记录已存在于您的数据库中时,以下方法将返回true。 必须在您的班级 DatabaseOperations
中声明此方法public boolean checkIfRecordExist(String nameOfTable,String columnName,String columnValue)
{
try
{
objDatabase=this.getReadableDatabase();
Cursor cursor=objDatabase.rawQuery("SELECT "+columnName+" FROM "+nameOfTable+" WHERE "+columnName+"='"+columnValue+"'",null);
if (cursor.moveToFirst())
{
objDatabase.close();
Log.d("Record Already Exists", "Table is:"+nameOfTable+" ColumnName:"+columnName);
return true;//record Exists
}
Log.d("New Record ", "Table is:"+nameOfTable+" ColumnName:"+columnName+" Column Value:"+columnValue);
objDatabase.close();
}
catch(Exception errorException)
{
Log.d("Exception occured", "Exception occured "+errorException);
objDatabase.close();
}
return false;
}
并在您的活动中将其称为
boolean recordExists= mydb.checkIfRecordExist(DatabaseOperations.TABLE_NAME ,DatabaseOperations.COL_1 ,valueToBeChecked)
if(recordExists)
{
//do your stuff
}
答案 1 :(得分:0)
在Register按钮内调用此方法OnClickListener - 验证输入的名称和密码已存在于数据库中。
/**
* Used to check name and password is already in db.
*
* @param name
* @param pass
* @return true - name and password already exists in db.
* false - name and password not exists in db.
*/
public boolean dataAlreadyExists(String name, String pass) {
SQLiteDatabase db =...;
String query="SELECT COUNT(*) FROM " + TABLE_NAME + " WHERE NAME ="+name+ " PASS ="+pass+";";
long count = DatabaseUtils.queryNumEntries(db,query,null);
return count > 0;
}