Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2013
    Posts
    4

    Unanswered: How to import data from text file to mysql database

    I have a text file .txt with the content as:

    Q: What is HTML used for?
    + It is used to create web pages
    - It is used to create documents
    - It is used to create images files

    Q: What is MySQL used for?
    + It is used to create a database
    - It is used to create documents
    - It is used to create images files

    How can I import the content of the file in two tables ("Questions" and "Answers") of the "QCM" MySQL database ?

    Questions:
    ID int
    question varchar(500)
    difficulty int

    Answers:
    ID int
    answer varchar(500)
    state boolean => If "+" is true, if "-" is false
    ID_question int

    Thanks you !
    Last edited by truonghc; 11-14-13 at 11:00.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use either PowerShell or Perl.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  3. #3
    Join Date
    Nov 2013
    Posts
    4
    Quote Originally Posted by Pat Phelan View Post
    I'd use either PowerShell or Perl.

    -PatP
    Can't I solve this problem in SQL ?

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Probably, but why? You can write novels and sonnets in Excel, but I wouldn't recommend doing that.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Nov 2013
    Posts
    4
    Quote Originally Posted by Pat Phelan View Post
    Probably, but why? You can write novels and sonnets in Excel, but I wouldn't recommend doing that.

    -PatP
    OK, can you explain more clearly to me if I use Pert or PowerShell because This is the first time I use them ?

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    The text file that you have is not columnar. The data spans multiple rows, and the sequence is critically important. Because of that sequence dependence, it is safer and simpler to process the data using a tool built to handle that kind of problem.

    Use whatever tool suits your needs, but the code needs to:
    1. read a line from the file
    2. if it is a question, add it to the questions table.
    3. if it is an answer, add both the text, the right/wrong indicator, and a reference to the question to the answers table.
    4. repeat
    Use any language that suits you, Perl or Powershell would simply be my choices so I used them as examples.

    -PatP
    [*]
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  7. #7
    Join Date
    Nov 2013
    Posts
    4
    It's clearly. Thanks you !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •