Android programming realizes the method of adding text content to SQLite table _android

Source: Internet
Author: User
Tags readline sqlite

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.

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.