Android adds text content to the sqlite table

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.