I am trying to import a CSV file to postgres. All my code is posted below. This is a simple thing and i can't figure the 'rust' way and i get an error during the insert that i can't figure out how to troubleshoot.
The error first. The table has some int and varchar columns. In the csv if i see "NA" i am turning that into a "0" so it will convert correctly. Based on the error below i can't tell if that is what is wrong or not.
dan@dan-VirtualBox:~/Downloads/csvreaders/rustcsvreader$ ./target/release/rustcsvreader < ~/Downloads/2008.csv
thread 'main' panicked at 'Insert failed: Db(DbError { severity: "ERROR", parsed_severity: None, code: SyntaxError, message: "syntax error at end of input", detail: None, hint: None, position: Some(Normal(188)), where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("scan.l"), line: Some(1074), routine: Some("scanner_yyerror") })', /checkout/src/libcore/result.rs:859
Also, is the way i am building the SQL parameters correct? Is there a better way?
dan@dan-VirtualBox:~/Downloads/csvreaders/rustcsvreader$ rustup show
Default host: x86_64-unknown-linux-gnu
stable-x86_64-unknown-linux-gnu (default)
rustc 1.18.0 (03fc9d622 2017-06-06)
thank you.
code starts here:
extern crate csv;
extern crate postgres;
use std::fs;
use std::string::String;
//use std::io;
//use std::fs::File;
//use std::path::Path;
use postgres::{Connection, TlsMode};
type OnTimeRecord = (
String, String, String, String, String, String, String,
String, String, String, String, String, String, String,
String, String, String, String, String, String, String,
String, String
);
fn main() {
//let path = Path::new("/home/dan/Downloads/2008.csv");
//http://stat-computing.org/dataexpo/2009/the-data.html
let mut rowcnt = 0;
let mut rdr = csv::Reader::from_reader(std::io::stdin());
//let mut rdr = csv::Reader::from_path("/home/dan/Downloads/2008.csv").unwrap();
let conn = Connection::connect("postgres://dan:dan@localhost/rowcounttest", TlsMode::None)
.expect("Connection failed");
let mut rec = csv::ByteRecord::new();
//while rdr.read_byte_record(&mut rec).expect("all done") {
for result in rdr.records() {
let rec = result.expect("no result");
//println!("{:?}", rec);
let mut fields : String = rec.iter().map(|f| if f == "NA" { "0" } else { f } ).collect();
rowcnt += 1;
conn.execute("INSERT INTO ontime_performance VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
$12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22,
$23, $24, $25, $26, $27, $28, $29
", &[&fields]
// &rec.get(0), &rec.get(1), &rec.get(2), &rec.get(3), &rec.get(4), &rec.get(5),
// &rec.get(6), &rec.get(7), &rec.get(8), &rec.get(9), &rec.get(10), &rec.get(11),
// &rec.get(12), &rec.get(13), &rec.get(14), &rec.get(15), &rec.get(16), &rec.get(17),
// &rec.get(18), &rec.get(19), &rec.get(20), &rec.get(21), &rec.get(22), &rec.get(23),
// &rec.get(24), &rec.get(25), &rec.get(26), &rec.get(27), &rec.get(28)
// ]
).expect("Insert failed");
}
println!("rowcount = {}", rowcnt);
}
stack backtrace:
0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
1: std::sys_common::backtrace::_print
at /checkout/src/libstd/sys_common/backtrace.rs:71
2: std::panicking::default_hook::{{closure}}
at /checkout/src/libstd/sys_common/backtrace.rs:60
at /checkout/src/libstd/panicking.rs:355
3: std::panicking::default_hook
at /checkout/src/libstd/panicking.rs:371
4: std::panicking::rust_panic_with_hook
at /checkout/src/libstd/panicking.rs:549
5: std::panicking::begin_panic
at /checkout/src/libstd/panicking.rs:511
6: std::panicking::begin_panic_fmt
at /checkout/src/libstd/panicking.rs:495
7: rust_begin_unwind
at /checkout/src/libstd/panicking.rs:471
8: core::panicking::panic_fmt
at /checkout/src/libcore/panicking.rs:69
9: core::result::unwrap_failed
10: rustcsvreader::main
11: __rust_maybe_catch_panic
at /checkout/src/libpanic_unwind/lib.rs:98
12: std::rt::lang_start
at /checkout/src/libstd/panicking.rs:433
at /checkout/src/libstd/panic.rs:361
at /checkout/src/libstd/rt.rs:57
13: __libc_start_main
14: _start