def export_citydefs(cursor, out_file):
    """
    Export citydefs from database to a sql insert script.
    """
    sql = 'SELECT citydef_uid, city, country, AsText(geom) '
    sql += 'FROM citydefs ORDER BY country, city'
    cursor.execute(sql)
    out_file.write('BEGIN TRANSACTION;\n')
    rows = cursor.fetchall()
    for row in rows:
        line = "INSERT INTO citydefs ('city', 'country', 'geom') VALUES"
        line += '("%s", "%s", GeomFromText(\'%s\', 4326));\n' % (row[1].
            encode('utf-8'), row[2].encode('utf-8'), row[3].encode('utf-8'))
        out_file.write(line)
    out_file.write('COMMIT;\n')
    return len(rows)
