android - How to create a new column if not exist in ORMLite while DB Upgrade -
i using ormlite, , going release new version. have db upgrade script(json) this.
{ "version": 9, "ddlqueries": [ { "querydescription": "add new column column name in tablename table", "commandtype": "alter", "table": "table name", "column": { "name": "column name", "type": "blob" } }, { "querydescription": "create new column column name in table name table", "commandtype": "alter", "table": "table name", "column": { "name": "column name", "type": "text" } } ] }
and onupgrade method this.
public void onupgrade(object db, connectionsource connectionsource, int oldversion, int newversion) { try { while(++oldversion<=newversion) { assetmanager assetmanager = context.getassets(); inputstream inputstream = assetmanager.open(migration_script_path + "version-" + oldversion + ".json"); list<string> queries = new querybuilder().loadqueries(new inputstreamreader(inputstream)); inputstream.close(); (string sql : queries) { executesql(db, sql); } postupgrade(db, connectionsource, oldversion); } } catch (ioexception e) { log.e(accountsdbhelper.class.getname(), "exception during onupgrade", e); //below exception must thrown rollback changes. throw new runtimeexception(e); } }
now want check if column exists, if not exists want create new column otherwise want ignore.
how can that.
thanks.
i not sure whether can check existed column @ stage, have workaround problem.
here workaround!
public void onupgrade(object db, connectionsource connectionsource, int oldversion, int newversion) { try { while(++oldversion<=newversion) { if(oldversion == 9) { cursor cursor = rawquery(db,"pragma table_info(table_name)",null); int count = cursor.getcount(); if(count > 0){ boolean iscolumn1available = false; boolean iscolumn2available = false; while (cursor.movetonext()) { string columnname = cursor.getstring(1); if(columnname.equals("column1")) iscolumn1available = true; if(columnname.equals("column2")) iscolumn2available = true; } //here want skip if columns created if(iscolumn1available && iscolumn2available ) return; } } assetmanager assetmanager = context.getassets(); inputstream inputstream = assetmanager.open(migration_script_path + "version-" + oldversion + ".json"); list<string> queries = new querybuilder().loadqueries(new inputstreamreader(inputstream)); inputstream.close(); (string sql : queries) { executesql(db, sql); } } } catch (ioexception e) { log.e(accountsdbhelper.class.getname(), "exception during onupgrade", e); //below exception must thrown rollback changes. throw new runtimeexception(e); } }
thanks.
Comments
Post a Comment