Retrieve Specific Columns from SQLite Database - android

I have an app that gets all the data from the sqlite database and converts it into JSON, but I was wondering how would I be able to do it if I only want to get the data of specific columns?
This is the code I use to get all the data from the SQLite databse and convert it to JSON:
Cursor data = db.getCartItems();
orderNameArray = new JSONArray();
data.moveToFirst();
while(data.isAfterLast() == false)
{
int totalColumn = data.getColumnCount();
orderNameObject = new JSONObject();
for (int i = 0; i < totalColumn; i++)
{
try {
orderNameObject.put(data.getColumnName(i), data.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
orderNameArray.put(orderNameObject);
data.moveToNext();
}
data.close();
Thanks in advance for all the insights or help in advance! :D

If you want to fetch only selected fields of table, then use the following query:
SELECT Coulmn1, Coulmn2, Coulmn3 FROM TABLENAME;
or u want all data
SELECT * FROM TABLENAME;
for more info https://www.tutorialspoint.com/sqlite/sqlite_select_query.htm

Related

Returns zero for all values retrieved from the parse database

I'm using parse backend to store and retrieve the datas for my android app, the storing gets done properly but i have problem in retrieving it. I just went through the parse documentation to retrieve the result but what i get is just 0 for all the retrieved values..im suret that the class exists in the parse cloud with valid values but still i get 0 for all the queries.. this is my code to save:
Toast.makeText(getApplicationContext(),"writing to parse",Toast.LENGTH_SHORT).show();
ParseObject dataObject = new ParseObject("Score");
dataObject.put("correct",correctAnswers);
dataObject.put("wrong",wrongAnswers);
dataObject.put("percent", percentage);
dataObject.saveInBackground();
this is how i get back the saved data
ParseQuery<Score> query = ParseQuery.getQuery("Score");
try {
List<Score> scoreList = query.find();
} catch (ParseException e) {
e.printStackTrace();
}
query = ParseQuery.getQuery("Score");
final Activity ctx = this;
query.findInBackground( new FindCallback<Score>() {
#Override public void done(List<Score> scoreList, ParseException e) {
if ( e == null ) {
ParseObject dataObject = ParseObject.create("Score");
int p = dataObject.getInt("correct");
int q = dataObject.getInt("wrong");
int r = dataObject.getInt("percent");
Toast.makeText(ExamRecordActivity.this,String.valueOf(p),Toast.LENGTH_SHORT).show();
Toast.makeText(ExamRecordActivity.this,String.valueOf(q),Toast.LENGTH_SHORT).show();
Toast.makeText(ExamRecordActivity.this,String.valueOf(r),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ctx,
"Error updating questions - please make sure you have internet connection",
Toast.LENGTH_LONG).show();
}
}
});
Inside the done method you are creating a new by calling ParseObject dataObject = ParseObject.create("Score"); and then trying to read values from it without putting any in.
I don't know what the structure of your class is but you need to be iterating through List<Score> scoreList in order to get the queried data.

Parse get multiple objects using their objectIds

I have a ArrayList<String> -named listObjectId below- of objectIds. I'm trying to get all the objects that have an objectId contained in the ArrayList.
The solution I have right now, I think, is very bad from a performance point of view:
for (int i = 0; i < listObjectId.size(); i++) {
ItemModel mItemModelRetrieved = null;
ParseQuery<ItemModel > query = ParseQuery.getQuery(ItemModel .class);
try {
mItemModelRetrieved = query.get(listObjectId.get(i));
subscriber.onNext(mItemModelRetrieved ); //-- I'm using RxJava
} catch (ParseException e) {
Log.e(TAG, "Error Local " + e.getMessage());
}
}
You're using the wrong method. You have the object ids, so create a ParseObject with them using ParseObject.createWithoutData and then fetch the object. Try the following:
List<ParseObject> parseObjects = new ArrayList<>();
for (String objectId : listObjectId) {
parseObjects.add(ParseObject.createWithoutData(ItemModel.class, objectId));
}
ParseObject.fetchAll(parseObjects);
// parseObjects will now contain all data retrieved from Parse.
The error you're getting tells you that the data type of the column you query must be of type Array, not the value you pass into the method.

How to bulk insert from Json to Sqlite in Android [duplicate]

This question already has answers here:
Android SQLite database: slow insertion
(5 answers)
Closed 8 years ago.
How is it possible to insert bulk json data coming from server into Sqlite database in Android very efficiently. The method I use now is very inefficient and it takes almost a minute to complete insertion of about 2000 records. The method I am following is :
for (int i = 0; i < jsonObj.length(); i++)
{
JSONObject itemObj = (JSONObject) jsonObj.get(i);
ContentValues value1 = new ContentValues();
ContentValues value2 = new ContentValues();
value2.put(DbHelper.BusinessID,itemObj.getString("BusinessID"));
value2.put(DbHelper.LocationID,itemObj.getString("LocationID"));
JSONArray list = itemObj.getJSONArray("OfficeDetails");
if (list != null)
{
for (int k = 0; k < list.length(); k++)
{
JSONObject elem = list.getJSONObject(k);
if (elem != null)
{
try
{
value1.put(DbHelper.Office_Code,elem.getInt("Office_Code"));
value1.put(DbHelper.Office_District,elem.getInt("Office_District"));
db.insert(DbHelper.MessageDetail,null, value1);
}
catch (Exception e)
{
e.printStackTrace();
}
}
db.insert(DbHelper.Message,null, value2);
}
The input that is coming is a nested Json array, which itself is nested. Is there a better way to fastly insert huge amount of data in very short time ?
You could try bulkInsert as the following:
ContentResolver.bulkInsert (Uri url, ContentValues[] values); //Array of rows to be inserted
This only fits if you are going to use only 1 URI, if you are going to use multiple uris, you should use applyBatch method in your ContentResolver.
Hope it helps
First create the model class of your json data. Using Gson you can get data inside an arraylist. then you can insert data into sqlite using that arraylist. GreenDao is your best option for fast performance.
when you receive json data in a stream, use following code :
Type collectionType = new TypeToken<ArrayList<your_model>>() {}.getType();
Gson gson = new Gson();
ArrayList<your_model> yourModelList = gson.fromJson(stream, collectionType);
There is an excellent library called JSQL available in Github.
https://github.com/wenchaojiang/JSQL
Its makes it so easy to save Persist JSON string and JSON Objects to your SQLite on Database.
Create a list of your json data and then used this custom query for bulk data insertion:
/**
* insert the bulk data into database
*
* #param query to insert data into table
* #param parameter data to be inserted into table
* #return number of rows got inserted
*/
protected int insertBulk(String query, String[][] parameter) throws SQLiteConstraintException, SQLiteException {
int rowCount = -1;
SQLiteStatement statement = mSqldb.compileStatement(query);
mSqldb.beginTransaction();
try {
for (int index = 0; index < parameter.length; index++) {
statement.bindAllArgsAsStrings(parameter[index]);
statement.execute();
statement.clearBindings();
}
rowCount = parameter.length;
} finally {
mSqldb.setTransactionSuccessful();
mSqldb.endTransaction();
}
return rowCount;
}

how to ignore null values from the database in comparing values entered by the user in android

I'm trying to save user information in my application. I have Employee number editText and Company EditText. My employee number is not a required field so that means my table can have null values. My problem is when I am comparing if the employee number entered by the user already exist in my database, it also reads the null values when the user do not enter anything on the employee number editText. Can anyone help me do this please?
This is my code where I save all the employee number in an arraylist and remove all the null values
ArrayList<String> employeenumber = databaseAdapter.getEmployeeNumber();
employeenumber.removeAll(Collections.singleton(null));
employeenumber.removeAll(Collections.singleton(""));
What I wanna do is, if the user enter employee number, it will check if the arraylist contains it and if yes, it will all check the company corresponds to it. I need to trap duplicate entry. For example the employee number is 1234 and the company is ABCD, the program must not accept it. Thanks for anyone who could help me.
First check whether the user enter something on employee number editText. Then use int flag to compare it. Try the code below in your main activity:
int flag1 = 0;
if (!Emp.equals(""))
{
ArrayList<String> CompanyandEmp = new ArrayList<String>();
CompanyandEmp = databaseAdapter.getCompanyEmp(Emp);
CompanyandEmp.removeAll(Collections.singleton(""));
for(int i=0;i< CompanyandEmp.size();i++)
{
String cmpemp = CompanyandEmp.get(i).toString();
if(cmpemp.equalsIgnoreCase(companyName))
{
flag1 = 1;
}
}
}
if (flag1 == 0)
{
//as per your requirement
}else{
Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show();
}
Then in your databaseAdapter:
public ArrayList<String> getCompanyEmp(String emp) {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> companyemp = new ArrayList<String>();
try {
Cursor c = db.query("select CompName FROM projsitemast where Emp='"+emp+"'", null);
if (c != null) {
c.moveToFirst();
for (int j = 0; j < c.getCount(); j++) {
companyemp.add(c.getString(0));
c.moveToNext();
}
c.close();
}
return companyemp;
} catch (Exception e) {
e.printStackTrace();
}
return companyemp;
}

Saving ArrayLists in SQLite databases

So I want to save an ordered set of double values, and I want to be able to insert, retrieve or delete any value from this easily. As of such, I'm using a an ArrayList, where I define a class called Doubles to store the double values.
How do I store this arraylist in a record in an SQLite database? I mean...what should the columns type be? Can it be done?
You cannot insert ArrayList directly into Sqlite. Instead, you could use JSONObject (org.json.JSONObject) to insert the ArrayList. Please check below snippet, you can try something like below....
To insert,
JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();
Insert the string into db.
To Read,
Read the string from db as String,
JSONObject json = new JSONObject(stringreadfromsqlite);
ArrayList items = json.optJSONArray("uniqueArrays");
To Insert :
ArrayList<String> inputArray=new ArrayList<String>();
Add Values to inputArray
Gson gson = new Gson();
String inputString= gson.toJson(inputArray);
System.out.println("inputString= " + inputString);
Use "inputString" to save the value of ArrayList<String> in SQLite Database
To retreive:
Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below:
outputarray is a String which is get from SQLiteDatabase for this example.
Type type = new TypeToken<ArrayList<String>>() {}.getType();
ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
In my case it was ArrayList of POJO classes Note
private String mNoteTitle;
private int mFingerIndex;
private Point mNoteCoordinates;
public Note(String noteTitle, int fingerIndex, Point noteCoordinates) {
this.mNoteTitle = noteTitle;
this.mFingerIndex = fingerIndex;
this.mNoteCoordinates = noteCoordinates;
}
As manual says JSONObject supports only following types: Object: a JSONObject, JSONArray, String, Boolean, Integer, Long, Double, NULL, or null. May not be NaNs or infinities. So, I should break my Note class into supported objects.
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray();
for(Note note: chordShape.getNotes()){
JSONObject singleNoteJsonObject = new JSONObject();
try {
singleNoteJsonObject.put(SHAPE_NOTE_TITLE, note.getNoteTitle());
singleNoteJsonObject.put(SHAPE_NOTE_FINGER_INDEX, note.getFingerIndex());
singleNoteJsonObject.put(SHAPE_NOTE_X, note.getNoteCoordinates().x);
singleNoteJsonObject.put(SHAPE_NOTE_Y, note.getNoteCoordinates().y);
} catch (JSONException e){
e.printStackTrace();
}
jsonArray.put(singleNoteJsonObject);
}
Pack created array into JSONObject.
try {
json.put(SHAPE_NOTES, jsonArray);
Log.i(TAG, json.toString());
} catch (JSONException e){
e.printStackTrace();
}
Create String.
String notesList = json.toString();
Put created String in ContentValues, cause in my case it's Android app
if(notesList.length() > 0){
contentValues.put(DatabaseHelper.SHAPE_NOTES_LIST, notesList);
}
And when i should read values from SQLite database.
ArrayList<Note> notes = new ArrayList<>();
while(cursor.moveToNext()){
JSONObject jsonNotes = null;
try {
jsonNotes = new JSONObject(cursor.getString(cursor.getColumnIndex(DatabaseHelper.SHAPE_NOTES_LIST)));
} catch (JSONException e){
e.printStackTrace();
}
if(jsonNotes != null){
Log.i(TAG, jsonNotes.toString());
JSONArray jsonArray = jsonNotes.optJSONArray(SHAPE_NOTES);
for(int i = 0; i < jsonArray.length(); i++){
Note note = null;
JSONObject arrayObject = null;
try {
arrayObject = jsonArray.getJSONObject(i);
} catch (JSONException e){
e.printStackTrace();
}
if(arrayObject != null){
try {
note = new Note(
arrayObject.getString(SHAPE_NOTE_TITLE),
arrayObject.getInt(SHAPE_NOTE_FINGER_INDEX),
new Point(
arrayObject.getInt(SHAPE_NOTE_X),
arrayObject.getInt(SHAPE_NOTE_Y)
)
);
} catch (JSONException e){
e.printStackTrace();
}
}
if(note != null){
notes.add(note);
}
}
}
}
cursor.close();
I suggest going through all 3 Notepad tutorials you want to store the values your storing to a database table. you don't store the actual array directly into the database just the data. but you shouldn't actually need to use an array at all instead of adding a new item to the array instead call your db insert method
I've needed to do something similar in my application, where I have a custom class (Foo, Bar, etc.) and I have an ArrayList of foo, bar, etc. that I persist to SQL. My knowledge of SQL isn't strong, but I'll explain my approach here in case it helps.
My understanding is that to store any kind of object, you need to define a particular table for that object type, where the table has separate columns representing the primitive types within that object. Furthermore, to persist and retrieve an ArrayList of those objects, you'll use one table row per ArrayList entry, and iterate over in a loop to store and retrieve.
There are ArrayLists of several custom classes in my application that I wanted to persist to DB. So, to make things tidy (well, to me at least -- I'm still a relatively new Java / Android programmer, so take this with a pinch of salt) I decided to implement a kind of "SQL Serializable Interface" that my DB-persistable objects must implement. Each object (Foo, Bar, etc.) that can be persisted to DB must implement:
A public static final TABLE_NAME string, the name of the SQL DB table used for this object type.
A public static final TABLE_CREATE_STRING, a complete SQL instruction to create the table for this object.
A constructor method to populate its member variables from a ContentValues object.
A 'get' method to populate a ContentValues from its member variables.
So, say I have ArrayLists of objects Foo and Bar. When the DB is first created, within my DB helper class I call Foo.TABLE_CREATE_STRING, Bar.TABLE_CREATE_STRING, etc. to create the tables for those objects.
To populate my ArrayList, I use something like:
cursor = dbh.retrieve(Foo.TABLE_NAME);
if(!cursor.moveToFirst()){
return false
}
do{
DatabaseUtils.cursorRowToContentValues(cursor, vales);
FooArrayList.add( new Foo(values) );
} while( cursor.moveToNext() );
Create a dbHelper class which has an inner class and pretty much whatever the notepad tutorial says. The class must be having an insertion method somthing like this :-
public long insertRows(ContentValues values, String tableName) {
long val = myDatabase.insert(tableName, null, values);
return val;
}
This method will then add values into the table row.
After that you can call this method from your main activity and since you are using cursor i believe you will call the method in a for loop
for(i=0;list.length();i++) // or may be its list.size :P
{
// Call the method here
}
and keep adding value in the database by calling the method in for loop

Categories

Resources