def add_spell_to_db(db_cursor, spell): existing_spell_flag = False parsed_spell = parse_spell(spell) # First, test if spell already exists. # Spit out error and exit if it does. # This may be subject to change for now db_cursor.execute("SELECT id FROM spell WHERE name = ?", (spell['Name'], )) matched_id = db_cursor.fetchone() if matched_id: existing_spell_flag = True print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \ % (colorz.RED, ) print "Found spell:" full_spell_description(db_cursor, matched_id[0]) # If spell doesn't exist, let's put it in. db_cursor.execute("SELECT spell_id FROM alt_spell WHERE name = ?", (spell['Name'], )) matched_id = db_cursor.fetchone() if matched_id: existing_spell_flag = True print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \ % (colorz.RED, ) print "Found spell:%s" % colorz.GREY print " %s" % spell['Name'] db_cursor.execute("SELECT name FROM spell WHERE id = ?", (matched_id[0])) alt_spell_name = db_cursor.fetchone()[0] print "See \"%s\"" % alt_spell_name if existing_spell_flag: print "%sSpell to be imported:%s" % (colorz.RED, colorz.GREY) print "\n".join(spell) return if "alt_spell_name" in parsed_spell: insert_alt_spell_into_db(db_cursor, parsed_spell) else: insert_spell_into_db(db_cursor, parsed_spell)
def add_spell_to_db(db_cursor, spell): existing_spell_flag = False parsed_spell = parse_spell(spell) # First, test if spell already exists. # Spit out error and exit if it does. # This may be subject to change for now db_cursor.execute("SELECT id FROM spell WHERE name = ?", (spell['Name'], )) matched_id = db_cursor.fetchone() if matched_id: existing_spell_flag = True print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \ % (colorz.RED, ) print "Found spell:" full_spell_description(db_cursor, matched_id[0]) # If spell doesn't exist, let's put it in. db_cursor.execute("SELECT spell_id FROM alt_spell WHERE name = ?", (spell['Name'],)) matched_id = db_cursor.fetchone() if matched_id: existing_spell_flag = True print "%sError: Spell already exists: Spell with matching name found. Please resolve in import file." \ % (colorz.RED, ) print "Found spell:%s" % colorz.GREY print " %s" % spell['Name'] db_cursor.execute("SELECT name FROM spell WHERE id = ?", (matched_id[0])) alt_spell_name = db_cursor.fetchone()[0] print "See \"%s\"" % alt_spell_name if existing_spell_flag: print "%sSpell to be imported:%s" % (colorz.RED, colorz.GREY) print "\n".join(spell) return if "alt_spell_name" in parsed_spell: insert_alt_spell_into_db(db_cursor, parsed_spell) else: insert_spell_into_db(db_cursor, parsed_spell)
per = line_count / float(len(all_spells_file)) * 100 stdout.write("%s\rLoading: %d%%%s" % (colorz.YELLOW, per, colorz.ENDC)) stdout.flush() #This may be useless now if re.match('\-+', line): break line = re.sub(" +$", "", line) if not line.strip(): spell_info = parse_spell(spell, alt_spells) if "alt_spell_name" in spell_info: alt_spells.append(spell_info) else: insert_spell_into_db(db_cursor, spell_info) db_conn.commit() del spell[:] #break continue spell.append(line) print "%s COMPLETE%s" % (colorz.YELLOW, colorz.ENDC) # We need to stick these in after the fact. for spell in alt_spells: insert_alt_spell_into_db(db_cursor, spell) db_conn.commit() db_cursor.close() db_conn.close()