Blog

How to Retrieve Database/Table Schema Using API

by on September 1, 2019

Backendless provides an easy-to-use API to introspect data tables. Given a table name, the API provides information about table columns, their names, data types, default values, etc. If a column represents a relationship, it is properly denoted as such in the provided information.

Consider the following data table. The table name is Person as it is indicated in the upper right corner:

The following code retrieves the schema information for the table:

  • Java
  • Kotlin
  • Objective-C
  • Swift
  • JavaScript
  • Flutter

Backendless.Data.describe("Person", new AsyncCallback<List<ObjectProperty>>() {
   @Override
   public void handleResponse(List<ObjectProperty> schema) {
       for (ObjectProperty property : schema) {
           Log.i(TAG, "Column name - " + property.getName());
           Log.i(TAG, "Data type - " + property.getType());
           Log.i(TAG, "Default value - " + property.getDefaultValue());
           Log.i(TAG, "----------------------------");
       }
   }

   @Override
   public void handleFault(BackendlessFault fault) {
       Log.e(TAG, fault.getMessage());
   }
});

Backendless.Data.describe("Person", object : AsyncCallback<List<ObjectProperty>> {
   override fun handleResponse(schema: List<ObjectProperty>) {
       for (property in schema) {
           Log.i(TAG, "Column name - ${property.name}")
           Log.i(TAG, "Data type - ${property.type}")
           Log.i(TAG, "Default value - ${property.defaultValue}")
           Log.i(TAG, "----------------------------")
       }
   }

   override fun handleFault(fault: BackendlessFault) {
       Log.e(TAG, fault.message)
   }
})

Backendless.shared.data.describe(tableName: "Person", responseHandler: { schema in
    for property in schema {
        print("Column name - \(property.name)")
        print("Data type - \(property.type)")
        print("Default value - \(property.defaultValue ?? "")")
        print("----------------------------")
    }            
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

[Backendless.shared.data describeWithTableName:@"Person" responseHandler:^(NSArray *schema) {
    for (ObjectProperty *property in schema) {
        NSLog(@"Column name - %@", property.name);
        NSLog(@"Column name - %@", [property getTypeName]);
        NSLog(@"Default value - %@", property.defaultValue);
        NSLog(@"----------------------------");
    }
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

const Backendless = require('backendless')
/*
 Or use `import Backendless from 'backendless'` for client side.
 If you don't use npm or yarn to install modules, you can add the following line
 <script src="//api.backendless.com/sdk/js/latest/backendless.min.js"></script>
 to your index.html file and use the global Backendless variable.
*/

Backendless.initApp('YOUR_APP_ID', 'YOUR_JS_API_KEY')

const onSuccess = schema => {
  schema.forEach(property => {
    console.log(`Column name - ${ property.name }`)
    console.log(`Data type - ${ property.type }`)
    console.log(`Default value - ${ property.defaultValue }`)
    console.log('----------------------------')
  })
}

const onError = error => {
  console.error('Server reported an error: ', error.message)
  console.error('error code: ', error.code)
  console.error('http status: ', error.status)
}

Backendless.Data.describe('Person')
  .then(onSuccess)
  .catch(onError)

Backendless.Data.describe("Person").then((schema) {
  schema.forEach((property) {
    print("""Column name - ${property.name}
Data type - ${property.type}
Default value - ${property.defaultValue}
----------------------------""");
  });
});

The code produces the following output:

Column name - created
Data type - DATETIME
Default value - null
----------------------------
Column name - updated
Data type - DATETIME
Default value - null
----------------------------
Column name - birthday
Data type - DATETIME
Default value - null
----------------------------
Column name - name
Data type - STRING
Default value - null
----------------------------
Column name - age
Data type - INT
Default value - null
----------------------------
Column name - ownerId
Data type - STRING
Default value - null
----------------------------
Column name - objectId
Data type - STRING_ID
Default value - null
----------------------------

2 Comments

i am getting null values instead of actual values from the backendless tables

If you experience a problem, please submit a support topic at https://support.backendless.com

Leave a Reply