Skip to content

Commit ab0743c

Browse files
committed
Merge pull request #359 from chrisfilo/enh/mysql
Added MySQL interface.
2 parents 620e8b2 + 26d93a5 commit ab0743c

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

CHANGES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Since last release
22
==================
33

4-
* ENH: New interfaces: nipy.Similarity, WatershedBEM
4+
* ENH: New interfaces: MySQLSink, nipy.Similarity, WatershedBEM
55

66
* FIX: Afni outputs should inherit from TraitedSpec
77

nipype/interfaces/io.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ def capture_provenance():
11131113
def push_provenance():
11141114
pass
11151115

1116+
11161117
class SQLiteSinkInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
11171118
database_file = File(exists=True, mandatory = True)
11181119
table_name = traits.Str(mandatory=True)
@@ -1158,3 +1159,61 @@ def _list_outputs(self):
11581159
conn.commit()
11591160
c.close()
11601161
return None
1162+
1163+
1164+
class MySQLSinkInputSpec(DynamicTraitedSpec, BaseInterfaceInputSpec):
1165+
host = traits.Str('localhost', mandatory=True,
1166+
requires=['username', 'password'],
1167+
xor=['config'], usedefault=True)
1168+
config = File(mandatory=True, xor=['host'], desc="MySQL Options File (same format as my.cnf)")
1169+
database_name = traits.Str(mandatory=True, desc='Otherwise known as the schema name')
1170+
table_name = traits.Str(mandatory=True)
1171+
username = traits.Str()
1172+
password = traits.Str()
1173+
1174+
1175+
class MySQLSink(IOBase):
1176+
""" Very simple frontend for storing values into MySQL database.
1177+
1178+
Examples
1179+
--------
1180+
1181+
>>> sql = MySQLSink(input_names=['subject_id', 'some_measurement'])
1182+
>>> sql.inputs.database_name = 'my_database'
1183+
>>> sql.inputs.table_name = 'experiment_results'
1184+
>>> sql.inputs.username = 'root'
1185+
>>> sql.inputs.password = 'secret'
1186+
>>> sql.inputs.subject_id = 's1'
1187+
>>> sql.inputs.some_measurement = 11.4
1188+
>>> sql.run() # doctest: +SKIP
1189+
1190+
"""
1191+
input_spec = MySQLSinkInputSpec
1192+
1193+
def __init__(self, input_names, **inputs):
1194+
1195+
super(MySQLSink, self).__init__(**inputs)
1196+
1197+
self._input_names = filename_to_list(input_names)
1198+
add_traits(self.inputs, [name for name in self._input_names])
1199+
1200+
def _list_outputs(self):
1201+
"""Execute this module.
1202+
"""
1203+
import MySQLdb
1204+
if isdefined(self.inputs.config):
1205+
conn = MySQLdb.connect(db=self.inputs.database_name,
1206+
read_default_file=self.inputs.config)
1207+
else:
1208+
conn = MySQLdb.connect(host=self.inputs.host,
1209+
user=self.inputs.username,
1210+
passwd=self.inputs.password,
1211+
db=self.inputs.database_name)
1212+
c = conn.cursor()
1213+
c.execute("REPLACE INTO %s (" % self.inputs.table_name +
1214+
",".join(self._input_names) + ") VALUES (" +
1215+
",".join(["%s"] * len(self._input_names)) + ")",
1216+
[getattr(self.inputs, name) for name in self._input_names])
1217+
conn.commit()
1218+
c.close()
1219+
return None

0 commit comments

Comments
 (0)