I am using Python 2.7 to extract information from webpages (with BeautifulSoup
), sort that information and insert it into a SQL table (with MySQLdb
). That is quite simple, and I was able to find great reference from other places on SO.
My issue is I need code to search the table and if the information already exists, update it. But if the information does not exist, create a new record.
Example:
Table
+-----------+----------+--------+
| Name | Phone | Date |
+-----------+----------+--------+
| John | 344-7989 | 9/1 |
+-----------+----------+--------+
| Alexander | 198-3333 | 8/16 |
+-----------+----------+--------+
The web-crawler finds new information online and stores it into a list of dictionaries:
[
{
"Name" : "Samantha",
"Phone" : "788-3443",
"Date" : "9/5"
}, {
"Name" : "John",
"Phone" : "222-9009",
"Date" : "9/5"
}
]
Now, one record (Samantha) is completely new, does not exist in the table. But the other record (John), already exist but his information is more recent. What is the code for editing the table with this information and creating:
Final Table
+-----------+----------+--------+
| Name | Phone | Date |
+-----------+----------+--------+
| John | 222-9009 | 9/5 |
+-----------+----------+--------+
| Alexander | 198-3333 | 8/16 |
+-----------+----------+--------+
| Samantha | 788-3443 | 9/5 |
+-----------+----------+--------+