首页 > 系统 > Android > 正文

android编程实现添加文本内容到sqlite表中的方法

2020-04-11 11:17:22
字体:
来源:转载
供稿:网友

本文实例讲述了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();  }}

希望本文所述对大家Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表