Search code examples
pythondatabasesqlitelistcherrypy

With SQLite, how do i read from a list into a database?


I am running a server with cherrypy and python script. Currently, there is a web page containing data of a list of people, which i need to get. The format of the web page is as follow:

www.url1.com, firstName_1, lastName_1
www.url2.com, firstName_2, lastName_2
www.url3.com, firstName_3, lastName_3 

I want to be able to break it up into 3 columns and store the information inside a database.

Originally, I tried reading the webpage into a list with the following method:

@cherrypy.expose
def receiveData(self):
    """ Get a list, one per line, of currently known online addresses,
        separated by commas.
    """

    method = "whoonline"
    fptr = urllib2.urlopen("%s/%s" % (masterServer, method))
    data = fptr.readlines()
    fptr.close()

    print data

where "masterServer" is the IP address of the webpage I am getting the data from.

The data printed is

['www.url1.com,FirstName1,LastName1\n', 'www.url2.com,FirstName2,LastName2\n', 'www.url3.com,FirstName3,LastName3\n' ]

The question I have is, how do i break this list down and store it into a database with 3 columns using Python and SQLite?

I am quite a beginner so please be detailed in your answer. And I'm thinking, maybe I don't even need to read everything into a variable called data first? But am not sure how to proceed.

Thank you in advance!


Solution

  • csv.reader() will let you split the lines into a list of values. Simply insert the values into the database in a query.