diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 0caf83838fb2d..0c1e5ee1f7ca3 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -173,10 +173,10 @@ def write_frame(frame, name, con, flavor='sqlite', if_exists='fail', **kwargs): replace: If table exists, drop it, recreate it, and insert data. append: If table exists, insert data. Create if does not exist. """ - + if 'append' in kwargs: import warnings - warnings.warn("append is deprecated, use if_exists instead", + warnings.warn("append is deprecated, use if_exists instead", FutureWarning) if kwargs['append']: if_exists='append' @@ -195,7 +195,12 @@ def write_frame(frame, name, con, flavor='sqlite', if_exists='fail', **kwargs): if create is not None: cur = con.cursor() +<<<<<<< HEAD cur.execute(create) +======= + create_table = get_schema(frame, name, flavor) + cur.execute(create_table) +>>>>>>> Restored append keyword with a FutureWarning cur.close() cur = con.cursor() @@ -241,17 +246,28 @@ def table_exists(name, con, flavor): def get_sqltype(pytype, flavor): sqltype = {'mysql': 'VARCHAR (63)', - 'sqlite': 'TEXT'} - - if issubclass(pytype, np.floating): + 'oracle': 'VARCHAR2', + 'sqlite': 'TEXT', + 'postgresql': 'TEXT'} + if issubclass(pytype, np.number): sqltype['mysql'] = 'FLOAT' sqltype['sqlite'] = 'REAL' +<<<<<<< HEAD if issubclass(pytype, np.integer): #TODO: Refine integer size. sqltype['mysql'] = 'BIGINT' sqltype['sqlite'] = 'INTEGER' +======= + sqltype['postgresql'] = 'NUMBER' + if issubclass(pytype, np.integer): + #TODO: Refine integer size. + sqltype['mysql'] = 'BIGINT' + sqltype['oracle'] = 'PLS_INTEGER' + sqltype['sqlite'] = 'INTEGER' + sqltype['postgresql'] = 'INTEGER' +>>>>>>> Restored append keyword with a FutureWarning if issubclass(pytype, np.datetime64) or pytype is datetime: # Caution: np.datetime64 is also a subclass of np.number. sqltype['mysql'] = 'DATETIME' @@ -276,7 +292,10 @@ def get_schema(frame, name, flavor, keys=None): columns = ',\n '.join('[%s] %s' % x for x in column_types) else: columns = ',\n '.join('`%s` %s' % x for x in column_types) +<<<<<<< HEAD +======= +>>>>>>> added test keyword_as_column_names keystr = '' if keys is not None: if isinstance(keys, basestring): diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index b443c55f97b8d..4e4fd6141fc83 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -27,6 +27,40 @@ bool: lambda x: "'%s'" % x, } +def format_query(sql, *args): + """ + + """ + processed_args = [] + for arg in args: + if isinstance(arg, float) and isnull(arg): + arg = None + + formatter = _formatters[type(arg)] + processed_args.append(formatter(arg)) + + return sql % tuple(processed_args) + +def _skip_if_no_MySQLdb(): + try: + import MySQLdb + except ImportError: + raise nose.SkipTest('MySQLdb not installed, skipping') + +from datetime import datetime + +_formatters = { + datetime: lambda dt: "'%s'" % date_format(dt), + str: lambda x: "'%s'" % x, + np.str_: lambda x: "'%s'" % x, + unicode: lambda x: "'%s'" % x, + float: lambda x: "%.8f" % x, + int: lambda x: "%s" % x, + type(None): lambda x: "NULL", + np.float64: lambda x: "%.10f" % x, + bool: lambda x: "'%s'" % x, +} + def format_query(sql, *args): """ @@ -219,19 +253,18 @@ def test_keyword_as_column_names(self): df = DataFrame({'From':np.ones(5)}) sql.write_frame(df, con = self.db, name = 'testkeywords') - class TestMySQL(unittest.TestCase): - + _multiprocess_can_split_ = True def setUp(self): try: - import MySQLdb + import MySQLdb except ImportError: raise nose.SkipTest try: self.db = MySQLdb.connect(read_default_group='pandas') except MySQLdb.Error, e: raise nose.SkipTest( - "Cannot connect to database. " + "Cannot connect to database. " "Create a group of connection parameters under the heading " "[pandas] in your system's mysql default file, " "typically located at ~/.my.cnf or /etc/.my.cnf. ") @@ -262,6 +295,7 @@ def test_write_row_by_row(self): self.db.commit() +>>>>>>> added mysql nosetests and made them optional result = sql.read_frame("select * from test", con=self.db) result.index = frame.index tm.assert_frame_equal(result, frame) @@ -388,7 +422,7 @@ def _check_roundtrip(self, frame): def test_tquery(self): try: - import MySQLdb + import MySQLdb except ImportError: raise nose.SkipTest frame = tm.makeTimeDataFrame() @@ -413,7 +447,7 @@ def test_tquery(self): def test_uquery(self): try: - import MySQLdb + import MySQLdb except ImportError: raise nose.SkipTest frame = tm.makeTimeDataFrame() @@ -441,7 +475,7 @@ def test_keyword_as_column_names(self): ''' _skip_if_no_MySQLdb() df = DataFrame({'From':np.ones(5)}) - sql.write_frame(df, con = self.db, name = 'testkeywords', + sql.write_frame(df, con = self.db, name = 'testkeywords', if_exists='replace', flavor='mysql')