ömer sarı ömer sarı - 3 months ago 16
Python Question

Data entry in sqlite database

l m a fresh user and l m not good at coding yet. l created a database with 3 column (date, station, pcp ).l have 41_years_data set and by using for loop ,l would like to insert these data set in database. L could not do it correctly. When l run code ,it gives " c.execute("INSERT INTO pcp VALUES(kdm,station,klm)")
sqlite3.OperationalError: no such column: kdm" error.l tried something but no work.

My code is below

import sqlite3
from datetime import date, datetime, timedelta

conn=sqlite3.connect("pcp.db")
c=conn.cursor()

pcp=open("p.txt")

station="1"

def create_table():
c.execute("CREATE TABLE IF NOT EXISTS pcp (date DATE,stations TEXT,pcp DOUBLE)")

def perdelta(start, end, delta):
curr = start
while curr < end:
yield curr
curr += delta

def data_entry():
for i in pcp:
k=print(i)
def data_entry2():
for a in perdelta(date(1979, 1, 1), date(2015, 1, 1), timedelta(days=1)):
b=print(a)

def dyn():
kdm=data_entry2()
klm=data_entry()
c.execute("INSERT INTO pcp(date,stations,pcp) VALUES(?,?,?)",( kdm,station,klm ))
conn.commit()







create_table()
dyn()

Answer

Your INSERT statement

c.execute("INSERT INTO pcp VALUES(kdm,station,klm)")

contains the names of your variables that hold the data values.

You need to construct the INSERT statement by concatenating the values inside those variables. Like so (maybe there is a more efficient way, but I am not too familiar with python)

insert_sql = 'INSERT INTO pcp(date,stations,pcp) VALUES( ' + kdm + ',' + station + ',' + klm + ')'
c.execute(insert_sql)
Comments