This article describes the Android programming implementation method of adding text content to SQLite tables. Share to everyone for your reference, specific as follows:
First step: Create a table
CREATE TABLE dlion (
_id integer PRIMARY KEY autoincrement,
content TEXT,
QuestionID Integer,
Answerid Integer,
[right] integer
);
Step two: Edit the text content and put it in the Res/raw/test.txt
Open with notepad++ and put the contents of each cell that will be placed in the table in one row
Remove trailing and Blank lines: Press Ctrl+h to select a regular expression--find the target: \s+$ Replace with empty
Remove line Header space: Press Ctrl+h to select Regular Expression--find target: ^\s+ Replace with empty
Step three: Open the database details reference:
"The method of realizing SD card reading database by Android Programming"
Step Fourth: Add data to Table
public void Addcontent () {try {BufferedReader localbufferreader = new BufferedReader (New InputStreamReader (GE
Tresources (). Openrawresource (R.raw.test));
db = OpenDatabase ();
for (int i = length + 1; i++) {//Add problem to database 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 + ")");
Add answer 1 to database 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 + ")"); Add answer 2 to database 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);
Add answer 3 to database 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);
Add answer 4 to database 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 (); }
}
I hope this article will help you with your Android programming.