Reputation: 322
Is there an automatic way to generate sqflite create table queries according to json data? I m building Erp apps.So, i have to deal with 40-50 tables each time and i think there should be an automatic way to do it like json to dart.
{
"sto_code": "120.001",
"sto_name": "NORMAL STOK",
"sto_amount": 1
}
Upvotes: 0
Views: 618
Reputation: 656
No package that I know of does that yet. But you could use this code snippet to generate a CREATE TABLE statement with an example json file.
String getType(Type type){
if(type is int){
return 'INTEGER';
}else if(type is String){
return 'TEXT';
}else if(type is double){
return 'REAL';
}else{
return 'TEXT';
}
}
String createColumns(Map<dynamic, dynamic> map){
String columns = '';
map.forEach((key, value){
columns += '$key ${getType(value.runtimeType)}, ';
});
return columns.endsWith(', ') ? columns.substring(0,columns.length - 2) : columns;
}
String createTable(String tableName, Map<dynamic, dynamic> map){
String columns = createColumns(map);
String table = 'CREATE TABLE $tableName ($columns)';
return table;
}
Upvotes: 1