Step 1: Create a table
Create table dlion (
_ Id integer primary key autoincrement,
Content TEXT,
QuestionId INTEGER,
AnswerId INTEGER,
[Right] INTEGER
);
Step 2: edit the text content and place it in res/raw/test.txt
Use Notepad ++ to open and put the content of each cell to be placed in the table in one row.
Remove trailing spaces and blank lines: press CTRL + H to select a regular expression -- search for the target: \ s + $ with null
Remove leading space from the line: press CTRL + H to select the Regular Expression -- search Target: ^ \ s + with null
Step 3: Open the database details reference
Android SD card read database
Step 4: add data to a table
Public void addContent (){
Try {
BufferedReader localBufferReader = new BufferedReader (
New InputStreamReader (getResources (). openRawResource (
R. raw. test )));
Db = openDatabase ();
For (int I = length + 1; I ++ ){
// Add the problem to the database
String questionStr = localBufferReader. readLine ();
If (questionStr = null ){
LocalBufferReader. close ();
Db. close ();
Break;
}
Log. v ("insert", "insert questionContent" + questionStr
+ "Success! ");
Db.exe cSQL ("insert into DLION (questionId, content) values (" + I
+ ", '" + QuestionStr + "')");
// Add answer 1 to the database
String answerStr1 = localBufferReader. readLine ();
If (answerStr1 = null ){
LocalBufferReader. close ();
Db. close ();
Break;
}
Log. v ("insert", "insert answerContent1" + answerStr1
+ "Success! ");
Db.exe cSQL ("insert into DLION (answerId, content) values (" + I
+ ", '" + AnswerStr1 + "')");
// Add answer 2 to the database www.2cto.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 );
// Add Answer 3 to the 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 the 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 ();
}
}
From oldfeel