android添加文本內容到sqlite表中

第一步: 創建表
 
CREATE TABLE DLION (

    _id        INTEGER PRIMARY KEY AUTOINCREMENT,

    content    TEXT,

    questionId INTEGER,

    answerId   INTEGER,

    [right]    INTEGER

);
 
第二步: 把編輯文本內容並放到 res/raw/test.txt
  用 Notepad++ 打開,把每一個將要放在表中單元格的內容放一行
    去除行尾空格和空白行:按CTRL+H 選擇正則表達式– 查找目標:\s+$ 替換為空
    去除行首空格:按CTRL+H 選擇正則表達式– 查找目標:^\s+ 替換為空
第三步: 打開數據庫 詳情參照
  android sd卡讀取數據庫
第四步: 添加數據到表
 
public void addContent() {
        try {
            BufferedReader localBufferReader = new BufferedReader(
                    new InputStreamReader(getResources().openRawResource(
                            R.raw.test)));
            db = openDatabase();
            for (int i = length + 1;; i++) {

                // 添加問題到數據庫
                String questionStr = localBufferReader.readLine();
                if (questionStr == null) {
                    localBufferReader.close();
                    db.close();
                    break;
                }
                Log.v("insert", "insert questionContent " + questionStr
                        + " success!");
                db.execSQL("insert into DLION(questionId,content) values(" + i
                        + ",'" + questionStr + "')");

                // 添加答案1到數據庫
                String answerStr1 = localBufferReader.readLine();
                if (answerStr1 == null) {
                    localBufferReader.close();
                    db.close();
                    break;
                }
                Log.v("insert", "insert answerContent1 " + answerStr1
                        + " success!");
                db.execSQL("insert into DLION(answerId,content) values(" + i
                        + ",'" + answerStr1 + "')");

                // 添加答案2到數據庫 www.aiwalls.com
                String answerStr2 = localBufferReader.readLine();
                if (answerStr2 == null) {
                    localBufferReader.close();
                    db.close();
                    break;
                }
                Log.v("insert", "insert answerContent2 " + answerStr2
                        + " success!");
                ContentValues answerContent2 = new ContentValues();
                answerContent2.put("answerId", i);
                answerContent2.put("content", answerStr2);
                db.insert("DLION", "_id", answerContent2);

                // 添加答案3到數據庫
                String answerStr3 = localBufferReader.readLine();
                if (answerStr3 == null) {
                    localBufferReader.close();
                    db.close();
                    break;
                }
                Log.v("insert", "insert answerContent3 " + answerStr3
                        + " success!");
                ContentValues answerContent3 = new ContentValues();
                answerContent3.put("answerId", i);
                answerContent3.put("content", answerStr3);
                db.insert("DLION", "_id", answerContent3);

                // 添加答案4到數據庫
                String answerStr4 = localBufferReader.readLine();
                if (answerStr4 == null) {
                    localBufferReader.close();
                    db.close();
                    break;
                }
                Log.v("insert", "insert answerContent4 " + answerStr4
                        + " success!");
                ContentValues answerContent4 = new ContentValues();
                answerContent4.put("answerId", i);
                answerContent4.put("content", answerStr4);
                db.insert("DLION", "_id", answerContent4);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

摘自 oldfeel

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。