如何检查数据库中的行是否包含特定值

时间:2018-08-27 18:27:57

标签: java android database android-sqlite

我一直在试验 SQLite数据库,我想知道您是否可以搜索通过数据库来查找某个值,并将其包含在if语句中,以说明该值存在于数据库中,然后说数据库已经具有该值,否则将值添加到数据库中?

数据库:

public class DatabaseHandler extends SQLiteOpenHelper
{

private static final int DB_VERSION = 1;
private static final String DB_NAME = "License.db";
private static final String TABLE_LICENSE = "License";
private static final String DB_ID = "_id";
private static final String DB_License = "LicenseNum";


public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DB_NAME, factory, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "Create Table " + TABLE_LICENSE + "(" +
            DB_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
            DB_License + " TEXT " +
            ");";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXIST" + TABLE_LICENSE);
    onCreate(db);
}

public void addLicense(Licenses license) {

    ContentValues values = new ContentValues();
    values.put(DB_License, license.get_License());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_LICENSE, null, values);
    db.close();

}

public String databaseToString() {

    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_LICENSE + " WHERE 1";

    Cursor c = db.rawQuery(query, null);

    c.moveToFirst();

    while (!c.isAfterLast()){
        if (c.getString(c.getColumnIndex("LicenseNum"))!= null){
            dbString += c.getString(c.getColumnIndex("LicenseNum"));
            dbString += "/n";
        }
    }
    db.close();
    return dbString;
}

public Cursor contain(String Num){
    SQLiteDatabase db = getReadableDatabase();
    return db.rawQuery(Num, null);
}

}

代码(类=“许可证”):

public class Licenses
{

private int _id;
private String _License;

public Licenses(String License) {
    this._License = License;
}

public int get_id() {
    return _id;
}

public String get_License() {
    return _License;
}

public void set_id(int _id) {
    this._id = _id;
}

public void set_License(String _License) {
    this._License = _License;
}

}

代码(类=“ License_Plate”):

public class License_Plate extends AppCompatActivity {
public static EditText License;
public static Button Set;
public static   String[] PlateNum = new String[1000];
private TextView LPN3;
private TextView LPN2;
private Button seedb;
private TextView dbtable;
private Button back;
private DatabaseHandler dbHandler;
Context context;
SQLiteDatabase.CursorFactory factory;
String name = "License.db";
int version = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_license__plate);
    License = (EditText) findViewById(R.id.LPN);
    Set = (Button) findViewById(R.id.Set);
    seedb = (Button) findViewById(R.id.seedb);
    dbHandler = new DatabaseHandler(context,name,factory,version);
}

public void Onclickone(View v){
    try {


        String Plate = License.getText().toString().toUpperCase().trim();
        String Extra = Plate + " Has been added to your array";
        String Extra2 = Plate + " is already part of Your license plates";
        String query = "Select * From License where name = '" + Plate + "'";
        if (dbHandler.contain(query).getCount() < 0 && Plate.length() >= 1 
&& Plate.length() <= 7) {
            setContentView(R.layout.add);
            LPN3 = (TextView) findViewById(R.id.Added);
            Licenses Licensess = new 
Licenses(License.getText().toString().toUpperCase().trim());
            dbHandler.addLicense(Licensess);
            printDatabase();

        } else if (dbHandler.contain(query).getCount() > 0 && Plate.length() 
>= 1 && Plate.length() <= 7) {
            setContentView(R.layout.have);
            LPN2 = (TextView) findViewById(R.id.Have);
            LPN2.setText(License.getText() + " is already part of your 
license plates");

        }
    } catch (Exception e){

Toast.makeText(License_Plate.this,e.getMessage(),Toast.LENGTH_LONG).show();
    }
}

public void Onclicktwo(View v){
    setContentView(R.layout.activity_license__plate);
    License = (EditText) findViewById(R.id.LPN);
    Set = (Button) findViewById(R.id.Set);
    seedb = (Button) findViewById(R.id.seedb);
}

public void Onclickthree(View v){
    setContentView(R.layout.activity_license__plate);
    License = (EditText) findViewById(R.id.LPN);
    Set = (Button) findViewById(R.id.Set);
    seedb = (Button) findViewById(R.id.seedb);
}

public void onclick4(View v){
    setContentView(R.layout.database);
    dbtable = (TextView) findViewById(R.id.dbtxt);
    printDatabase();
}

public void onclick5(View v){
    setContentView(R.layout.activity_license__plate);
    License = (EditText) findViewById(R.id.LPN);
    Set = (Button) findViewById(R.id.Set);
    seedb = (Button) findViewById(R.id.seedb);
}

public void printDatabase(){
    String dbString = dbHandler.databaseToString();
    dbtable.setText(dbString);
}

}

error

如果有人可以帮助您,请这样做。非常感谢。

1 个答案:

答案 0 :(得分:0)

您已经有一个SELECT子句,但这是错误的,因此请更改:

String query = "Select * From License where name = '" + Plate + "'";

收件人:

String query = "Select * From License where LicenseNum = '" + Plate + "'";

尽管对列名和表名进行了硬编码不是一个好习惯。

如果该语句不返回任何行,则表中不存在Plate