SqlAlchemy / Python数据类型

时间:2018-04-16 15:57:16

标签: python python-3.x sqlite sqlalchemy

我正在努力让我的python和sql数据类型相互通信。我不确定我在这里缺少什么。

我的代码:

import pandas as pd
import sqlite3
from sqlalchemy import create_engine
from sqlalchemy import MetaData
from sqlalchemy import Table
from sqlalchemy import Column
from sqlalchemy import Integer, String, Float, DATE, DECIMAL

def db_conn():
    global conn
    global engine

    db_uri = "sqlite:///db.sqlite"
    engine = create_engine(db_uri)

    conn = engine.connect()
    meta_db = MetaData(engine)

    print("Connected to Database at: " + str(datetime.today()))

    return conn

db_conn()

# create table
meta = MetaData(engine)
table = Table('MarketData2', meta,
   Column('DT', Integer, primary_key=True),
              Column('Currency', String),
              Column('Signal', String),
              Column('Value', Float))
meta.create_all()

def update_MarketData_db(dt, currency, sig, val):
    # insert multiple data
    conn.execute(table.insert(),[
       {'DT': str(dt),
        'Currency': str(currency),
        'Signal': str(sig),
        'Value': float(val)}])

update_MarketData_db('2014-05-04', 'test', 'test_blac', 420.0)

如您所见,我在函数中显式声明了每种数据类型。我也尝试过sqlalchemy中的Float,FLOAT,Decimal和DECIMAL类,但这些类也没有用过。

追溯:

IntegrityError: (sqlite3.IntegrityError) datatype mismatch [SQL: 'INSERT INTO "MarketData2" ("DT", "Currency", "Signal", "Value") VALUES (?, ?, ?, ?)'] [parameters: ('2014-05-04', 'test', 'test_blac', 420.0)] (Background on this error at: http://sqlalche.me/e/gkpj)

0 个答案:

没有答案