Hello, I need help with SQLite. When adding a record to the database, I need to check whether such an entry exists. I do it like this:

String dbIf = "SELECT EXISTS(SELECT * FROM " + DBHelper.dbName + " WHERE " + DBHelper.barcodeIt + " = " + barcodeItem.getText().toString() + " LIMIT 1)"; sqlDB.execSQL(dbIf); 

Question: how can i pull out a java rezltat?

  • in SQLlite android there is an update () method that either updates the record if it already exists, or adds a new one if the record with such ID does not already exist, if this is what you need to get in the end - pavlofff

2 answers 2

The response extension from above is code example:

 public static boolean checkIssetNewsLinkInDB(Context context, String link){ // check link to issue news in DB String selection = DatabaseDescription.News.COLUMN_LINK_NEWS + "=?"; String[] selectionArgs = new String[]{link}; F1NewsReaderDatabaseHelper helper = new F1NewsReaderDatabaseHelper(context); Cursor cursor = helper.getWritableDatabase().query(DatabaseDescription.News.TABLE_NAME, null, selection, selectionArgs, null, null, null); if(cursor.getCount() != 0) { cursor.close(); helper.close(); return false; } cursor.close(); helper.close(); return true; } 

    DbHelpera has a query () and rawQuery () method for purely sql lines that return a cursor object, in which the result lies, but before that you should call openReadableDataBase ()

    • Can you throw an example? - sololev
    • I do not have a computer at hand now, you can google it. - AZ