Last active
August 29, 2015 14:04
-
-
Save bmschmidt/9756df2ff78bf1a340f3 to your computer and use it in GitHub Desktop.
Quickly format strings for searchstring in the mysql client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This uses a fake sprintf style construction to handle easily resetting searchstrings without rebuilding the whole database. | |
Anything in a string like this: | |
%(blah blah)s | |
will be broken out in mysql as a literal. | |
By default, this assumes you only need the "catalog" field: things will get strange if you try to use any non-unique fields. | |
""" | |
import re | |
import MySQLdb | |
format="""<img src="http://chroniclingamerica.loc.gov/lccn/%(paper)s/%(FROM_DAYS(publish_day))s)/ed-%(edition)s/seq-1/thumbnail.jpg"</img> <a href="http://chroniclingamerica.loc.gov/lccn/%(paper)s/%(FROM_DAYS(publish_day))s/ed-%(edition)s/seq-1">Read online</a>""" | |
def CreateSearchstring(format): | |
format = MySQLdb.escape_string(format) | |
lit = 'CONCAT("' + re.sub(r"%\((.*?)\)s",r'",\1,"',format) + '")' | |
return "UPDATE catalog SET catalog.searchstring=%s;" %lit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment