Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store data from website to parse.com And how to to fetch data of it in my application

I want to use the parse database for developing web app, since data will upload from the desktop PC and retreival of the same will be done in parse mobile application.

Is it possible to use the parse database for website backend ?

Since I want to use same parse database for application and desktop version.

Can anyone please help me, by providing some idea how to achieve that?

Thanks in advance.

like image 934
Avijit Avatar asked May 10 '13 06:05

Avijit


1 Answers

Is it possible to use the parse database for website backend ?

YES, it is possible: .NET Guide, REST API

Since I want to use same parse database for application and desktop version.

YES, you can use same database for application and desktop version.


for store data using ANDROID create object like:

ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground();

above code is create table in parse.com(database is automatically created when you store data using object.) check dashboard: like below image

enter image description here

for fetch data try this way:

ParseQuery query = new ParseQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback() {
  public void done(ParseObject object, ParseException e) {
    if (e == null) {
      // object will be your game score
    } else {
      // something went wrong
    }
  }
});

same way you can use for WEB Application as well Desktop application (REST API)


for create table User i have to create object like:

ParseObject gameScore = new ParseObject("User");
    gameScore.put("First Name", "dhaval");
    gameScore.put("Last Name", "Sodha");
    gameScore.put("Email", "[email protected]");
    gameScore.put("Phone", "9876543210");
    gameScore.put("Address", "xyz");
    gameScore.put("City", "ahmedabad");
    gameScore.put("Country", "India");
    gameScore.saveInBackground();

above Object ll create table with fields(ID ,First Name, Last Name, Email, Phone, Address, City, ... time, created...etc )


EDITED:


get data like (SELECT * FROM USER WHERE CITY = 'AHMEDABAD' and COUNTRY = 'INDIA')

           ParseQuery lotsOfWins = new ParseQuery("User");
            lotsOfWins.whereEqualTo("city", "Ahmedabad");

            ParseQuery fewWins = new ParseQuery("User");
            fewWins.whereEqualTo("country", "India");

            List<ParseQuery> queries = new ArrayList<ParseQuery>();
            queries.add(lotsOfWins);
            queries.add(fewWins);

            ParseQuery mainQuery = ParseQuery.or(queries);
            mainQuery.findInBackground(new FindCallback() {
            public void done(List<ParseObject> scoreList, ParseException e) {
                if (e == null) {
                    Log.d("score", "Retrieved " + scoreList.size() + " scores");
                    // HERE SIZE is 0 then 'No Data Found!'
                } else {
                    Log.d("score", "Error: " + e.getMessage());
                }
            }

Here: public void done(List<ParseObject> scoreList, ParseException e) you get result in object

List<ParseObject>: is list of object return by query.

ParseException: is Exception if occur..

So, scoreList.size() give you Total size of all Object return by Query.

How Fetch data:

String username = scoreList.getString("username");

How Save File: (Whatever MIME type like png,jpg,doc,txt....etc)its works for all

Upload File using Below code:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
                USERIMAGE.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                String UsetPhoto = "user"+System.currentTimeMillis()+"image";
                ParseFile file = new ParseFile(UsetPhoto, byteArray);

                ParseObject gameScore = new ParseObject("UserDetails");
                gameScore.put("userName", "" + userName.getText().toString());
                gameScore.put("userPhoto", "" + file);
                gameScore.saveInBackground();

retrive file from parse API using above object:

ParseFile applicantResume = (ParseFile)gameScore.get("userPhoto");
            applicantResume.getDataInBackground(new GetDataCallback() {
              public void done(byte[] data, ParseException e) {
                if (e == null) {
                  // data has the bytes for the resume
                } else {
                  // something went wrong
                }
              }
            });

Here you get: public void done(byte[] data, ParseException e)

byte[] : byte[] of file(whatever file type - if you have uploaded png then save that byte as png).

ParseException : is Exception if occur..

like image 102
Dhaval Parmar Avatar answered Oct 13 '22 02:10

Dhaval Parmar