简体   繁体   中英

Android SQLite Database check if query exists?

Hi i'm still new in android and SQLite. I got activity which that can add query to my attached db file. The problem is, i can't add data using my methods. Is there a simple methods to check if query is exists ?

Here is my DB Access

public class DBAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DBAccess instance;
Cursor c = null;

private DBAccess(Context context)
{
    this.openHelper = new DatabaseOpenHelper(context);
}

public static DBAccess getInstance(Context context)
{
    if(instance==null)
    {
        instance=new DBAccess(context);
    }
    return instance;
}

public void open()
{
    this.db = openHelper.getWritableDatabase();
}

public void close()
{
    if(db!=null)
    {
        this.db.close();
    }
}

public void tambah(String a,String b)
{
    String query= ("insert into TabelAbjad (kata,arti) values('"+a+"','"+b+"')");
    db.execSQL(query);
}

public boolean checkdata(String c, String d)
{
    String s;
    String query= ("select kata from TabelAbjad where kata = '"+c+"' AND kata = '"+d+"'");
    db.execSQL(query);
    return true;
}

Here is when i try to call the methods

private void adddata()
{
    DBAccess dbAccess = DBAccess.getInstance(getApplicationContext());
    dbAccess.open();
    String k = editkata.getText().toString().trim();
    String a = editarti.getText().toString().trim();
    if (dbAccess.checkdata(k,a))
    {
        Toast.makeText(this, "Data already exists",Toast.LENGTH_LONG).show();
        dbAccess.close();
        finish();
    }
    else
    {
        dbAccess.tambah(k,a);
        dbAccess.close();
        Toast.makeText(this, "Data Saved",Toast.LENGTH_LONG).show();
    }

}

PS : I'm call the method in button

You are missing a semicolon (;) in your query string. Try this String query= ("select kata from TabelAbjad where kata = '"+c+"' AND kata = '"+d+"';"); for all your query strings

Your issue is that no row in the table will exist where the column kata has the value c as well as ( AND ) the value d it is impossible as a column can only have a single value, thus no rows would ever be extracted.

Perhaps you want to find rows that have either c OR d

in which case you could use :-

String query= ("select kata from TabelAbjad where kata = '"+c+"' OR kata = '"+d+"'");
  • ie AND has been changed to OR

Furthermore, execSQL cannot return a result, as per :-

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data. execSQL

You thus need to either use the rawQuery method or the convenience query method, the latter recommended unless it's limitations mean that it cannot be used. As such you should try using something along the lines of (OR instead of AND assumed) :-

public boolean checkdata(String c, String d)
    boolean rv = false;
    String whereargs = "kata=? OR kata=?"; // the WHERE clause less the WHERE keyword ? for the arguments (used on a 1 for 1 basis)
    String[] whereargs = new String[]{c,d}; // The arguments that will be substituted into the SQL
    String[] columns = new String[]{"kata"};
    Cursor csr = db.query("TabelAbjad",columns,whereclause,whereargs,null,null,null);
    if (csr.getCount() > 0) {
        rv = true;
    }
    csr.close();
    return rv;
}
  • Although this may appear to be more complex to code it has advantages as it build the underlying SQL, it protects against SQL injection and it correctly encloses/escapes (wraps the values in single quotes etc) the values)arguments
  • You retrieve data (rows) into a Cursor (in this case you simply want to know if any rows matched the given criteria so getCount() is used (it returns the number of rows extracted which could be 0 if none)).

Is there a simple methods to check if query is exists ?

I believe that what you really mean is there anyway to check if the query returned any results.

As said above a query returns data via a Cursor (like a table but according to the query eg the Cursor above will consist of a number of rows (0 or more) with a single column named kata (ie the query is SELECT **kata** FROM ..... ) ) and thus you need to use a suitable method.

You can check/access numerous aspects/properties. Typically you'd move around the Cursor (eg while (your_cursor.moveToNext) {.... do things ....} can be used traverse all rows in the cursor). Cursor .

Once positioned appropriately at a row then you can use the get????(column_offset) to retrieve the data that came from the database (where column_offset is an integer with 0 representing the first column, however it is generally much wiser to retrieve the actual offset using the getColumnIndex(column_name_as_a_string method.))

So assuming that you wanted the data (String) in the first row that was extracted above into the Cursor csr then you could use :-

if (csr.moveToFirst()) {
   String mykata = csr.getString(csr.getColumnIndex("kata"));
}
csr.close(); // You should always close a cursor when done with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM