import MySQLdb as mysql
from faker import Factory

# Open database connection
db = mysql.connect('localhost', 'root', 'rojak', 'rojak_database')

# Create new db cursor
cursor = db.cursor()

sql = '''
INSERT INTO `sentiment`(`name`)
VALUES ('{}');
'''

MAX_SENTIMENT=10
for i in xrange(MAX_SENTIMENT):
    # Generate random data for the sentiment
    sentiment_name = 'sentiment_{}'.format(i + 1)

    # Parse the SQL command
    insert_sql = sql.format(sentiment_name)

    # insert to the database
    try:
        cursor.execute(insert_sql)
        db.commit()
    except mysql.Error as err:
        print("Something went wrong: {}".format(err))
        db.rollback()

# Close the DB connection
db.close()



@@ -47,101 +47,363 @@
 ory%0A
-%0A# Open database connection%0Adb = mysql.connect('localhost', 'root', 'rojak', 'rojak_database'
+import os%0Aimport sys%0A%0AROJAK_DB_HOST = os.getenv('ROJAK_DB_HOST', 'localhost')%0AROJAK_DB_USER = os.getenv('ROJAK_DB_USER', 'root')%0AROJAK_DB_PASS = os.getenv('ROJAK_DB_PASS', 'rojak')%0AROJAK_DB_NAME = os.getenv('ROJAK_DB_NAME', 'rojak_database')%0A%0A# Open database connection%0Adb = mysql.connect(ROJAK_DB_HOST, ROJAK_DB_USER,%0A        ROJAK_DB_PASS, ROJAK_DB_NAME
 )%0A%0A#
@@ -486,16 +486,32 @@
 %60(%60name%60
+, %60candidate_id%60
 )%0AVALUES
@@ -512,16 +512,22 @@
 VALUES (
+'%7B%7D', 
 '%7B%7D');%0A'
@@ -534,153 +534,803 @@
 ''%0A%0A
-MAX_SENTIMENT=10%0Afor i in xrange(MAX_SENTIMENT):%0A    # Generate random data for the sentiment%0A    sentiment_name = 'sentiment_%7B%7D'.format(i + 1)%0A%0A
+# Get list of the candidates%0Acandidates = %5B%5D%0Asql_get_candidates = '''%0ASELECT id,name FROM candidate;%0A'''%0A%0Atry:%0A    cursor.execute(sql_get_candidates)%0A    results = cursor.fetchall()%0A    for row in results:%0A        candidate_id = row%5B0%5D%0A        candidate_name = row%5B1%5D%0A        candidates.append(%7B%0A            'id': candidate_id,%0A            'name': candidate_name%7D)%0Aexcept mysql.Error as err:%0A    print 'Unable to fetch candidate data', err%0A    sys.exit()%0A%0A    # Generate random data for the sentiment%0Afor candidate in candidates:%0A    candidate_id = candidate%5B'id'%5D%0A    candidate_name = candidate%5B'name'%5D%0A    for sentiment in  %5B'pro', 'con', 'net'%5D:%0A        candidate_first_name = candidate_name.split(' ')%5B0%5D.lower()%0A        sentiment_name = '%7B%7D_%7B%7D'.format(sentiment, candidate_first_name)%0A%0A    
     
@@ -1353,16 +1353,20 @@
 command%0A
+    
     inse
@@ -1395,27 +1395,45 @@
 ntiment_name
-)%0A%0A
+, candidate_id)%0A%0A    
     # insert
@@ -1449,16 +1449,20 @@
 atabase%0A
+    
     try:
@@ -1466,24 +1466,28 @@
 ry:%0A        
+   
+ 
 cursor.execu
@@ -1509,16 +1509,20 @@
         
+    
 db.commi
@@ -1525,16 +1525,20 @@
 ommit()%0A
+    
     exce
@@ -1564,24 +1564,28 @@
 rr:%0A        
+    
 print(%22Somet
@@ -1618,16 +1618,20 @@
 t(err))%0A
+    
         

Add 'sentiment' data generator