I have an SQL database on my app.
As the title says, how can I get my item count?
My app gets an input from the user and stores in the database, and displays it.
MainActivity.java
String task = inputField.getText().toString();
Log.d("MainActivity",task);
helper = new TaskDBHelperHome(MainActivity.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.clear();
values.put(TaskContractHome.Columns.TASK, task);
db.insertWithOnConflict(TaskContractHome.TABLE, null, values,
SQLiteDatabase.CONFLICT_IGNORE);
updateUI();
.
TaskDBHelperHome.java
public class TaskDBHelperHome extends SQLiteOpenHelper {
public TaskDBHelperHome(Context context) {
super(context, TaskContractHome.DB_NAME, null, TaskContractHome.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"%s TEXT)", TaskContractHome.TABLE,
TaskContractHome.Columns.TASK);
Log.d("TaskDBHelper","Query to form table: "+sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContractHome.TABLE);
onCreate(sqlDB);
}
}
.
TaskContractHome.java
import android.provider.BaseColumns;
public class TaskContractHome {
public static final String DB_NAME = "home";
public static final int DB_VERSION = 1;
public static final String TABLE = "tasks";
public class Columns {
public static final String TASK = "task";
public static final String _ID = BaseColumns._ID;
}
}
I want to know how I can count items in the database. (I mean programmatically) Thanks.
Aucun commentaire:
Enregistrer un commentaire