From 40f35f3ba6044a26a4b844287519f5a3903227ef Mon Sep 17 00:00:00 2001 From: p32929 Date: Sat, 28 Mar 2020 17:36:25 +0600 Subject: [PATCH] updated readme --- README.md | 187 ++++++++++++++++++ .../easiestsqlandroidapp/MainActivity.java | 25 ++- .../java/org/richit/easiestsqllib/Datum.java | 22 +-- .../org/richit/easiestsqllib/EasiestDB.java | 64 +++--- 4 files changed, 253 insertions(+), 45 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9c0575 --- /dev/null +++ b/README.md @@ -0,0 +1,187 @@ +# EasiestSqlLibrary +The Easiest and Laziest approach to Android SQL Database. + +## Installation + + + +## Basic Usage +Steps to follow: +1. Initialize +2. Add Tables and Columns +3. Call `doneAddingTables` method + +After that, you can do all kinds of CRUD ( Create, Read, Update, Delete ) operations. + +## Initialize the Database and the Tables +###### EasiestDB.init(Context context) +###### easiestDB.addTableColumns(String tableName, Column... columns) +###### easiestDB.doneAddingTables() + +``` +EasiestDB easiestDB = EasiestDB.init(this) + .addTableColumns("table 1", + new Column("Column a1", "text"), + new Column("Column a2", "text") + ) + .addTableColumns("table 2", + new Column("Column b1", "text"), + new Column("Column b2", "text", "unique") + ) + .doneAddingTables(); +``` + +* You don't need to add the "ID" primary key column. ```EasiestSqlLibrary``` does that by default. + +## Add data +###### easiestDB.addDataInTable(int tableIndex, Datum... data) +###### Datum(String columnName, String value) +###### Datum(String columnName, int value) +###### Datum(String columnName, double value) +###### Datum(int columnIndex, String value) +###### Datum(int columnIndex, int value) +###### Datum(int columnIndex, double value) + +``` +boolean added = easiestDB.addDataInTable(0, + new Datum(1, "Value1"), + new Datum(2, "Value2") +); +``` + +## Get All data from a table +###### easiestDB.getAllDataFrom(int tableIndex) +###### easiestDB.getAllDataFrom(String tableName) + +``` +Cursor cursor = easiestDB.getAllDataFrom(0); +if (cursor != null) { + while (cursor.moveToNext()) { + int value1 = cursor.getInt(columnIndex); + String value2 = cursor.getString(columnIndex); + double value3 = cursor.getDouble(columnIndex); + } +} +``` +or +###### easiestDB.getAllDataOrderedBy(int columnIndex, boolean ascending, int tableIndex) + +``` +Cursor cursor = easiestDB.getAllDataOrderedBy(0, true, 0); +if (cursor != null) { + while (cursor.moveToNext()) { + int value1 = cursor.getInt(columnIndex); + String value2 = cursor.getString(columnIndex); + double value3 = cursor.getDouble(columnIndex); + } +} +``` + +## Get All data from a row within a table +###### easiestDB.getOneRowData(int tableIndex, int rowNumber) +###### easiestDB.getOneRowData(String tableName, int rowNumber) + +``` +Cursor cursor = easiestDB.getOneRowData(0, 0); +if (cursor != null) { + cursor.moveToFirst(); + int value1 = cursor.getInt(columnIndex); + String value2 = cursor.getString(columnIndex); + double value3 = cursor.getDouble(columnIndex); +} +``` + +## Search value in a column within a table +###### easiestDB.searchInOneColumn(int columnIndex, String valueToSearch, int limit, int tableIndex) +###### easiestDB.searchInOneColumn(String columnName, String valueToSearch, int limit, int tableIndex) + +``` +Cursor cursor = easiestDB.searchInOneColumn(1, "Value1", 0, 0); +if (cursor != null) { + cursor.moveToFirst(); + int value1 = cursor.getInt(columnIndex); + String value2 = cursor.getString(columnIndex); + double value3 = cursor.getDouble(columnIndex); +} +``` + +## Match values in multiple columns. ( Example: Matching ID and password for a user within a table ) +###### easiestDB.searchValuesInMultipleColumns(int tableIndex, Datum... data) + +``` +Cursor cursor = easiestDB.searchValuesInMultipleColumns(1, + new Datum(1, "Value1"), + new Datum(2, "Value2") +); +boolean matched = cursor.getCount() > 0; +``` + +## Update data in a row +###### easiestDB.updateData(int tableIndex, int rowNumber, Datum... data) + +``` +boolean updated = easiestDB.updateData(0, 1, + new Datum(1, "Value1.1"), + new Datum(2, "Value2.2") +); +``` + +## Delete one row +###### easiestDB.deleteRow(int tableIndex, int rowNumber) + +``` +boolean deleted = easiestDB.deleteRow(0, 1); +``` + +## Delete a row if values match in a column +###### easiestDB.deleteRowIfValuesMatchIn(int tableIndex, Datum data) +###### easiestDB.deleteRowIfValuesMatchIn(int tableIndex, Datum data) + +``` +boolean deleted = easiestDB.deleteRowIfValuesMatchIn(0, + new Datum(1, "Value1") +); +``` + +## Delete all data from a table +###### easiestDB.deleteAllDataFrom(String tableName) +###### easiestDB.deleteAllDataFrom(int tableIndex) + +``` +easiestDB.deleteAllDataFrom(0) +``` + +## Delete the all data from the database +###### easiestDB.deleteDatabase() + +``` +boolean deleted = easiestDB.deleteDatabase(); +``` + +I hope, you will enjoy using the library. Feel free to contribute codes. + +#### License + +``` +MIT License + +Copyright (c) 2020 Fayaz Bin Salam + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` \ No newline at end of file diff --git a/app/src/main/java/org/richit/easiestsqlandroidapp/MainActivity.java b/app/src/main/java/org/richit/easiestsqlandroidapp/MainActivity.java index c64423f..70f4983 100644 --- a/app/src/main/java/org/richit/easiestsqlandroidapp/MainActivity.java +++ b/app/src/main/java/org/richit/easiestsqlandroidapp/MainActivity.java @@ -2,8 +2,6 @@ import android.database.Cursor; import android.os.Bundle; -import android.util.Log; -import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -22,6 +20,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + // Init easiestDB = EasiestDB.init(this) .addTableColumns("table 1", new Column("Column a1", "text"), @@ -32,5 +31,27 @@ protected void onCreate(Bundle savedInstanceState) { new Column("Column b2", "text", "unique") ) .doneAddingTables(); + + // Add + boolean added = easiestDB.addDataInTable(0, + new Datum(1, "Value1"), + new Datum(2, "Value2") + ); + + // Get all + Cursor cursor = easiestDB.getAllDataFrom(0); + if (cursor != null) { + while (cursor.moveToNext()) { +// int value1 = cursor.getInt(columnNumber); +// String value2 = cursor.getString(columnNumber); +// double value3 = cursor.getDouble(columnNumber); + + String id = cursor.getString(0); + + } + } + + boolean deleted = easiestDB.deleteDatabase(); + } } diff --git a/easiestsqllib/src/main/java/org/richit/easiestsqllib/Datum.java b/easiestsqllib/src/main/java/org/richit/easiestsqllib/Datum.java index efec298..4cd99b0 100644 --- a/easiestsqllib/src/main/java/org/richit/easiestsqllib/Datum.java +++ b/easiestsqllib/src/main/java/org/richit/easiestsqllib/Datum.java @@ -26,7 +26,7 @@ of this software and associated documentation files (the "Software"), to deal public class Datum { private String columnName = "", value = ""; - private int columnNumber; + private int columnIndex; public Datum(String columnName, String value) { this.columnName = columnName.replace(" ", "_").toUpperCase(); @@ -43,18 +43,18 @@ public Datum(String columnName, double value) { this.value = String.valueOf(value); } - public Datum(int columnNumber, String value) { - this.columnNumber = columnNumber; + public Datum(int columnIndex, String value) { + this.columnIndex = columnIndex; this.value = value; } - public Datum(int columnNumber, int value) { - this.columnNumber = columnNumber; + public Datum(int columnIndex, int value) { + this.columnIndex = columnIndex; this.value = String.valueOf(value); } - public Datum(int columnNumber, double value) { - this.columnNumber = columnNumber; + public Datum(int columnIndex, double value) { + this.columnIndex = columnIndex; this.value = String.valueOf(value); } @@ -74,11 +74,11 @@ public void setValue(String value) { this.value = value; } - public int getColumnNumber() { - return columnNumber; + public int getColumnIndex() { + return columnIndex; } - public void setColumnNumber(int columnNumber) { - this.columnNumber = columnNumber; + public void setColumnIndex(int columnIndex) { + this.columnIndex = columnIndex; } } diff --git a/easiestsqllib/src/main/java/org/richit/easiestsqllib/EasiestDB.java b/easiestsqllib/src/main/java/org/richit/easiestsqllib/EasiestDB.java index 22fb3c3..9025867 100644 --- a/easiestsqllib/src/main/java/org/richit/easiestsqllib/EasiestDB.java +++ b/easiestsqllib/src/main/java/org/richit/easiestsqllib/EasiestDB.java @@ -42,8 +42,8 @@ public class EasiestDB extends SQLiteOpenHelper { private ContentValues contentValues; // Delete more - public void deleteDatabase() { - context.deleteDatabase(DATABASE_NAME); + public boolean deleteDatabase() { + return context.deleteDatabase(DATABASE_NAME); } public void deleteAllDataFrom(String tableName) { @@ -57,9 +57,9 @@ public void deleteAllDataFrom(int tableIndex) { } // Delete data - public boolean deleteRow(int tableIndex, int rowIndex) { + public boolean deleteRow(int tableIndex, int rowNumber) { SQLiteDatabase db = this.getWritableDatabase(); - return db.delete(tableArrayList.get(tableIndex).getTableName(), "id = ?", new String[]{String.valueOf(rowIndex)}) == 1; + return db.delete(tableArrayList.get(tableIndex).getTableName(), "id = ?", new String[]{String.valueOf(rowNumber)}) == 1; } public boolean deleteRowIfValuesMatchIn(int tableIndex, Datum data) { @@ -68,7 +68,7 @@ public boolean deleteRowIfValuesMatchIn(int tableIndex, Datum data) { } // Update data - public boolean updateData(int tableIndex, int rowIndex, Datum... data) { + public boolean updateData(int tableIndex, int rowNumber, Datum... data) { for (int i = 0; i < data.length; i++) { if (data[i].getColumnName().isEmpty()) { contentValues.put(tableArrayList.get(tableIndex).getColumns()[i].getColumnName(), data[i].getValue()); @@ -77,7 +77,7 @@ public boolean updateData(int tableIndex, int rowIndex, Datum... data) { } } try { - return writableDatabase.update(tableArrayList.get(tableIndex).getTableName(), contentValues, "id = ?", new String[]{String.valueOf(rowIndex)}) > 0; + return writableDatabase.update(tableArrayList.get(tableIndex).getTableName(), contentValues, "id = ?", new String[]{String.valueOf(rowNumber)}) > 0; } catch (Exception e) { return false; } @@ -97,7 +97,7 @@ public Cursor searchValuesInMultipleColumns(int tableIndex, Datum... data) { for (int i = 0; i < data.length; i++) { if (data[i].getColumnName().isEmpty()) { - columnsToMatch[i] = tableArrayList.get(tableIndex).getColumns()[data[i].getColumnNumber()].getColumnName(); + columnsToMatch[i] = tableArrayList.get(tableIndex).getColumns()[data[i].getColumnIndex()].getColumnName(); } else { columnsToMatch[i] = data[i].getColumnName(); } @@ -108,14 +108,14 @@ public Cursor searchValuesInMultipleColumns(int tableIndex, Datum... data) { } // Search - public Cursor searchInOneColumn(int columnNumber, String valueToSearch, int limit, int tableNumber) { - String allColNames[] = new String[tableArrayList.get(tableNumber).getColumns().length + 1]; + public Cursor searchInOneColumn(int columnIndex, String valueToSearch, int limit, int tableIndex) { + String allColNames[] = new String[tableArrayList.get(tableIndex).getColumns().length + 1]; allColNames[0] = "ID"; - for (int i = 0; i < tableArrayList.get(tableNumber).getColumns().length; i++) { - allColNames[i + 1] = tableArrayList.get(tableNumber).getColumns()[i].getColumnName(); + for (int i = 0; i < tableArrayList.get(tableIndex).getColumns().length; i++) { + allColNames[i + 1] = tableArrayList.get(tableIndex).getColumns()[i].getColumnName(); } - Cursor cursor = writableDatabase.query(tableArrayList.get(tableNumber).getTableName(), - allColNames, allColNames[columnNumber] + "=?", + Cursor cursor = writableDatabase.query(tableArrayList.get(tableIndex).getTableName(), + allColNames, allColNames[columnIndex] + "=?", new String[]{valueToSearch}, null, null, null, limit == -1 ? null : String.valueOf(limit)); @@ -126,13 +126,13 @@ public Cursor searchInOneColumn(int columnNumber, String valueToSearch, int limi } } - public Cursor searchInOneColumn(String columnName, String valueToSearch, int limit, int tableNumber) { - String allColNames[] = new String[tableArrayList.get(tableNumber).getColumns().length + 1]; + public Cursor searchInOneColumn(String columnName, String valueToSearch, int limit, int tableIndex) { + String allColNames[] = new String[tableArrayList.get(tableIndex).getColumns().length + 1]; allColNames[0] = "ID"; - for (int i = 0; i < tableArrayList.get(tableNumber).getColumns().length; i++) { - allColNames[i + 1] = tableArrayList.get(tableNumber).getColumns()[i].getColumnName(); + for (int i = 0; i < tableArrayList.get(tableIndex).getColumns().length; i++) { + allColNames[i + 1] = tableArrayList.get(tableIndex).getColumns()[i].getColumnName(); } - Cursor cursor = writableDatabase.query(tableArrayList.get(tableNumber).getTableName(), + Cursor cursor = writableDatabase.query(tableArrayList.get(tableIndex).getTableName(), allColNames, " " + columnName + " " + "=?", new String[]{valueToSearch}, null, null, null, limit == -1 ? null : String.valueOf(limit)); @@ -155,9 +155,9 @@ public Cursor getAllDataFrom(String tableName) { return res; } - public Cursor getAllDataOrderedBy(int columnNumber, boolean ascending, int tableIndex) { + public Cursor getAllDataOrderedBy(int columnIndex, boolean ascending, int tableIndex) { String postfix = ascending ? "" : " DESC "; - String colNam = columnNumber == 0 ? " ID " : tableArrayList.get(tableIndex).getColumns()[columnNumber - 1].getColumnName(); + String colNam = columnIndex == 0 ? " ID " : tableArrayList.get(tableIndex).getColumns()[columnIndex - 1].getColumnName(); Cursor res = writableDatabase.rawQuery("select * from " + tableArrayList.get(tableIndex).getTableName() + " ORDER BY " + colNam + postfix, null); return res; } @@ -196,10 +196,10 @@ private String[] getAllColumnsFromTable(String tableName) { } - public Cursor getOneRowData(int tableIndex, int rowIndex) { + public Cursor getOneRowData(int tableIndex, int rowNumber) { Cursor cursor = writableDatabase.query(tableArrayList.get(tableIndex).getTableName(), getAllColumnsFromTable(tableIndex), "ID=?", - new String[]{String.valueOf(rowIndex)}, + new String[]{String.valueOf(rowNumber)}, null, null, null, "1"); if (cursor.getCount() > 0) { @@ -209,10 +209,10 @@ public Cursor getOneRowData(int tableIndex, int rowIndex) { } } - public Cursor getOneRowData(String tableName, int rowID) { + public Cursor getOneRowData(String tableName, int rowNumber) { Cursor cursor = writableDatabase.query(tableName.replace(" ", ""), getAllColumnsFromTable(tableName), "ID=?", - new String[]{String.valueOf(rowID)}, + new String[]{String.valueOf(rowNumber)}, null, null, null, "1"); if (cursor.getCount() > 0) { @@ -227,7 +227,7 @@ public boolean addDataInTable(int tableIndex, Datum... data) { contentValues = new ContentValues(); for (int i = 0; i < data.length; i++) { if (data[i].getColumnName().isEmpty()) { - contentValues.put(getColumnNameFromTableAndColumnNumber(tableIndex, data[i].getColumnNumber()), data[i].getValue()); + contentValues.put(getColumnNameFromTableAndcolumnIndex(tableIndex, data[i].getColumnIndex()), data[i].getValue()); } else { contentValues.put(data[i].getColumnName(), data[i].getValue()); } @@ -243,8 +243,8 @@ public boolean addDataInTable(int tableIndex, Datum... data) { // #2 // - private String getColumnNameFromTableAndColumnNumber(int tableIndex, int columnNumber) { - return tableArrayList.get(tableIndex).getColumns()[columnNumber - 1].getColumnName(); + private String getColumnNameFromTableAndcolumnIndex(int tableIndex, int columnIndex) { + return tableArrayList.get(tableIndex).getColumns()[columnIndex - 1].getColumnName(); } // #3 @@ -356,9 +356,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // #1 -// public Cursor getAllDataOrderedBy(int columnNumber, boolean ascending, String tableName) { +// public Cursor getAllDataOrderedBy(int columnIndex, boolean ascending, String tableName) { // String postfix = ascending ? "" : " DESC "; -// String colNam = columnNumber == 0 ? " ID " : getColumnNameFromTableAndColumnNumber(tableName, columnNumber); +// String colNam = columnIndex == 0 ? " ID " : getColumnNameFromTableAndcolumnIndex(tableName, columnIndex); // Cursor res = writableDatabase.rawQuery("select * from " + tableName + " ORDER BY " + colNam + postfix, null); // return res; // } @@ -368,7 +368,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // contentValues = new ContentValues(); // for (int i = 0; i < data.length; i++) { // if (data[i].getColumnName().isEmpty()) { -// contentValues.put(getColumnNameFromTableAndColumnNumber(tableName, data[i].getColumnNumber()), data[i].getValue()); +// contentValues.put(getColumnNameFromTableAndcolumnIndex(tableName, data[i].getcolumnIndex()), data[i].getValue()); // } else { // contentValues.put(data[i].getColumnName(), data[i].getValue()); // } @@ -382,7 +382,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // } // #3 -// private String getColumnNameFromTableAndColumnNumber(String tableName, int columnNumber) { +// private String getColumnNameFromTableAndcolumnIndex(String tableName, int columnIndex) { // int index = -1; // for (int i = 0; i < tableArrayList.size(); i++) { // if (tableName.toUpperCase().equals(tableArrayList.get(i).getTableName())) { @@ -391,7 +391,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // } // } // -// return tableArrayList.get(index).getColumns()[columnNumber - 1].getColumnName(); +// return tableArrayList.get(index).getColumns()[columnIndex - 1].getColumnName(); // } // #4