Java 如何在eclipse中从文件填充数据库表?
我正在eclipse中从事小型Android项目。我创建了一个数据库表,但不是使用EditText控件填充列,而是希望使用文件中的数据(txt或xml)填充它。我使用EditText填充了如下表 数据库类:Java 如何在eclipse中从文件填充数据库表?,java,android,xml,sqlite,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,"
@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>
任何帮助…?首先,您必须解析放置在资产文件夹中的xml,然后将这些值放入数据库中。您可以尝试在本文中分析android xml。一个简单的pull解析代码如下所示。这里的
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();
}
如果要解析xml文件,可以使用XmlPullParser。例如,将xml文件(这里是test.xml)放在assets文件夹中。
xml文件是:
读取xml文件的代码是:
1.首先,您可以定义一个类来表示数据,如
公开课记录{
私人字符串工作人员id;
私有字符串staff_name;
私人约会日;
私人int开始时间;
私人int end_时间;
私有字符串注释;
@凌驾
公共布尔等于(对象o){
//TODO自动生成的方法存根
返回super.equals(o);
}
@凌驾
公共字符串toString(){
//TODO自动生成的方法存根
返回super.toString();
}
公共字符串getStaff_id(){
返回工作人员身份证;
}
公共无效设置职员id(字符串职员id){
this.staff\u id=staff\u id;
}
公共字符串getStaff_name(){
返回员工姓名;
}
public void setStaff_name(字符串staff_name){
this.staff\u name=staff\u name;
}
公共字符串getAppointment_day(){
返回预约日;
}
公共约会日(字符串约会日){
this.appointment\u day=约会日;
}
public int getStart_time(){
返回开始时间;
}
公共无效设置开始时间(整数开始时间){
this.start\u time=开始时间;
}
public int getEnd_time(){
返回结束时间;
}
公共无效设置结束时间(int结束时间){
this.end_time=end_time;
}
公共字符串getComment(){
回复评论;
}
公共void setComment(字符串注释){
this.comment=注释;
}
}
2.在要读取文件的位置,定义
私有列表=新的ArrayList();
此列表包含记录,在此测试文件中,有两条记录需要读取
3.读取文件的电源线:
//资产文件夹中的文件
InputStream tinstr=null;
XmlPullParserFactory工厂;
试一试{
factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser=factory.newPullParser();
AssetManager AssetManager=getAssets();
tinstr=assetManager.open(“test.xml”);
setInput(新的InputStreamReader(tinstr));
int eventType=parser.getEventType();
记录=空;
while(eventType!=XmlPullParser.END_文档){
字符串xmlName=parser.getName();
开关(事件类型){
case XmlPullParser.START_标记:
if(xmlName.endsWith(“记录”)){
记录=新记录();
record.setStart_time(Integer.valueOf(parser.getAttributeValue(null,“start_time”));
record.setAppointment_day(parser.getAttributeValue(null,“appointment_day”);
setComment(parser.getAttributeValue(null,“comment”);
record.setEnd_time(Integer.valueOf(parser.getAttributeValue(null,“end_time”));
setStaff_id(parser.getAttributeValue(null,“staff_id”);
record.setStaff_name(parser.getAttributeValue(null,“staff_name”);
}
打破
case XmlPullParser.END_标记:
if(xmlName.endsWith(“记录”)和&record!=null){
列表。添加(记录);
}
打破
违约:
打破
}
eventType=parser.next();
}
}catch(XMLPullParseRexE){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
希望这能有所帮助。您在尝试时遇到了哪些困难?读取文件、提取值、将它们放入数据库……困难的是我不知道从文件中读取的任何方法。我只知道如何从输入(EditText)中提取数据。任何提示。。!!
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();
}