如何在eclipse中从文件填充数据库表?

时间:2014-10-19 07:02:25

标签: java android xml sqlite

我在eclipse中处理小型Android项目。我创建了一个数据库表,但我没有使用EditText控件填充列,而是希望使用文件(txt或xml)中的数据填充它。我有一个使用EditText填充的表格。

数据库类:

@Override
public void onCreate(SQLiteDatabase sqldb) {
    myTableQuery = "CREATE TABLE staff"      +
            "(staff_ID INTEGER PRIMARY KEY," +
            "staff_Name TEXT,"               +
            "appointment_Day TEXT,"          +
            "start_Time TEXT,"               +
            "end_Time TEXT,"                 +
            "comment TEXT)";

    sqldb.execSQL(myTableQuery);
}

public void AddStaff(String id, String name, String day, String start, String end, String comment){

    SQLiteDatabase sqldb=this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("staff_ID", id);
    values.put("staff_Name", name);
    values.put("appointment_Day", day);
    values.put("start_Time", start);
    values.put("end_Time", end);
    values.put("comment", comment);

    sqldb.insert("staff", null, values);
    sqldb.close();


}

AddActivity类:

public void AddRowItemTable(View v){


    EditText staff_id = (EditText) findViewById(R.id.sId);
    EditText staff_name = (EditText) findViewById(R.id.sname);
    EditText day = (EditText) findViewById(R.id.day);
    EditText start = (EditText) findViewById(R.id.start);
    EditText end = (EditText) findViewById(R.id.end);
    EditText comment = (EditText) findViewById(R.id.comment);

    String id = staff_id.getText().toString();
    String name = staff_name.getText().toString();
    String d = day.getText().toString();
    String s = start.getText().toString();
    String e = end.getText().toString();
    String c = comment.getText().toString();

    MainActivity.myDB.AddStaff(id, name, d, s, e, c);
    Intent newintent=new Intent(this, MainActivity.class);
    startActivity(newintent);

}

不是这样做,而是如何从文件中读取数据来填充它。文件看起来像这样。

<?xml version="1.0" encoding="utf-8"?>
<staff>

    <record staff_ID="S1" staff_Name="John" appointment_Day="Monday" start_Time="9" end_Time="12" comment="xxx"/>
    <record staff_ID="S2" staff_Name="Bob" appointment_Day="Monday" start_Time="10" end_Time="11" comment="xxx"/>

</staff>

任何帮助.. ??

2 个答案:

答案 0 :(得分:0)

首先,您必须解析已放置在assets文件夹中的xml,然后将这些值放在db中。您可以尝试在此link中查找解析android xml。一个简单的拉解析代码看起来像这样。这里test.xml是在资产文件夹中保存xml的文件名。

        //File in assets folder
        InputStream tinstr = null;
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();
            AssetManager assetManager = getAssets();
            tinstr = assetManager.open("test.xml");
            parser.setInput(new InputStreamReader(tinstr));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

答案 1 :(得分:0)

If you want to parse your xml file, you can using XmlPullParser. For example, put your xml file (here is test.xml) in the assets folder.
and the xml file is:
<?xml version="1.0" encoding="utf-8"?>
<staff>

    <record staff_ID="S1" staff_Name="John" appointment_Day="Monday" start_Time="9" end_Time="12" comment="xxx"/>
    <record staff_ID="S2" staff_Name="Bob" appointment_Day="Monday" start_Time="10" end_Time="11" comment="xxx"/>

</staff>

And the code to read the xml file is:

1. firstly, you can define a class to present the data, like 
public class Record{
        private String staff_id;
        private String staff_name;
        private String appointment_day;
        private int start_time;
        private int end_time;
        private String comment;
        @Override
        public boolean equals(Object o) {
            // TODO Auto-generated method stub
            return super.equals(o);
        }
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return super.toString();
        }
        public String getStaff_id() {
            return staff_id;
        }
        public void setStaff_id(String staff_id) {
            this.staff_id = staff_id;
        }
        public String getStaff_name() {
            return staff_name;
        }
        public void setStaff_name(String staff_name) {
            this.staff_name = staff_name;
        }
        public String getAppointment_day() {
            return appointment_day;
        }
        public void setAppointment_day(String appointment_day) {
            this.appointment_day = appointment_day;
        }
        public int getStart_time() {
            return start_time;
        }
        public void setStart_time(int start_time) {
            this.start_time = start_time;
        }
        public int getEnd_time() {
            return end_time;
        }
        public void setEnd_time(int end_time) {
            this.end_time = end_time;
        }
        public String getComment() {
            return comment;
        }
        public void setComment(String comment) {
            this.comment = comment;
        }

    }
2. In the place where you want to read the file, define
private List<Record> lists = new ArrayList<Record>();
this lists contains the record, here in this test file, there are two records to read

3. The cord to read the file:

// File in assets folder
        InputStream tinstr = null;
        XmlPullParserFactory factory;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser parser = factory.newPullParser();
            AssetManager assetManager = getAssets();
            tinstr = assetManager.open("test.xml");
            parser.setInput(new InputStreamReader(tinstr));
            int eventType = parser.getEventType();
            Record record = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String xmlName = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if(xmlName.endsWith("record")){
                        record = new Record();
                        record.setStart_time(Integer.valueOf(parser.getAttributeValue(null, "start_Time")));
                        record.setAppointment_day(parser.getAttributeValue(null, "appointment_Day"));
                        record.setComment(parser.getAttributeValue(null, "comment"));
                        record.setEnd_time(Integer.valueOf(parser.getAttributeValue(null, "end_Time")));
                        record.setStaff_id(parser.getAttributeValue(null, "staff_ID"));
                        record.setStaff_name(parser.getAttributeValue(null, "staff_Name"));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if(xmlName.endsWith("record") && record != null){
                        lists.add(record);
                    }
                    break;
                default:
                    break;
                }
                eventType = parser.next();
            }
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

希望这可以提供帮助。