pandas.DataFrame.to_sql的使用方法

root/123
databases:cz

输入:

import pandas as pd
from sqlalchemy import create_engine

engine = create_engine("mysql+pymysql://root:123@localhost:3306/cz?charset=utf8")

df = pd.DataFrame({'balance':[10000.000],'account':['13579.SH'],'charges':[0.0005]})

df.to_sql(name='my_balance', con=engine ,if_exists='append',index=True)  #,index=False, index_label='id'

返回:

$ C:\\Users\\vv\\AppData\\Local\\Programs\\Python\\Python37\\python.exe c:\\Users\\vv\\.vscode\\extensions\\ms-python.python-2019.10.41019\\pythonFiles\\ptvs
d_launcher.py --default --nodebug --client --host localhost --port 4696 d:\\python-学习资料\\实验\\gp-计算实验\\5.py
C:\Users\vv\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py:170: Warning: (3719, "'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.")
  result = self._query(query)
C:\Users\vv\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xB0\\xB2\\xD7\\xB0\\xB0\\xFC...' for column 'VARIABLE_VALUE' at row 1")
  result = self._query(query)
C:\Users\vv\AppData\Local\Programs\Python\Python37\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 1")
  result = self._query(query)

亲测可用,当返回这个时,数据库中新增数据。

你可能感兴趣的:(pandas.DataFrame.to_sql的使用方法)