本文实例讲述了android编程实现添加文本内容到sqlite表中的方法。分享给大家供大家参考,具体如下:
第一步: 创建表
1
2
3
4
5
6
7
|
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+ 替换为空
第三步: 打开数据库 详情参照:
第四步: 添加数据到表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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程序设计有所帮助。