为什么SQLITE使用Python 3不接受大于8的My INTEGER / TEXT数据?

迈萨姆H

问题

我正在尝试将csv文件读取到Pandas,并将其写入SQLite数据库。该过程适用于csv文件中的所有列,但“ Fill qty”是正整数(int64)除外。该过程将类型从TEXT / INTEGER更改为BLOB。因此,我尝试仅将Pandas的“ Fll qty”列加载到SQLite,并且令人惊讶地发现,我可以安全地对所有小于10的整数执行此操作(我的数据集中没有9,因此基本上为1,2)。 ..,8已成功加载)。

这是我尝试过的:

我尝试了一下:将Schema中的“ Fill_Qty”类型更改为INTEGER转换为REAL,NULL或TEXT,将Pandas中的数据类型从int64更改为float或string,然后再插入SQLite表。他们都没有工作。从外观上看,“ Trade_History.csv”文件在Pandas或Excel中似乎很好。有我眼睛看不到的东西吗?!所以我真的很困惑这里发生了什么!

您将需要.csv文件来测试代码。这是代码和.csv文件:https : //github.com/Meisam-Heidari/Trading_Min_code

编码:

### Imports:
import pandas as pd
import numpy as np
import sqlite3
from sqlite3 import Error

def create_database(db_file):
    try:
        conn = sqlite3.connect(db_file)
    finally:
        conn.close()

def create_connection(db_file):
    """ create a database connection to the SQLite database
        specified by db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
        conn = sqlite3.connect(db_file)
        return conn
    return None

def create_table(conn,table_name):  
    try:
        c = conn.cursor()
        c.execute('''CREATE TABLE {} (Fill_Qty TEXT);'''.format(table_name))
    except Error as e:
        print('Error Code:  ', e)
    finally:
        conn.commit()
        conn.close()
    return None

def add_trade(conn, table_name, trade):
    try:
        print(trade)
        sql = '''INSERT INTO {} (Fill_Qty)
              VALUES(?)'''.format(table_name)
        cur = conn.cursor()
        cur.execute(sql,trade)
    except Error as e:
        print('Error When trying to add this entry:  ',trade)
    return cur.lastrowid

def write_to_db(conn,table_name,df):
    for i in range(df.shape[0]):
        trade = (str(df.loc[i,'Fill qty']))
        add_trade(conn,table_name,trade)
        conn.commit()

def update_db(table_name='My_Trades', db_file='Trading_DB.sqlite', csv_file_path='Trade_History.csv'):
    df_executions = pd.read_csv(csv_file_path)
    create_database(db_file)
    conn = create_connection(db_file)
    table_name = 'My_Trades'
    create_table(conn, table_name)
    # writing to DB
    conn = create_connection(db_file)
    write_to_db(conn,table_name,df_executions)
    # Reading back from DB
    df_executions = pd.read_sql_query("select * from {};".format(table_name), conn)
    conn.close()
    return df_executions



### Main Body:

df_executions = update_db()


任何替代方案

我想知道是否有人有类似的经历?有什么建议/解决方案可以帮助我在SQLite中加载数据吗?我正在尝试使某些东西轻巧便携,除非没有其他选择,否则我不建议不要使用Postgres或MySQL。

机械肉

.execute()插入数据时,您没有将容器传递给参考:https : //www.python.org/dev/peps/pep-0249/#id15

您需要做的是:

trade = (df.loc[i,'Fill qty'],)
#                            ^ this comma makes `trade` into a tuple

您遇到的错误类型将是:

ValueError:参数类型不受支持

要么:

sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用1,并且提供2。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么python中的sorted()不接受位置参数?

在PostgreSQL中将列数据类型从Text更改为Integer

Python:为什么不接受月份的01或02或03输入?

Python不接受UTF-8编码

Python对sqlite3的executemany不接受元组列表

如何将Integer保存到数据库(SQLite)

Python:opencv warpPerspective既不接受2个参数也不接受3个参数

Elasticsearch不接受数据

为什么TF Boosted Trees不接受数字数据作为输入?

为什么线性回归不接受系列数据作为第一个参数?

为什么我无法使用Python3解析JSON数据?

Python ffmpeg不接受路径,为什么?

为什么Integer.bitCount()对于255的输入返回8?

为什么数据类型为number(3,2)的列在Oracle中不接受22.3作为值?

使用INTEGER数据类型时,为什么SQL * Loader加载808594481?

为什么hashcat不接受此RAR3 hash?

ngTable为什么不接受我的数据集?

为什么此python类方法不起作用?(它使用SQLite3)

为什么不接受sudo?

为什么使用Python 3的Tweepy根本不从流返回任何数据?

Python Sqlite3- Python Shell可以创建和编辑数据库,但是.py文件为什么不能创建或访问任何数据库

使用Ada.Text_IO.Integer_IO

如果我可以使用 SQLite 3 在 python 中组织数据,我为什么要使用类?

为什么spring boot中的controller只接受json类型的数据,不接受thymeleaf表单提交的数据?

为什么 dplyr 过滤器不接受整数数据帧?

为什么python不接受我的输入并使用它?

pyodbc - 使用 python 3 时,游标不接受整数参数

使用 Python 在 sqlite3 中保存列表使用什么数据类型?

为什么 append 函数不接受相同类型的数据?