Android adds text content to the SQLite table to read the database from the android SD card

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 the file and put each row in the cell content of the table (Note: ^ indicates the beginning of the row $ indicates the end of the 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" + question STR + "success! "); Db.exe csql (" 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.exe csql (" 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 ();}}
Related Article

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.