我的数据库中有4列(id,name,surname,image)。我只想检查输入的名称是否已存在于" name"柱。
这是我的SQLiteOpenHelper:
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public void queryData(String sql){
SQLiteDatabase database = getWritableDatabase();
database.execSQL(sql);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
public void insertData(String name, String surname, byte[] image){
SQLiteDatabase database = getWritableDatabase();
String sql = "INSERT INTO STUDENTS VALUES (NULL, ?, ?, ?)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, name);
statement.bindString(2, surname);
statement.bindBlob(3, image);
statement.executeInsert();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
/*public void removeSingleContact(String name, String price, byte[] image) {
SQLiteDatabase dbb = getWritableDatabase();
dbb.delete(name, surname, image);
}*/
public Cursor getData(String sql){
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sql, null);
}
}
这就是我添加到SQlite的方式;
public class AddEditStudent extends AppCompatActivity {
public static SQLiteHelper sqLiteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addedit_student);
sqLiteHelper = new SQLiteHelper(this, "StudentsDB.sqlite", null, 1);
sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS STUDENTS(Id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, surname VARCHAR, image BLOB)");
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edtName.getText().toString().trim().length() == 0) {
Toast.makeText(getApplicationContext(), "Please provide your students name", Toast.LENGTH_SHORT).show();
} else
try {
//THIS IS WHERE I WANT TO CHECK---------
sqLiteHelper.insertData(
edtName.getText().toString().trim(),
edtPrice.getText().toString().trim(),
imageViewToByte(imageView)
);
Toast.makeText(getApplicationContext(), "Added successfully!", Toast.LENGTH_SHORT).show();
finish();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
EDIT ---- 谢谢你的帮助到目前为止。我有2个按钮,一个用于更新,一个用于插入,我面临的问题是如何在不使用ID的情况下知道要更新的行。我有一个由cardview填充的gridview,在cardview中有一个编辑信息的按钮,请看图像。
https://i.stack.imgur.com/aFJTl.png
如果我点击我做的出口标志;
holder.dit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do something
Intent editintent = new Intent(context, AddEditStudent.class);
editintent.putExtra("studentname", finalHolder.txtName.getText().toString());
editintent.putExtra("studentsurname", finalHolder.txtPrice.getText().toString());
context.startActivity(editintent);
}
});
然后在AddEditStudent Class中插入和更新是。
答案 0 :(得分:2)
更新您插入def a_backslash_b = $/a\b/$; def a_slash_b = $/a/b/$; def a_dollar_b = $/a$$b/$;
Query
答案 1 :(得分:0)
一种方法是让name
列上的唯一约束处理重复名称的可能性。然后,插入重复名称的attemtps将在数据库级别失败。
CREATE TABLE IF NOT EXISTS STUDENTS (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
surname VARCHAR,
image BLOB,
CONSTRAINT constraint_name UNIQUE (name)
)
请注意,无法向现有表添加唯一约束。相反,您将不得不创建一个新的。
从documentation for SQLiteStatement#executeInsert()
开始,此方法返回:
插入的最后一行的行ID(如果此插入成功)。否则为-1。
换句话说,对name
列使用唯一约束,此插入数据的调用应返回-1
。您可以更新代码以检查此返回值并采取相应措施(您当前没有检查返回值)。
答案 2 :(得分:0)
当你要求我帮助更新存在的记录时 过程将遵循
首先在课程update
SQLiteHelper
添加此方法
public void updateData(ContentValues contentValues, String whereClause, String[] whereArgs){
SQLiteDatabase database = getReadableDatabase();
database.update(DATABASE_TABLE, contentValues, whereClause, whereArgs);
}
现在应用If
else
声明中的更改
String name = edtName.getText().toString().trim();
String query = "Select * From STUDENTS where name = '"+name+"'";
if(sqLiteHelper.getData(query).getCount()>0){
Toast.makeText(getApplicationContext(), "Already Exist!", Toast.LENGTH_SHORT).show();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("surname", edtPrice.getText().toString().trim());
contentValues.put("image", imageViewToByte(imageView));
String whereClause = "name=?";
String[] whereArgs = new String[] {"'"+name+"'"};
sqLiteHelper.updateData(contentValues, whereClause, whereArgs);
}else{
sqLiteHelper.insertData(
name,
edtPrice.getText().toString().trim(),
imageViewToByte(imageView)
);
}
答案 3 :(得分:-2)
我认为这会对你有帮助。
boolean isAdded(String name) {
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
String query="SELECT * FROM "+TABLE_NAME+" WHERE "+NAME+" = "+name;
Cursor cursor = sqLiteDatabase.rawQuery(query,null);
if(cursor.getCount()>0)
return true;
else
return false;
}