从raw / textfile.txt读取数据并将其存储到sqlite中,但无法读取下一行

时间:2016-12-21 09:34:21

标签: java android sqlite filereader

我必须从raw / textFile.txt读取文本,然后将数据存储到sqlite中,一切正常,但每当我尝试读取新行或将其插入数据库时​​,它会显示错误D / DatabaseHelper:Error同时尝试向数据库添加mid sem答案(日志,当它无法添加数据时)。 所以我想阅读如下给定数据并将其存储到数据库中,如何读取可用于以相同方式打印的新行数据?以及如何储存它?

数据位于raw / textfile.txt

2-1-Cloud Computing-The advantages of using cloud computing
a)  Data backup and storage of data"
b)  Powerful server capabilities
c)  SaaS ( Software as a service)
d)  Information technology sandboxing capabilities
e)  Increase in productivity
f)  Cost effective & Time saving

2-2-Cloud Computing-The platforms that are used for large scale cloud computing are
a)   Apache Hadoop
b)   MapReduce

这里是读取方法

  public void readAnswers(InputStream inputStream, Context context) {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] contents = line.split("-");
            String marksId = contents[0];
            try {
                ansId = contents[1];
                subject = contents[2];
                answer = contents[3];
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            Log.d("ans",answer);
            CDataSource cDataSource=CDataSource.getInstance(context);
            cDataSource.addAnswers(new MidSemAnswers(marksId, ansId, subject, answer));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

并将值添加到数据库

     public void addAnswers(MidSemAnswers midSemAnswers)
{
    openWritable();
    m_sqLiteDatabase.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        values.put(KEY_MARKS_ID, midSemAnswers.getMarks_id());
        values.put(KEY_SUBJECT, midSemAnswers.getSubject());
        values.put(KEY_QUESTION_ID, midSemAnswers.getAnswer_id());
        values.put(KEY_QUESTION, midSemAnswers.getAnswer());
        // Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
        m_sqLiteDatabase.insertOrThrow(MID_SEM_ANSWERS_TABLE, null, values);
        m_sqLiteDatabase.setTransactionSuccessful();
        Log.d(LOG,"Database: "+values);

    }catch (Exception e)
    {
        Log.d(LOG,"Error while trying to add mid sem answers to database");
    }finally {
        m_sqLiteDatabase.endTransaction();
    }
}

stacktrace

12-21 14:17:10.651 2254-2254/com.example.questionbank D/ans: The platforms that are used for large scale cloud computing are
 12-21 14:17:10.653 2254-2254/com.example.questionbank E/SQLiteLog: (1) no such table: midSemAnsBank
 12-21 14:17:10.653 2254-2254/com.example.questionbank D/DatabaseHelper: Error while trying to add mid sem answers to database

0 个答案:

没有答案