我在这里看到过很多类似的问题,但是都没有解决我的问题。
我正在制作一个将XML内容放入数据库的应用程序。一切正常,但是当我第二次打开应用程序时,添加了越来越多的值。我试图删除表,删除数据库,甚至删除文件,但这些都不起作用。
现在,我添加了此代码,该应用程序仅将xml的LAST行解析到数据库中,我真的不知道为什么会发生这种情况。

myDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
        myDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_LINK + " TEXT NOT NULL);"
                );


完整代码:

XMLHelper:

package com.example.partedoxml;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
    private String URL_MAIN = "http://he4dless.webege.com/packages.xml";
    String TAG = "XMLHelper";

    Boolean currTag = false;
    String currTagVal = "";
    public PostValue menes = null;
    public ArrayList<PostValue> he4dless = new ArrayList<PostValue>();

    public void get() {
        try{
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);
            InputStream mInputStream = new URL(URL_MAIN).openStream();
            mXmlReader.parse(new InputSource(mInputStream));



        }catch(Exception e){
            Log.e(TAG, "Exeption:"+e.getMessage());


        }




    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if(currTag){
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currTag = false;

        //if(localName.equalsIgnoreCase("id"))
        //packages.setId(currTagVal);

        if(localName.equalsIgnoreCase("name"))
        menes.setName(currTagVal);

        else if(localName.equalsIgnoreCase("link"))
        menes.setLink(currTagVal);

        else if(localName.equalsIgnoreCase("menes"))
            he4dless.add(menes);
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        Log.i(TAG, "TAG:"+localName);

        currTag = true;
        currTagVal = "";

        if(localName.equals("menes"))
            menes = new PostValue();

    }



}


SQLHelper:

package com.example.partedoxml;

import java.io.File;

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

public class SQLHelper {

    public static final String DATABASE_NAME = "he4dless";

    public static final String DATABASE_TABLE = "menes";
    public static final int DATABASE_VERSION = 1;

    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_LINK = "link";
    public static final String TAG_1 = "tag1";
    public static final String TAG_2 = "tag2";
    public static final String TAG_3 = "tag3";

    private DbHelper myHelper;
    private Context myContext;
    private SQLiteDatabase myDatabase;



    private static class DbHelper extends SQLiteOpenHelper {


    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_LINK + " TEXT NOT NULL);"
                //TAG_1 + " TEXT NOT NULL, " +
                //TAG_2 + " TEXT NOT NULL, " +
                //TAG_3 + " TEXT NOT NULL);"
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);

    }





}
    public SQLHelper(Context c){
        myContext = c;
    }
    public SQLHelper open(){
        myHelper = new DbHelper(myContext);
        myDatabase = myHelper.getWritableDatabase();
        myDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
        myDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " +
                KEY_LINK + " TEXT NOT NULL);"
                );







        return this;
    }
    public void close(){
        myHelper.close();
    }
    public long create(String name, String link) {
        ContentValues cv = new ContentValues();


        cv.put(KEY_NAME, name);
        cv.put(KEY_LINK, link);

        return myDatabase.insert(DATABASE_TABLE, null, cv);

    }

}


主要活动:

package com.example.partedoxml;

import android.support.v7.app.ActionBarActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        tv = (TextView)findViewById(R.id.tv);
        new PostAsync().execute();
    }

    class PostAsync extends AsyncTask<Void, Void, Void>{
        ProgressDialog pd;
        XMLHelper helper;

        @Override
        protected void onPreExecute() {
            pd = ProgressDialog.show(MainActivity.this, "Esta porra esta carregando", "Baixando o XML", true, false);

        }

        @Override
        protected Void doInBackground(Void... params) {
            helper = new XMLHelper();
            helper.get();

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            StringBuilder builder = new StringBuilder();
            for(PostValue post : helper.he4dless){
                //builder.append("\nId: "+ post.getId());

                builder.append("\nName: "+ post.getName());

                builder.append("\nSection: "+ post.getLink());
                builder.append("\n");


                String name = post.getName();
                String link = post.getLink();

                SQLHelper entry = new SQLHelper(MainActivity.this);

                entry.open();

                entry.create(name, link);
                entry.close();


            }
            tv.setText(builder.toString());
            pd.dismiss();
        }



    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


如果有人解决了这个问题,我将感到非常高兴

最佳答案

您的open()删除并重新创建表,然后在每个for循环迭代中重新打开数据库。

将架构设置留给帮助程序回调,例如onCreate()。另外,您无需在循环内重新打开数据库。

09-03 22:39