After having created and set up tables for an experiment they’re ready to have data loaded into them. In this example, I will load ascii files that have already been formatted into a structure that matches the tables to which I’m loading. I’ve again embedded the procedure in a python script, so I can load multiple ascii files (pertaining, say, to multiple experimental conditions and runs) pretty easily.
#!/usr/bin/python
import commands
import os
import sys
import time
import MySQLdb
subjs = ['6']
conditions = ['classaction','classobject','veraction','verobject']
hemis = ['lh','rh']
runs = ['1','2','3','4','5','6']
try:
connection = MySQLdb.connect(read_default_file="~/.my.cnf",db="yourdb" )
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
cursor = connection.cursor()
def insertdata_to_db():
print "que hora es?\n"+time.ctime()
try:
file = "'/disks/gpfs/fmri/results/s0"+ss+"/SUMA/surface.data/s0"+ss+"."+h+"."+cc+"BT.run"+rr+".readyfordb.txt'"
print "File loading: "+file
insert_statement = "load data local infile "+file+" into table vertexdata fields terminated by ' ';"
print "Insert statement: "+insert_statement
cursor.execute(insert_statement)
print "Number of rows inserted: %d" % cursor.rowcount
except MySQLdb.Error, e:
print "Error during s0"+ss+"_"+cc+"_"+h+" run "+rr+" %d -->> %s" % (e.args[0], e.args[1])
sys.exit (1)
for ss in subjs:
for cc in conditions:
for rr in runs:
for h in hemis:
insertdata_to_db()
The script again uses the MySQLdb module. I’ve created a function called insertdata_to_db that uses this sql as an insert statement
load data local infile filename into table vertexdata fields terminated by ' ';
because I’m loading a file from the local fileshare, and the columns in this ascii file are separated by a space, denoted in the sql with fields terminated by ' '. The function also looks for exceptions and boots out if there is an error while loading. I then run this function within nested loops to iterate over subjs, conditions, runs, and hemis.






