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到数据库
                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();
        }
    }

出处:http://www.cnblogs.com/oldfeel/archive/2012/04/20/2459151.html

 

 

posted @ 2012-05-16 10:39  还是你最好  阅读(475)  评论(0编辑  收藏  举报