-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(open-mysql-db): pandas support (#3868)
* feat(open-mysql-db): refactor 1. remove unnecessary instance var port 2. fix cause null bug 3. remove unnecessary throws 4. fix ctx.close() sequence bug 5. config sessionTimeout and requestTimeout 6. add docs of SqlEngine * feat(open-mysql-db): refactor * feat(open-mysql-db): revert passsword * feat(open-mysql-db): mock commit and schema table count * feat(open-mysql-db): replace data type text with string * feat(open-mysql-db): remove null --------- Co-authored-by: yangwucheng <[email protected]>
- Loading branch information
1 parent
1c1e213
commit b7e592c
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pandas as pd | ||
from sqlalchemy import create_engine | ||
|
||
if __name__ == '__main__': | ||
# Create a Pandas DataFrame (replace this with your actual data) | ||
data = {'id': [1, 2, 3], | ||
'name': ['Alice', 'Bob', 'Charlie'], | ||
'age': [25, 30, 35], | ||
'score': [1.1, 2.2, 3.3], | ||
'ts': [pd.Timestamp.utcnow().timestamp(), pd.Timestamp.utcnow().timestamp(), | ||
pd.Timestamp.utcnow().timestamp()], | ||
'dt': [pd.to_datetime('20240101', format='%Y%m%d'), pd.to_datetime('20240201', format='%Y%m%d'), | ||
pd.to_datetime('20240301', format='%Y%m%d')], | ||
} | ||
df = pd.DataFrame(data) | ||
|
||
# Create a MySQL database engine using SQLAlchemy | ||
engine = create_engine('mysql+pymysql://root:[email protected]:3307/demo_db') | ||
|
||
# Replace 'username', 'password', 'host', and 'db_name' with your actual database credentials | ||
|
||
# Define the name of the table in the database where you want to write the data | ||
table_name = 'demo_table1' | ||
|
||
# Write the DataFrame 'df' into the MySQL table | ||
df.to_sql(table_name, engine, if_exists='replace', index=False) | ||
|
||
# 'if_exists' parameter options: | ||
# - 'fail': If the table already exists, an error will be raised. | ||
# - 'replace': If the table already exists, it will be replaced. | ||
# - 'append': If the table already exists, data will be appended to it. | ||
|
||
print("Data written to MySQL table successfully!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters