当前位置:数据库 > SQLite >>

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.zzzyk.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!");
                Conte

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,