Sqlite error - table data has no column named date
⚓ Rust 📅 2026-03-05 👤 surdeus 👁️ 2I have a program that reads in a CSV file line by line. As each line is read, I am attempting to insert the fields into an SQLite database. The program reads the first line of the CSV file and prints out the first date value correctly. When it attempts to insert this date value into the database an error is generated "table data has no column named date". I have reviewed the code and do not see an error. Any assistance will be appreciated. Below are the functions used to create the table and insert CSV data into an SQLite database via rusqlite.
use rusqlite::{Connection, Error};
pub fn create_table(conn: &Connection) -> Result<(), Error> {
match conn.execute(
"CREATE TABLE IF NOT EXISTS data (
date TEXT NOT NULL PRIMARY KEY,
open REAL NOT NULL,
high REAL NOT NULL,
low REAL NOT NULL,
close REAL NOT NULL,
volume INTEGER NOT NULL
)",
[],
) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
use std::fs::File;
use std::io::{BufReader, BufRead};
use rusqlite::{Connection, named_params};
use crate::utils::enums::io_sql_error::IoSqlError;
pub fn csv_to_sql(csv_file: &File, conn : &Connection) -> Result<(), IoSqlError> {
let reader = BufReader::new(csv_file);
let mut lines = reader.lines().skip(1);
let mut fields: Vec<String>;
while let Some(line) = lines.next() {
match line {
Ok(lineitem) => {
fields = lineitem.split(',').map(|s| s.trim().to_string()).collect();
eprintln!("Read line date: {}", fields[0]);
// prints out the corret date
},
Err(e) => {
eprintln!("Error reading line: {}", e);
return Err(From::from(e));
}
}
match conn.execute (
"Insert INTO data (date, open, high, low, close, volume)
values (:date, :open, :high, :low, :close, :volume)",
named_params! {"date": fields[0].to_string(),
"open": fields[1].parse::<f64>().unwrap_or(0.0),
"high": fields[2].parse::<f64>().unwrap_or(0.0),
"low": fields[3].parse::<f64>().unwrap_or(0.0),
"close": fields[4].parse::<f64>().unwrap_or(0.0),
"volume": fields[5].parse::<i64>().unwrap_or(0)},
)
{
Ok(_) => {
eprintln!("Record added successfully");
},
Err(e) => {
eprintln!("Error inserting into database: {}", e);
return Err(From::from(e));
// Error inserting into database: table data has no column named date
}
};
} // end of while loop
Ok(())
} // end of function
2 posts - 2 participants
🏷️ Rust_feed