2016-09-10 2 views
-1

Ich bin neu in SQLite in Android. Ich habe die Datenbank mit diesem Schema erfolgreich erstellt:So suchen Sie nach einer Zeichenfolge in einer SQLite-Datenbank

public final class BookDbSchema { 

    private BookDbSchema() { 
    } 

    public static final class BookEntry implements BaseColumns{ 
     public static final String NAME = "bookItems"; 

     public static final String ID = "id"; 
     public static final String URL = "url"; 
     public static final String TITLE = "title"; 
    } 
} 

Das Problem, das ich habe, sucht die Datenbank nach einer bestimmten Zeichenfolge.

Ich möchte die ID Spalte suchen, wenn existiert. Bitte wie mache ich das?

Ich habe gelesen the documentation, aber ich weiß nicht, wo Sie "1993" für in der Abfrage-Methode eingeben.

Antwort

1

i belive Sie für diesen Dank

Context context; 
    BookDbSchema mDbHelper; 

    public BookDbSchema (Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
    } 
    ... 

    ArrayList<String> urls = new ArrayList<>(); 
    ArrayList<String> titles = new ArrayList<>(); 
    ArrayList<String> ids= new ArrayList<>(); 

    mDbHelper = new BookDbSchema(context); 
    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

    String[] projection = { 
      FeedEntry.ID, 
      FeedEntry.URL, 
      FeedEntry.TITLE 
    }; 

    String selection = FeedEntry.ID + " LIKE ? "; //WHERE 
    String[] selectionArgs = {"1993"}; //VALUE 

    Cursor c = db.query(
      FeedEntry.NAME, // Your Table Name 
      projection, 
      selection, 
      selectionArgs, 
      null, 
      null, 
      null 
    ); 


    if(c.getCount() != 0) { 
     c.moveToFirst(); 
     for(int i = 0; i<c.getCount(); i++) { 
      urls.add(c.getString(c.getColumnIndex(FeedEntry.URL))); 
      titles.add(c.getString(c.getColumnIndex(FeedEntry.TITLE))); 
      ids.add(c.getString(c.getColumnIndex(FeedEntry.ID))); 
      c.moveToNext(); 
     } 

     String firstUrl = urls.get(0); //Returns the first url found 
     String firstID = ids.get(0); //Returns the first id found 
     int urlSize = urls.size(); // returns the count of urls found 
    }else{ 
     //Nothing found 
    } 


    c.close(); 
    db.close(); 
+0

suchen. Das funktioniert, aber wird es funktionieren, wenn das Ergebnis nicht in der ersten Zeile gefunden wurde? – X09

+0

ja, ich werde den awnser so bearbeiten, wenn Sie mehrere Reihen mit id von 1993 haben, werden Sie in der Lage sein, alle von ihnen –

+0

zu bekommen, ich bearbeite meinen awnser, 'urls' und' titles' werden jetzt jede Reihe mit id 1993 –

1

können Sie die folgenden Methoden:

Cursor query (String table, 
       String[] columns, 
       String selection, 
       String[] selectionArgs, 
       String groupBy, 
       String having, 
       String orderBy, 
       String limit) 

query(NAME, new String[]{ID, URL, TITLE}, ID + "=?", new String[]{"1993"}, null, null, null, null); 
Verwandte Themen