Skip to content

Commit

Permalink
updated new apis for matching column data
Browse files Browse the repository at this point in the history
  • Loading branch information
p32929 committed Apr 17, 2019
1 parent 182cb06 commit 5bd3c89
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allprojects {
Add the dependency
```
dependencies {
implementation 'com.github.p32929:AndroidEasySQL-Library:1.3.10'
implementation 'com.github.p32929:AndroidEasySQL-Library:1.3.11'
}
```

Expand Down Expand Up @@ -150,11 +150,11 @@ if (res != null) {
}
```

### Get/Read one row data by matching a column data:
To get data from a row by matching data with a column, call ```getOneRowData(columnNumber, valueToMatchWithTheColumn)``` or ```getOneRowData(columnName, valueToMatchWithTheColumn)```
### Get/Read/Search one/multiple row data by matching a column data:
To get data from one/multiple rows by matching data with a column, call ```searchInColumn(columnNumber, valueToSearch, limit)``` or ```searchInColumn(columnName, valueToSearch, limit)```
example:
```
Cursor res = easyDB.getOneRowData(1, "data");
Cursor res = easyDB.searchInColumn(1, "data", 1); // Here we passed limit = 1. Thus it will return only one row data with the matched column value
if (res != null) {
res.moveToFirst(); // Because here's only one row data
String ID = res.getString(0); // Column 0 is the ID column
Expand All @@ -166,15 +166,18 @@ if (res != null) {
or

```
Cursor res = easyDB.getOneRowData("ID", "data");
Cursor res = easyDB.searchInColumn("ID", "data", -1); // Here we passed limit = -1. Thus it will return all the rows with the matched column values
if (res != null) {
res.moveToFirst(); // Because here's only one row data
String ID = res.getString(0); // Column 0 is the ID column
String c1 = res.getString(1);
String c2 = res.getString(2);
while (res.moveToNext()) {
String ID = res.getString(0); // Column 0 is the ID column
String c1 = res.getString(1);
String c2 = res.getString(2);
}
}
```

> Please DO NOT pass ```limit = 0``` as the parameter
### Match data from multiple columns:
To check if some values exist or not in the database, first call ```getAllColumns()``` to get all the column names like this:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public Cursor getOneRowData(int rowID) {
}
}

@Deprecated
public Cursor getOneRowData(int columnNumber, String value) {
if (!initedDb || writableDatabase == null) initDatabase();
String allColNames[] = new String[columns.size() + 1];
Expand All @@ -139,6 +140,7 @@ public Cursor getOneRowData(int columnNumber, String value) {
}
}

@Deprecated
public Cursor getOneRowData(String columnName, String value) {
if (!initedDb || writableDatabase == null) initDatabase();
String allColNames[] = new String[columns.size() + 1];
Expand All @@ -158,6 +160,44 @@ public Cursor getOneRowData(String columnName, String value) {
}
}

public Cursor searchInColumn(int columnNumber, String valueToSearch, int limit) {
if (!initedDb || writableDatabase == null) initDatabase();
String allColNames[] = new String[columns.size() + 1];
allColNames[0] = "ID";
for (int i = 0; i < columns.size(); i++) {
allColNames[i + 1] = columns.get(i).columnName;
}
Cursor cursor = writableDatabase.query(TABLE_NAME,
allColNames, allColNames[columnNumber].toString() + "=?",
new String[]{valueToSearch},
null, null, null, limit == -1 ? null : String.valueOf(limit));

if (cursor.getCount() > 0) {
return cursor;
} else {
return null;
}
}

public Cursor searchInColumn(String columnName, String valueToSearch, int limit) {
if (!initedDb || writableDatabase == null) initDatabase();
String allColNames[] = new String[columns.size() + 1];
allColNames[0] = "ID";
for (int i = 0; i < columns.size(); i++) {
allColNames[i + 1] = columns.get(i).columnName;
}
Cursor cursor = writableDatabase.query(TABLE_NAME,
allColNames, " " + columnName + " " + "=?",
new String[]{valueToSearch},
null, null, null, limit == -1 ? null : String.valueOf(limit));

if (cursor.getCount() > 0) {
return cursor;
} else {
return null;
}
}

//
public boolean matchColumns(String columnsToMatch[], String valuesToMatch[]) {
String query = "";
Expand Down
21 changes: 11 additions & 10 deletions app/src/main/java/p32929/databaseeasier/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {

easyDB = EasyDB.init(this, "TEST") // TEST is the name of the DATABASE
.setTableName("DEMO TABLE") // You can ignore this line if you want
.addColumn(new Column("C1","text", "unique"))
.addColumn(new Column("C1", "text"))
.addColumn(new Column("C2", "text", "unique"))
.doneTableColumn();

Expand Down Expand Up @@ -149,23 +149,24 @@ private void showData() {
String c1 = res.getString(1);
String c2 = res.getString(2);
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";

textViewResult.setText(tres);
}
textViewResult.setText(tres);
}

public void getData1(View view) {
String tres = "";
Cursor res = easyDB.getOneRowData(1, "5");
Cursor res = easyDB.searchInColumn(1, "1", -1);

if (res != null) {
res.moveToFirst();

String row = res.getString(0);
String c1 = res.getString(1);
String c2 = res.getString(2);
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";
while (res.moveToNext()) {
String row = res.getString(0);
String c1 = res.getString(1);
String c2 = res.getString(2);
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";
}

textViewResult.setText(tres);

} else {
Toast.makeText(this, "No Data", Toast.LENGTH_SHORT).show();
}
Expand Down

0 comments on commit 5bd3c89

Please sign in to comment.