Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将数据帧复制到Sqlite-绑定问题_Python_Sqlite_Dataframe - Fatal编程技术网

Python 将数据帧复制到Sqlite-绑定问题

Python 将数据帧复制到Sqlite-绑定问题,python,sqlite,dataframe,Python,Sqlite,Dataframe,我知道这是一个有几个线程的简单主题,但我找不到解决方案 我拥有的是一个txt文件,我将其读入数组并将其转换为数据帧。 之后,我将转换单个列,使其成为日期/数字。我确信有一种更有效的方法来构建我所寻找的东西,但它对我很有效 我将得到一个如下所示的数据帧: PSVLD Market Date PSVLD Ticker PSVLD Tenor Date PSVLD Percent of Spot \ 1 2017-02-03 .MSCIEA

我知道这是一个有几个线程的简单主题,但我找不到解决方案

我拥有的是一个txt文件,我将其读入数组并将其转换为数据帧。 之后,我将转换单个列,使其成为日期/数字。我确信有一种更有效的方法来构建我所寻找的东西,但它对我很有效

我将得到一个如下所示的数据帧:

    PSVLD Market Date PSVLD Ticker PSVLD Tenor Date  PSVLD Percent of Spot  \
1         2017-02-03      .MSCIEA               1M                   50.0   
2         2017-02-03      .MSCIEA               1M                   60.0   
3         2017-02-03      .MSCIEA               1M                   70.0   
4         2017-02-03      .MSCIEA               1M                   75.0   
5         2017-02-03      .MSCIEA               1M                   80.0   
6         2017-02-03      .MSCIEA               1M                   85.0   
7         2017-02-03      .MSCIEA               1M                   90.0   
8         2017-02-03      .MSCIEA               1M                   92.5   
9         2017-02-03      .MSCIEA               1M                   95.0   
10        2017-02-03      .MSCIEA               1M                   97.5   

    PSVLD Vol  
1    34.96749  
2    34.36383  
3    32.58459  
4    30.53958  
5    28.30699  
6    24.74774  
7    20.07822  
8    17.38867  
9    14.58027  
10   11.84767  
现在我想把这些数据插入数据库。我创建了一个新表,并尝试将此数据帧执行到我的文件中,但它不起作用

con = sq3.connect('my_db.db')

query = 'CREATE TABLE ImpliedVola (Date date, Ticker varchar(50), Tenor varchar(10),Strike real, IV real)' 
con.execute(query)
con.commit()
con.executemany('INSERT INTO ImpliedVola VALUES (?, ?, ?, ?, ?)', df) 
但不幸的是,它提到了17个绑定。你知道我做错了什么吗?提前谢谢

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 5, and there are 17 supplied.
我会用本地熊猫的方法

演示:

结果:

D:\temp>sqlite3 df.sqlite
SQLite version 3.10.1 2016-01-13 21:41:56
Enter ".help" for usage hints.
sqlite> .mode column
sqlite> .header on
sqlite> select * from ImpliedVola;
index       PSVLD_Market_Date    PSVLD_Ticker  PSVLD_Tenor_Date  PSVLD_Percent_of_Spot  PSVLD_Vol
----------  -------------------  ------------  ----------------  ---------------------  ----------
0           2017-02-03 00:00:00  .MSCIEA       1M                50.0                   34.96749
1           2017-02-03 00:00:00  .MSCIEA       1M                60.0                   34.36383
2           2017-02-03 00:00:00  .MSCIEA       1M                70.0                   32.58459
3           2017-02-03 00:00:00  .MSCIEA       1M                75.0                   30.53958
4           2017-02-03 00:00:00  .MSCIEA       1M                80.0                   28.30699
5           2017-02-03 00:00:00  .MSCIEA       1M                85.0                   24.74774
6           2017-02-03 00:00:00  .MSCIEA       1M                90.0                   20.07822
7           2017-02-03 00:00:00  .MSCIEA       1M                92.5                   17.38867
8           2017-02-03 00:00:00  .MSCIEA       1M                95.0                   14.58027
9           2017-02-03 00:00:00  .MSCIEA       1M                97.5                   11.84767
我会用本地熊猫的方法

演示:

结果:

D:\temp>sqlite3 df.sqlite
SQLite version 3.10.1 2016-01-13 21:41:56
Enter ".help" for usage hints.
sqlite> .mode column
sqlite> .header on
sqlite> select * from ImpliedVola;
index       PSVLD_Market_Date    PSVLD_Ticker  PSVLD_Tenor_Date  PSVLD_Percent_of_Spot  PSVLD_Vol
----------  -------------------  ------------  ----------------  ---------------------  ----------
0           2017-02-03 00:00:00  .MSCIEA       1M                50.0                   34.96749
1           2017-02-03 00:00:00  .MSCIEA       1M                60.0                   34.36383
2           2017-02-03 00:00:00  .MSCIEA       1M                70.0                   32.58459
3           2017-02-03 00:00:00  .MSCIEA       1M                75.0                   30.53958
4           2017-02-03 00:00:00  .MSCIEA       1M                80.0                   28.30699
5           2017-02-03 00:00:00  .MSCIEA       1M                85.0                   24.74774
6           2017-02-03 00:00:00  .MSCIEA       1M                90.0                   20.07822
7           2017-02-03 00:00:00  .MSCIEA       1M                92.5                   17.38867
8           2017-02-03 00:00:00  .MSCIEA       1M                95.0                   14.58027
9           2017-02-03 00:00:00  .MSCIEA       1M                97.5                   11.84767

谢谢你的意见。这是可行的,但to_sql()会替换表中以前的所有数据。由于我想每天添加数据,我不知道如何解决这个问题。@StefanMüller,简单地使用
如果_exists='append'
另一个问题,我可以声明一些主键并更新现有值吗?就像sql中的更新?@StefanMüller一样,您必须手动执行。没有本地的熊猫方法来解决这个问题。嗨,马苏,你认为我可以用这样的方法来解决这个问题吗<代码>对于索引,df.iterrows()中的行:con.execute('INSERT INTO impvolu_Data(Date,Ticker,Tenor,Strike,IV)值(?,,,,,,,,?)在重复键更新IV=?',[row[0],row[1],row[2],row[3],row[4],row[4]])感谢您的输入。这是可行的,但to_sql()会替换表中以前的所有数据。由于我想每天添加数据,我不知道如何解决这个问题。@StefanMüller,简单地使用
如果_exists='append'
另一个问题,我可以声明一些主键并更新现有值吗?就像sql中的更新?@StefanMüller一样,您必须手动执行。没有本地的熊猫方法来解决这个问题。嗨,马苏,你认为我可以用这样的方法来解决这个问题吗<代码>对于索引,df.iterrows()中的行:con.execute('INSERT INTO ImpVol_Data(Date,Ticker,Tenor,Strike,IV)值(?,,,,,,,,?)在重复键更新IV=?',[row[0],row[1],row[2],row[3],row[4],row[4])然而,有人知道我在这里做错了什么?因为如果我做对了,我可以用updater或insert替换查询。不过,有人知道我做错了什么?因为如果正确的话,我可以用updater或insert替换查询。