2013-07-26 59 views
1

我已经搜索解决与其他主题的问题,但我没有找到好的。所以我会解释我想要做什么。 我在网站上恢复JSON文件像这样的:HashMap <String,String>等到SQLiteDataBase

{ 
    "prod": { 
     "ean": "000000000000", 
     "cat": "category ...", 
     "title": "title ...", 
     "brand": "brand ...", 
     "intake": [ 
      { 
       "carbohydrates": "10g" 
      }, 
      { 
       "protein": "8g" 
      }, 
      { 
       "lipids": "0g" 
      } 
     ], 
     "others": "blablabla" 
    } 
} 

我可以分析得很好,并显示在像TextViews不同领域,我用一个ListView来显示“入口”阵列。现在我想将所有这些值放入数据库中。我可以把一切都放在我的数据库中,除了这个数组(我使用HashMap)。知道这个阵列的元素数量可能会有所不同, 有人有一个想法如何进行?

谢谢,我为我的坏英语道歉。 如果你问我,我可以提供更多信息。

有我的数据库处理程序:

public class Base extends SQLiteOpenHelper { 


private static int DATABASE_VERSION = 1; 

private static final String DATABASE_NAME = "etiquettesDatabase"; 

private static final String TABLE_ETIQUETTE = "etiquettes"; 

private static final String KEY_ID = "id"; 
private static final String KEY_TITLE = "title"; 
private static final String KEY_BRAND = "brand"; 
private static final String KEY_INTAKE = "intake"; // i don't know how to proceed here 
private static final String KEY_FAMILY = "family"; 
private static final String KEY_OTHERS = "others"; 

public Base(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = 
      "CREATE TABLE " + TABLE_ETIQUETTE + "(" 
        + KEY_ID + " INTEGER PRIMARY KEY," 
        + KEY_TITLE + " TEXT," 
        + KEY_BRAND + " TEXT," 
        + KEY_FAMILY + " TEXT," 
        + KEY_INTAKE + " TEXT," 
        + KEY_OTHERS + " TEXT" + ")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ETIQUETTE); 
    onCreate(db); 
} 

void addEtiquette(Etiquette etiquette) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_TITLE, etiquette.get_titre()); 
    values.put(KEY_BRAND, etiquette.get_marque()); 
    values.put(KEY_FAMILY, etiquette.get_famille()); 
    //values.put(KEY_INTAKE, etiquette.get_nutrition()); 
    values.put(KEY_OTHERS, etiquette.get_autres()); 


    db.insert(TABLE_ETIQUETTE, null, values); 
    db.close(); 
} 

Etiquette getEtiquette(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = null; 

    if (db != null) { 
     cursor = db.query(TABLE_ETIQUETTE, new String[]{KEY_ID, 
       KEY_TITLE, KEY_BRAND, KEY_INTAKE, KEY_OTHERS}, KEY_ID + "=?", 
       new String[]{String.valueOf(id)}, null, null, null, null); 
    } 
    Etiquette etiquette; 
    /* 
    if (cursor != null && cursor.moveToFirst()) { 
     cursor.moveToFirst(); Log.v("test", "cursor /null"); 
     etiquette = new Etiquette(Integer.parseInt(cursor.getString(0)), 
       cursor.getString(1), 
       cursor.getString(2), 
       cursor.getString(3), 
       cursor.getClass(), 
       cursor.getString(5) 
       ); 
    } else { 
     etiquette=null; Log.v("test", "etiquette = null"); 
    } 
    */ 
    cursor.close(); 
    //Log.v("test", "cursor"+String.valueOf(Integer.parseInt(cursor.getString(0)))); 
/* 
    Etiquette etiquette = new Etiquette(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), 
      cursor.getString(2), 
      cursor.getString(3), 
      cursor.getString(4)); 
*/ 
    // return etiquette; 
    return null; 
} 


public List<Etiquette> getAllEtiquettes() { 
    List<Etiquette> etiquettesList = new ArrayList<Etiquette>(); 

    String selectQuery = "SELECT * FROM " + TABLE_ETIQUETTE; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    if (cursor.moveToFirst()) { 
     do { 
      Etiquette etiquette = new Etiquette(); 
      etiquette.set_id(Integer.parseInt(cursor.getString(0))); 
      etiquette.set_titre(cursor.getString(1)); 
      etiquette.set_famille(cursor.getString(2)); 
      etiquette.set_marque(cursor.getString(3)); 
      //etiquette.set_nutrition(cursor.getString(4)); 
      etiquette.set_autres(cursor.getString(5)); 

      etiquettesList.add(etiquette); 
     } while (cursor.moveToNext()); 
    } 

    return etiquettesList; 
} 


public int updateEtiquette(Etiquette etiquette) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_TITLE, etiquette.get_titre()); 
    values.put(KEY_BRAND, etiquette.get_famille()); 
    values.put(KEY_OTHERS, etiquette.get_autres()); 
// values.put(KEY_INTAKE, etiquette.get_nutrition()); 
    values.put(KEY_OTHERS, etiquette.get_autres()); 

    return db.update(TABLE_ETIQUETTE, values, KEY_ID + " = ?", 
      new String[]{String.valueOf(etiquette.get_id())}); 
} 

public void deleteEtiquette(Etiquette etiquette) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_ETIQUETTE, KEY_ID + " = ?", 
      new String[]{String.valueOf(etiquette.get_id())}); 
    db.close(); 
} 

public int getEtiquettesCount() { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    String countQuery = "SELECT * FROM " + TABLE_ETIQUETTE; 
    assert db != null; 
    Cursor cursor = db.rawQuery(countQuery, null); 
    int n = cursor.getCount(); 
    cursor.close(); 

    return n; 
} 

int getDATABASE_VERSION(){ 
    return this.DATABASE_VERSION; 
} 

void setDATABASE_VERSION(int newDATABASE_VERSION){ 
    this.DATABASE_VERSION = newDATABASE_VERSION; 
} 
} 
+0

什么是你的数据库表结构? –

+0

等一下,我编辑我的问题,告诉你我的DB处理程序 –

+0

有我的数据库结构Renjith –

回答

1

您可以通过两种方式实现它,

第一种方式:
介绍进气新表使用你的id作为外键和存储量和其在该表中的值,这将是使用简单的联合查询即可轻松访问。

方式二:
将所有摄入量和值与预先定义的分隔符一个字符串,并将其存储在当前的领域,当检索使用用于解析他们和显示分隔符,只要你喜欢

String value_delimter = ":"; 
String intake_delimter = "&" 
String intak_value = "intake1"+value_delimter +"value1"+intake_delimter 
"intake2"+value_delimter +"value2"+intake_delimter 
"intake3"+value_delimter +"value3"+intake_delimter 

您可以在使用拆分与分隔符检索数据时访问值。 希望这可以帮助你,
如果这是有用的,请接受并投票了:)

+1

我会尝试第二个,因为Armaan奇怪告诉我: “碳水化合物:10克|蛋白质:8克|脂质:0克” –

+0

它运作良好!谢谢 –

1

创建与阵列像这样的使用环路

“碳水化合物:10克,蛋白质:8G,血脂:0克” 特殊字符分隔的字符串

然后把这整个字符串作为列值,当你回来尝试conmma分离,然后冒号分隔以获得你的价值。

+0

我必须将我所有的HashMap值连接到一个字符串来做到这一点?我不明白你的意思。你能给我一个小例子吗? –

+0

是的,你需要使用字符串生成器或任何其他使用分隔符的字符连接它。并且在所有串联之后将该字符串存储在数据库表的列中。所以它会被保存。 –

+0

当您从该列中检索字符串时,再次使用分隔字符拆分该字符串以获取确切的数据。 –

相关问题