麻烦将XML解析为SQLite数据库

时间:2013-08-10 02:14:24

标签: android sqlite

我正在处理的应用程序成功解析.xml文件中的数据并将其存储到数据库中。但是,在每次启动应用程序时,数据库的大小将增加解析数据存储到的表行数。例如,如果解析的数据需要存储三行,那么每次后续的应用程序启动都会将数据库增加到(按行数排列)6,然后是9,然后是12,依此类推等等。

我希望在每次启动应用程序时防止表的大小增加。我每次尝试重置数据库,但我不确定如何正确地解决这个问题,或者这是否是我应该采用的解决方案。

我的目的是完整地显示数据库:

public class QAView extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qa_view);

        TextView tv = (TextView) findViewById(R.id.tvData);
        QADatabase entries = new QADatabase(this);
        try
        {
            entries.open();
            String data = entries.getAllData();
            tv.setText(data);
            entries.close();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
}

我有解析器代码来处理来自.xml文件的数据:

    XmlResourceParser parser = getResources().getXml(R.xml.qa);

    try
    {
        while(parser.next() != XmlPullParser.END_DOCUMENT)
        {
            if(parser.getEventType() != XmlPullParser.START_TAG)
            {
                continue;
            }

            String name = parser.getName();

            if(name.equals("question"))
            {
                question = readTextQuestion(parser);
            }
            else if(name.equals("options"))
            {
                readTextAnswers(parser);
            }
        }
    }
    catch(XmlPullParserException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

我也有相应的数据库代码:

public class QADatabase
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_QUESTION = "question";
    public static final String KEY_ANSWER1 = "answer1";
    public static final String KEY_ANSWER2 = "answer2";
    public static final String KEY_ANSWER3 = "answer3";
    public static final String KEY_ANSWER4 = "answer4";
    public static final String KEY_ANSWER5 = "answer5";
    public static final String KEY_CORRECT = "correct";

    public static final String DATABASE_NAME = "QuestionAnswerDatabase";
    public static final String DATABASE_TABLE = "QuestionAnswerTable";
    public static final int DATABASE_VERSION = 2;

    private DatabaseHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        private DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase)
        {
            sqLiteDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY, " +
                    KEY_QUESTION + " TEXT NOT NULL, " +
                    KEY_ANSWER1 + " TEXT NOT NULL, " +
                    KEY_ANSWER2 + " TEXT NOT NULL, " +
                    KEY_ANSWER3 + " TEXT NOT NULL, " +
                    KEY_ANSWER4 + " TEXT NOT NULL, " +
                    KEY_ANSWER5 + " TEXT NOT NULL, " +
                    KEY_CORRECT + " TEXT NOT NULL);"
            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2)
        {
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(sqLiteDatabase);
        }
    }

    public QADatabase(Context c)
    {
        ourContext = c;
    }

    public QADatabase open() throws SQLException
    {
        ourHelper = new DatabaseHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        ourHelper.close();
    }

    public void createEntry(String question, String answer1, String answer2, String answer3, String answer4, String answer5, String correct)
    {
        ContentValues cv = new ContentValues();
        cv.put(KEY_QUESTION, question);
        cv.put(KEY_ANSWER1, answer1);
        cv.put(KEY_ANSWER2, answer2);
        cv.put(KEY_ANSWER3, answer3);
        cv.put(KEY_ANSWER4, answer4);
        cv.put(KEY_ANSWER5, answer5);
        cv.put(KEY_CORRECT, correct);
        ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public String getAllData()
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        while(c.moveToNext())
        {
            result = result + c.getString(0) + " " + c.getString(1) + " " + c.getString(2) + " " + c.getString(3) + " " + c.getString(4) + " " + c.getString(5) + " " + c.getString(6) + " " + c.getString(7) + "\n";
        }

        return result;
    }

    public String getQuestion(long l) throws SQLException
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);

        if(c != null)
        {
            // move the cursor to the first row
            c.moveToFirst();

            // look for ID "l" in the "Questions" column
            String question = c.getString(1);

            return question;
        }

        // otherwise, return null
        return null;
    }

    public String getAnswerChoice(long l, int answerChoiceNumber) throws SQLException
    {
        String[] columns = new String[] {KEY_ROWID, KEY_QUESTION, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);

        if(c != null)
        {
            // move the cursor to the first row
            c.moveToFirst();

            // look for ID "l" in the "Answers" column
            String correctAnswer = c.getString(answerChoiceNumber + 2); // need to offset by 2 for the KEY_ROWID and KEY_QUESTION columns

            return correctAnswer;
        }

        // otherwise, return null
        return null;
    }

    public Cursor getAllTitles()
    {
        return ourDatabase.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER5, KEY_CORRECT},
                null, null, null, null, null);
    }

1 个答案:

答案 0 :(得分:0)

您可以为每个问题分配一个固定的主ID(rowID),因此反复执行此操作只会给您重复的主键约束异常,您的表格不会增长。