Which of the following is used to export a DataFrame to MYSQL?
Note: Here 'df' is a DataFrame.
Which of the following is used to export a DataFrame to MYSQL?
Note: Here 'df' is a DataFrame.
Solution
✅ Correct Option: 1
df.to_sql() writes the rows of a DataFrame into a table in a SQL database such as MySQL. It needs a connection or SQLAlchemy engine, for example df.to_sql('students', con=engine, if_exists='replace', index=False).
Options 2, 3 and 4: read_sql_query(), read_sql() and read_sql_table() all do the opposite. They import data from a database into a DataFrame.
Memory trick: a method called on df (df.to_...) sends data out, while the pd.read_... functions bring data in.
Related questions:
2026: 18 May Shift 2
2025: 20 May Shift 1