1

我正在学习制作android应用程序。我的应用程序的目标是将来自 URL 的 XML 文件的解析数据存储到 SQLite 数据库中。我已经成功完成了解析过程,但我想将数据从 XML 存储到 SQLite,这样我的应用程序就可以在 Internet 访问中断时打开 XML 数据。这是我项目中的代码

MainActivity.java

public class MainActivity extends ListActivity {
static final String URL = "http://data.bmkg.go.id/propinsi_15_2.xml";
static final String KEY_ITEM = "Cuaca";
static final String KEY_ID = "Isi";
static final String KEY_ROW = "Row";
static final String KEY_KOTA = "Kota";
static final String KEY_LINTANG = "Lintang";
static final String KEY_BUJUR = "Bujur";
static final String KEY_CUACA = "Cuaca";
static final String KEY_SUHUMIN = "SuhuMin";
static final String KEY_SUHUMAX = "SuhuMax";
static final String KEY_KELEMBAPANMIN = "KelembapanMin";
static final String KEY_KELEMBAPANMAX = "KelembapanMax";
static final String KEY_KECEPATANANGIN = "KecepatanAngin";
static final String KEY_ARAHANGIN = "ArahAngin";
private ProgressDialog pDialog;

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String,String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    new AmbilData().execute();
}

class AmbilData extends AsyncTask<String, String, String>{
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading, Mohon Menunggu Beberapa saat...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground (String... args){
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);

        NodeList nl = doc.getElementsByTagName(KEY_ROW);
        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();

            Element e = (Element) nl.item(i);

            map.put(KEY_KOTA, parser.getValue(e, KEY_KOTA));
            map.put(KEY_LINTANG, parser.getValue(e, KEY_LINTANG));
            map.put(KEY_BUJUR, parser.getValue(e, KEY_BUJUR));
            map.put(KEY_CUACA, parser.getValue(e, KEY_CUACA));
            map.put(KEY_SUHUMIN, parser.getValue(e, KEY_SUHUMIN));
            map.put(KEY_SUHUMAX, parser.getValue(e, KEY_SUHUMAX));
            map.put(KEY_KELEMBAPANMIN, parser.getValue(e, KEY_KELEMBAPANMIN));
            map.put(KEY_KELEMBAPANMAX, parser.getValue(e, KEY_KELEMBAPANMAX));
            map.put(KEY_KECEPATANANGIN, parser.getValue(e, KEY_KECEPATANANGIN));
            map.put(KEY_ARAHANGIN, parser.getValue(e, KEY_ARAHANGIN));

            menuItems.add(map);
        }
        return null;
    }
    protected void onPostExecute(String file_url){
        pDialog.dismiss();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, menuItems, R.layout.list_kota, new String[] {KEY_KOTA, KEY_CUACA, KEY_LINTANG, KEY_BUJUR, KEY_SUHUMIN, KEY_SUHUMAX, KEY_KELEMBAPANMIN, KEY_KELEMBAPANMAX, KEY_KECEPATANANGIN, KEY_ARAHANGIN}, new int[] {R.id.kota, R.id.cuaca, R.id.lintang, R.id.bujur, R.id.suhumin, R.id.suhumax, R.id.kelembapanmin, R.id.kelembapanmax, R.id.kecepatanangin, R.id.arahangin});
                setListAdapter(adapter);
            }
        });
    }
}
}

我一直在搜索并从互联网上找到了许多教程,但我不知道在我的应用程序中使用哪种最佳方法。

4

2 回答 2

1

首先,您必须创建 SQLite 管理员 adminSQLite.java:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class adminSQLite extends SQLiteOpenHelper {

public adminSQLite(Context context, String name, CursorFactory factory,
        int version) {
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {//if DB doesn't exists create it

    db.execSQL("create table your_table(column1 text,column2 text, column3 text,column4 text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {//if version is newer, upgrade the DB
    db.execSQL("drop table if exists your_table");//delete the old table
      db.execSQL("create table your_table(column1 text,column2 text, column3 text,column4 text, column5 text)");//create the new table

}

}

在此之后,您应该在代码中调用此类

int versionDB=1;
adminSQLite admin = new adminSQLite(this, "DBname", null,
                versionDB);
        SQLiteDatabase bd = admin.getWritableDatabase();//Open DB in write mode

并插入数据:

bd.execSQL("insert into your_table values('"+XMLValue1+"','"+XMLValue2+"','"+XMLValue3+"','"+XMLValue4+"')");

您也可以使用 ContentValues 插入:

 ContentValues registers = new ContentValues();//be sure that you're getting your XML data in Strings var
        registers.put("column1", XMLValue1);
        registers.put("column2", XMLValue2);
        registers.put("column3", XMLValue3);
        registers.put("column4", XMLValue4);
        bd.insert("your_table", null, registers);
        bd.close();

如果您想在插入后获取数据:

Cursor data= bd.rawQuery("Select * from your_table",null);
if (data.moveToFirst()){
   do{
      String data1=data.getString(0);//column index
      String data2=data.getString(1);
      String data3=data.getString(2);
      String data4=data.getString(3);
    }while (data.moveToNext());
}
else{
//no data 
}
于 2013-05-10T16:32:19.860 回答
0

我建议您首先“思考”如何构建您的 sql 表。可能哈希图中的每个 KEY 都应该是列。

看到这个关于 sqlite的好教程。

于 2013-05-10T16:18:44.280 回答