script to port old mysql facts to django models
This commit is contained in:
parent
2dc2b6a8a2
commit
bc2301ce0d
33
scripts/port_facts_to_django.py
Normal file
33
scripts/port_facts_to_django.py
Normal file
@ -0,0 +1,33 @@
|
||||
from ConfigParser import ConfigParser, NoOptionError
|
||||
import os
|
||||
import sys
|
||||
|
||||
from facts.models import FactCategory, Fact
|
||||
|
||||
import MySQLdb as mdb
|
||||
|
||||
config_file = 'dr.botzo.cfg'
|
||||
config = ConfigParser()
|
||||
config.read(os.path.expanduser(config_file))
|
||||
|
||||
dbhost = config.get('dr.botzo', 'dbhost')
|
||||
dbuser = config.get('dr.botzo', 'dbuser')
|
||||
dbpass = config.get('dr.botzo', 'dbpass')
|
||||
dbname = config.get('dr.botzo', 'dbname')
|
||||
|
||||
db = mdb.connect(dbhost, dbuser, dbpass, dbname, charset='utf8', use_unicode=True)
|
||||
try:
|
||||
cur = db.cursor()
|
||||
query = '''
|
||||
SELECT * FROM facts_facts
|
||||
'''
|
||||
cur.execute(query)
|
||||
for row in cur.fetchall():
|
||||
category_name = row[1]
|
||||
category, c = FactCategory.objects.get_or_create(name=category_name)
|
||||
|
||||
fact_text = row[2]
|
||||
fact = Fact.objects.create(category=category, fact=fact_text)
|
||||
except NoOptionError as noe:
|
||||
sys.exit("Aborted due to error in config: {0:s}".format(str(noe)))
|
||||
|
Loading…
Reference in New Issue
Block a user