Skip to content

Commit 5bd3c89

Browse files
committed
updated new apis for matching column data
1 parent 182cb06 commit 5bd3c89

File tree

3 files changed

+63
-19
lines changed

3 files changed

+63
-19
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ allprojects {
1414
Add the dependency
1515
```
1616
dependencies {
17-
implementation 'com.github.p32929:AndroidEasySQL-Library:1.3.10'
17+
implementation 'com.github.p32929:AndroidEasySQL-Library:1.3.11'
1818
}
1919
```
2020

@@ -150,11 +150,11 @@ if (res != null) {
150150
}
151151
```
152152

153-
### Get/Read one row data by matching a column data:
154-
To get data from a row by matching data with a column, call ```getOneRowData(columnNumber, valueToMatchWithTheColumn)``` or ```getOneRowData(columnName, valueToMatchWithTheColumn)```
153+
### Get/Read/Search one/multiple row data by matching a column data:
154+
To get data from one/multiple rows by matching data with a column, call ```searchInColumn(columnNumber, valueToSearch, limit)``` or ```searchInColumn(columnName, valueToSearch, limit)```
155155
example:
156156
```
157-
Cursor res = easyDB.getOneRowData(1, "data");
157+
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
158158
if (res != null) {
159159
res.moveToFirst(); // Because here's only one row data
160160
String ID = res.getString(0); // Column 0 is the ID column
@@ -166,15 +166,18 @@ if (res != null) {
166166
or
167167

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

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

androideasysql-library/src/main/java/p32929/androideasysql_library/EasyDB.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public Cursor getOneRowData(int rowID) {
120120
}
121121
}
122122

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

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

163+
public Cursor searchInColumn(int columnNumber, String valueToSearch, int limit) {
164+
if (!initedDb || writableDatabase == null) initDatabase();
165+
String allColNames[] = new String[columns.size() + 1];
166+
allColNames[0] = "ID";
167+
for (int i = 0; i < columns.size(); i++) {
168+
allColNames[i + 1] = columns.get(i).columnName;
169+
}
170+
Cursor cursor = writableDatabase.query(TABLE_NAME,
171+
allColNames, allColNames[columnNumber].toString() + "=?",
172+
new String[]{valueToSearch},
173+
null, null, null, limit == -1 ? null : String.valueOf(limit));
174+
175+
if (cursor.getCount() > 0) {
176+
return cursor;
177+
} else {
178+
return null;
179+
}
180+
}
181+
182+
public Cursor searchInColumn(String columnName, String valueToSearch, int limit) {
183+
if (!initedDb || writableDatabase == null) initDatabase();
184+
String allColNames[] = new String[columns.size() + 1];
185+
allColNames[0] = "ID";
186+
for (int i = 0; i < columns.size(); i++) {
187+
allColNames[i + 1] = columns.get(i).columnName;
188+
}
189+
Cursor cursor = writableDatabase.query(TABLE_NAME,
190+
allColNames, " " + columnName + " " + "=?",
191+
new String[]{valueToSearch},
192+
null, null, null, limit == -1 ? null : String.valueOf(limit));
193+
194+
if (cursor.getCount() > 0) {
195+
return cursor;
196+
} else {
197+
return null;
198+
}
199+
}
200+
161201
//
162202
public boolean matchColumns(String columnsToMatch[], String valuesToMatch[]) {
163203
String query = "";

app/src/main/java/p32929/databaseeasier/MainActivity.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {
3838

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

@@ -149,23 +149,24 @@ private void showData() {
149149
String c1 = res.getString(1);
150150
String c2 = res.getString(2);
151151
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";
152-
153-
textViewResult.setText(tres);
154152
}
153+
textViewResult.setText(tres);
155154
}
156155

157156
public void getData1(View view) {
158157
String tres = "";
159-
Cursor res = easyDB.getOneRowData(1, "5");
158+
Cursor res = easyDB.searchInColumn(1, "1", -1);
159+
160160
if (res != null) {
161-
res.moveToFirst();
162-
163-
String row = res.getString(0);
164-
String c1 = res.getString(1);
165-
String c2 = res.getString(2);
166-
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";
161+
while (res.moveToNext()) {
162+
String row = res.getString(0);
163+
String c1 = res.getString(1);
164+
String c2 = res.getString(2);
165+
tres += "Row: " + row + " C1 = " + c1 + " C2 = " + c2 + "\n";
166+
}
167167

168168
textViewResult.setText(tres);
169+
169170
} else {
170171
Toast.makeText(this, "No Data", Toast.LENGTH_SHORT).show();
171172
}

0 commit comments

Comments
 (0)