首页 > 解决方案 > Python插入sqlite表

问题描述

一直说参数的类型不受支持。我究竟做错了什么?我试过 [rows] 和 ,rows)。我在其他两行中遇到的另一个错误是我提供的绑定数量不正确。当前语句使用 9 个,提供了 11 个。

import csv
import sqlite3




   #define connection and cursor

    conn = sqlite3.connect('metrics1.db')


    c = conn.cursor()

    #Table Definition
    ##cmd = 'CREATE TABLE IF NOT EXISTS Metrics(ID INTEGER PRIMARY KEY 
    AUTOINCREMENT, ticket_num INTEGER, s$

#Creating the Tables
c.execute('''CREATE TABLE IF NOT EXISTS Metrics([ID] INTEGER PRIMARY KEY AUTOINCREMENT, [ticket_num] IN$
#Opening Metrics CSV
file = open('/root/CustomerSurveys.csv')

#Read File
rows = csv.reader(file)

# Importing the contents of the file into metrics table
c.executemany("INSERT INTO metrics values (?,?,?,?,?,?,?,?,?)", [rows])

# SQL query to retrieve all data from
# the person table To verify that the
# data of the csv file has been successfully
# inserted into the table
c.execute("SELECT * FROM metrics1")
print(c.fetchall())
# Output to the console screen


conn.commit()
conn.close()

标签: pythonsqlite

解决方案


推荐阅读