Make SQLite/MySQL database using text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Apostle
    New Member
    • Dec 2008
    • 28

    Make SQLite/MySQL database using text

    Hi All,
    I have text file and want to make script to insert it in database. The text looks like this (Sample line):

    01O 1 1 Hapo mwanzo Mungu aliziumba mbingu na nchi.

    The text is tab delimited.
    Thanks alot!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here's an example:
    Code:
    import MySQLdb
    db = MySQLdb.connect(passwd="something",db="whatever")
    c = db.cursor()
    c.execute(
        """INSERT INTO tablename (First_Name,Middle_Name,Last_Name,Month,Day,
        Year,Gender,Address,City,Pin,State,Phone)
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
        (fn,mn,ln,month,day,year,gender,add,city,pin,state,phone))
    To get your data into a list:
    Code:
    >>> s = "01O\t1\t1\tHapo\tmwanzo\tMungu\taliziumba\tmbingu\tna\tnchi"
    >>> s.split()
    ['01O', '1', '1', 'Hapo', 'mwanzo', 'Mungu', 'aliziumba', 'mbingu', 'na', 'nchi']
    >>>

    Comment

    • Apostle
      New Member
      • Dec 2008
      • 28

      #3
      thanks alot
      I love the simplicity

      Comment

      Working...