realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

How to know wether there is a table in my database or not?

Open Catherineliujj opened this issue 6 years ago • 8 comments

I am using Realm datebase. Now I have problem in knowing there is a table in my database?

Catherineliujj avatar Jan 06 '20 07:01 Catherineliujj

Well if you're missing any tables, the Realm migration will run, and you'll need to add it to the schema.

If you didn't increment the schema version number and define a migration, and you didn't set deleteIfMigrationNeeded, then it'll give you an exception that'll tell you that you have a missing table.

Zhuinden avatar Jan 06 '20 07:01 Zhuinden

Well if you're missing any tables, the Realm migration will run, and you'll need to add it to the schema.

If you didn't increment the schema version number and define a migration, and you didn't set deleteIfMigrationNeeded, then it'll give you an exception that'll tell you that you have a missing table. @Zhuinden Thank you. Maybe I didn`t describe my problem well. Then allow me describe its details. I use table A in a component, but when I package this component separately, table A may not exist and is not created, I need to know wether it exsist or not to decide which action I should take.

Catherineliujj avatar Jan 06 '20 08:01 Catherineliujj

Well, if you add the table with a @RealmModule and it's not there, then it will trigger a migration.

Zhuinden avatar Jan 06 '20 08:01 Zhuinden

@Zhuinden I think this has nothing to do with the old and new versions of realm, so it does not need to be migrated

Catherineliujj avatar Jan 06 '20 08:01 Catherineliujj

@Zhuinden This is method of other database , I find it on the Intenet.

select count(*) from all_tables where table_name='TABLENAME';

Catherineliujj avatar Jan 06 '20 08:01 Catherineliujj

Okay, technically you can check if Realm has a table with a given name using

val dynamicRealm = DynamicRealm.getInstance(realmConfiguration)
val schema = dynamicRealm.schema
val hasTable = schema.get("MyTableName") != null
...
dynamicRealm.close()

Zhuinden avatar Jan 06 '20 08:01 Zhuinden

@Zhuinden Thank you very much, I will try it.

Catherineliujj avatar Jan 06 '20 08:01 Catherineliujj

Closed as question seems to have been answered.

rorbech avatar Aug 19 '20 09:08 rorbech