如何使用 python 将 csv 中的值插入到 sqlite3 中

how to insert values from csv into sqlite3 using python

我有一个 python 代码,它使用 pandas 从 csv 文件中读取,然后创建一个名为 [=42 的文件 sqlite3 =].

            date  temperaturemin  temperaturemax
0     2007-01-13            48.0            69.1
1     2007-01-19            34.0            54.0
2     2007-01-21            28.0            35.1
3     2007-01-25            30.9            46.9
4     2007-01-27            32.0            64.0
5     2007-02-05            19.9            39.9

sqlite3 table :

CREATE TABLE IF NOT EXISTS rduWeather 
                           (id INTEGER PRIMARY KEY, 
                            Date varchar(256),
                            TemperatureMin text,
                            TemperatureMax text)'''

系统能够:

  1. 从 csv 中读取并获取所需的值
  2. 创建数据库文件
  3. 创建 sqlite table

not able to insert the retrieved csv data into sqlite3 table

我正在使用 iloc() 遍历检索到的数据,以便将数据插入 sqlite3 table。

系统显示错误:

builtins.TypeError: Could not operate 1 with block values unsupported operand type(s) for -: 'str' and 'int'

代码:

导入 sqlite3

import pandas as pd
import os

class readCSVintoDB():

    def __init__(self):
        '''
        self.csvobj = csvOBJ
        self.dbobj = dbOBJ
        '''

        self.importCSVintoDB()

    def importCSVintoDB(self):

        csvfile = "C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv"
        df = pd.read_csv(csvfile,sep=';')
        dp = (df[['date','temperaturemin','temperaturemax']])
        print(dp)

        '''
        check if DB file exist 
        if no create an empty db file
        '''
        if not(os.path.exists('./rduDB.db')):
            open('./rduDB.db','w').close()

        '''
        connect to the DB and get a connection cursor
        '''
        myConn = sqlite3.connect('./rduDB.db')
        dbCursor = myConn.cursor()

        dbCreateTable = '''CREATE TABLE IF NOT EXISTS rduWeather 
                           (id INTEGER PRIMARY KEY, 
                            Date varchar(256),
                            TemperatureMin text,
                            TemperatureMax text)'''

        dbCursor.execute(dbCreateTable)
        myConn.commit()

        '''
        insert data into the database
        '''
        counter =0
        for i in len(dp-1):

            print(dp.iloc[len(dp)])
        #print(len(dp))

            #dbCursor.execute('''
            #INSERT INTO  rduWeather ('Date','TemperatureMin','TemperatureMax') VALUES (?,?,?)''', i)

    #myConn.commit()

        myConn.close()        



test1 = readCSVintoDB()

你可以简单地使用DataFrame.to_sql()方法:

import sqlite3

conn = sqlite3.connect('c:/temp/test.sqlite')

#...

df.to_sql('rduWeather', conn, if_exists='append', index_label='id')

演示:

In [100]: df.to_sql('rduWeather', conn, if_exists='append', index_label='id')

In [101]: pd.read_sql('select * from rduWeather', conn)
Out[101]:
   id        date  temperaturemin  temperaturemax
0   0  2007-01-13            48.0            69.1
1   1  2007-01-19            34.0            54.0
2   2  2007-01-21            28.0            35.1
3   3  2007-01-25            30.9            46.9
4   4  2007-01-27            32.0            64.0
5   5  2007-02-05            19.9            39.9

In [102]: pd.read_sql('select * from rduWeather', conn, index_col='id')
Out[102]:
          date  temperaturemin  temperaturemax
id
0   2007-01-13            48.0            69.1
1   2007-01-19            34.0            54.0
2   2007-01-21            28.0            35.1
3   2007-01-25            30.9            46.9
4   2007-01-27            32.0            64.0
5   2007-02-05            19.9            39.9