是sqlalchemy的新手。当我运行此代码时,没有数据库。我希望它创建数据库,添加定义的表和数据。阅读to_sql的文档这个代码应该创建表,如果它不存在(它没有),当我运行它时会抛出一个错误,表没有列num 1 ??? AND不会创建数据库。我做错了什么?
import pandas as pd
import sqlite3
from sqlalchemy import create_engine
date_stuff = [ (20171219, 13.71,28), (20171319, 144.71,33), (20171919, 99.99,99)]
labels = ['date', 'num 1' , 'num 2']
dev_env = "/home/test/Desktop/mtest/hvdata/"
db_name = "tinydatabase.db"
def new_sql_add ( todays_data ):
todays_data.to_sql(name='mcm_trends', con = db ,if_exists='append')
if __name__ == '__main__' :
db_path = dev_env + db_name
db = create_engine('sqlite:///db_path')
df_for_sql = pd.DataFrame.from_records( date_stuff , columns = labels)
new_sql_add ( df_for_sql )