形式データを解析するAndroidデータのsqliteデータベースにデータを挿入します。 http://demo4896782.mockable.io/shopsjson構文解析からsqliteデータベースにデータを挿入
[
{
"shopName": "Hello World.",
"shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు",
"shopAddress": "Bomanahalli",
"previousDues": 0,
"shopID": 1
},
{
"shopName": "Hello World.",
"shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు",
"shopAddress": "Bomanahalli",
"previousDues": 20,
"shopID": 2
},
{
"shopName": "Hello World.",
"shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు",
"shopAddress": "Bomanahalli",
"previousDues": 400,
"shopID": 3
}
]
- 私はこのJSONデータ
のAPiを挿入する必要があり、このテーブルへのデータベース作成
public class DueAmountDataBHelper extends SQLiteOpenHelper {
public DueAmountDataBHelper(Context context) {
super(context, "abc.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_PRODUCT_DUE_AMT =
"create table due_amt_tab(" +
"shopId text primary key, " +
"shopName text NOT NULL, " +
"teluguName text NOT NULL, " +
"place text NOT NULL, " +
"dueAmount text NOT NULL " +
")";
db.execSQL(CREATE_TABLE_PRODUCT_DUE_AMT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public List<DueAmtDBModel> getShopdata() {
List<DueAmtDBModel> data = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from due_amt_tab", null);
StringBuffer stringBuffer = new StringBuffer();
DueAmtDBModel dataModel = null;
while (cursor.moveToNext()) {
dataModel = new DueAmtDBModel();
String shopId, shopName, teluguName, place, dueAmount;
shopId = cursor.getString(cursor.getColumnIndexOrThrow("shopId"));
shopName = cursor.getString(cursor.getColumnIndexOrThrow("shopName"));
teluguName = cursor.getString(cursor.getColumnIndexOrThrow("teluguName"));
place = cursor.getString(cursor.getColumnIndexOrThrow("place"));
dueAmount = cursor.getString(cursor.getColumnIndexOrThrow("dueAmount"));
dataModel.setShopId(shopId);
dataModel.setShopName(shopName);
dataModel.setTeluguName(teluguName);
dataModel.setPlace(place);
dataModel.setDueAmount(dueAmount);
stringBuffer.append(dataModel);
data.add(dataModel);
}
return data;
}
}
-
1)DBHelperClass:
私のコードは次の通りであります
ありがとうございます。
または単にテーブルに文字列として 'json-string'を格納し、モデルクラスを使用して必要に応じて** Gson **ライブラリを使用して直列化または逆直列化することができます。 –
私はそれを行うことができませんでした、サンプルプログラムを見せてください。 –