Skip to content

BUG: in io.sql.write_frame (replace) #4110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jphme opened this issue Jul 3, 2013 · 1 comment · Fixed by #6164
Closed

BUG: in io.sql.write_frame (replace) #4110

jphme opened this issue Jul 3, 2013 · 1 comment · Fixed by #6164
Labels
Blocker Blocking issue or pull request for an upcoming release Bug IO Data IO issues that don't fit into a more specific label IO SQL to_sql, read_sql, read_sql_query
Milestone

Comments

@jphme
Copy link

jphme commented Jul 3, 2013

From http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.sql.write_frame.html :

Parameters: (...)
if_exists: {‘fail’, ‘replace’, ‘append’}, default ‘fail’ :
fail: If table exists, do nothing. replace: If table exists, drop it, recreate it, 
and insert data. append: If table exists, insert data. Create if does not exist.

Everything works fine as long as the table does not exist.
If table already exists :

sql.write_frame(sp5002, name="sp500", con=conn, if_exists='replace')

-> "sqlite3.OperationalError: no such table: sp500"

Reason is that there is no new "create" statement after the drop statement:

    create = None
    if exists and if_exists == 'replace':
        create = "DROP TABLE %s" % name
    elif not exists:
        create = get_schema(frame, name, flavor)

In my opinion it should be changed to something like: (?)

    create = None
    if exists and if_exists == 'replace':
        create = "DROP TABLE %s" % name + "\n"+get_schema(frame, name, flavor)
    elif not exists:
        create = get_schema(frame, name, flavor)

If i manually work around that, everything works as expected:

    exists = sql.table_exists('sp500', conn, 'sqlite')
    if not exists:
        sql.write_frame(sp5002, name="sp500", con=conn, if_exists='replace')
    else:
        create = "DROP TABLE sp500"
        cur = conn.cursor()
        cur.execute(create)
        cur.close()
        sql.write_frame(sp5002, name="sp500", con=conn, if_exists='replace')

#2971

@jphme
Copy link
Author

jphme commented Jan 20, 2014

still broken in 0.13 ...

hayd pushed a commit to hayd/pandas that referenced this issue Jan 29, 2014
The if_exists argument in io.sql.write_frame needed data validation
because the logic of the function implicitly used 'append' if the argument
value was any string that was not either 'fail' or 'replace'.
I added a new unit test to support the requirement.

BUG: Fix if_exists='replace' functionality in io.sql.write_frame

This should resolve issues pandas-dev#2971 and pandas-dev#4110

CLN: Refactor in between test clean ups to be more DRY

TST: Complete test coverage for if_exists uses in io.sql.write_frame

CLN: Refactor to make interaction between exists and if_exists clearer

This refactor results in the function logic being clearer, since if_exists is
only relevant when exists is True, the program flow is better served to
have if_exists control flow only when exists is True

BUG: Fix regression introduced by c28f11a0041a9f3b25f33b0539e42fa802b1d8d4

sqlite3 convenience function executescript not available
in other database flavors.

TST: Adding if_exist test for mysql flavor
jreback pushed a commit to jreback/pandas that referenced this issue Feb 4, 2014
The if_exists argument in io.sql.write_frame needed data validation
because the logic of the function implicitly used 'append' if the argument
value was any string that was not either 'fail' or 'replace'.
I added a new unit test to support the requirement.

BUG: Fix if_exists='replace' functionality in io.sql.write_frame

This should resolve issues pandas-dev#2971 and pandas-dev#4110

CLN: Refactor in between test clean ups to be more DRY

TST: Complete test coverage for if_exists uses in io.sql.write_frame

CLN: Refactor to make interaction between exists and if_exists clearer

This refactor results in the function logic being clearer, since if_exists is
only relevant when exists is True, the program flow is better served to
have if_exists control flow only when exists is True

BUG: Fix regression introduced by c28f11a0041a9f3b25f33b0539e42fa802b1d8d4

sqlite3 convenience function executescript not available
in other database flavors.

TST: Adding if_exist test for mysql flavor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker Blocking issue or pull request for an upcoming release Bug IO Data IO issues that don't fit into a more specific label IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant