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

时间:2018-03-27 13:21:49

标签: python pandas dataframe sqlite readfile

我有一个使用 pandas 从csv文件读取的python代码,然后创建一个名为 rduDB.db 的文件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表:

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

系统能够:

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

    无法将检索到的csv数据插入sqlite3表

    我使用iloc()循环检索数据,以便将数据插入到sqlite3表中。

    系统显示错误:

      

    builtins.TypeError:无法在不支持块值的情况下操作1   操作数类型 - :'str'和'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()
    

1 个答案:

答案 0 :(得分:0)

您只需使用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