I want to write an oracle query to fetch data from table based on values(where clause) from an excel sheet. The excel sheet contains 3500+ values and i have to get data for all those 3500+ values from my database.
I want to compare name of columns of an Excel file with name of fields of a table in SQL database while importing data from Excel file. And then I can map my database filed name with Excel file column name.
I am able to insert values into my Excel file using c# successfully. But when my column name contains "#" (for example Target_Group#), I get this error:
Unknown column name
But in my Excel the column name should be "Target_Group# "
My insert query is:
string query = "INSERT INTO [Sheet2$] ([Target_Group#]) VALUES('OHMCRWP0')";
I am using OLEDB connections
When I import a csv file to sqlite database, it imports number as string to integer column, how can I fix this? A line from my csv file like this:
31,c,BB ROSE - 031,c31,,9,7,0,"142,000",0
CSV files do no have data types; everything is a string.
To convert all values in a column into a number, use something like this:
UPDATE MyTable SET MyColumn = CAST(MyColumn AS INTEGER)
When importing csv files, SQLite assumes all fields are text fields. So you need to perform some extra steps in order to set the correct data types.
However, it is my understanding that you cannot use the ALTER TABLE statement to modify a column in SQLite. Instead, you will need to rename the table, create a new table, and copy the data into the new table.
https://www.techonthenet.com/sqlite/tables/alter_table.php
So suppose I have an employees.csv file I want to import into SQLite database with the correct data types.
employee_id,last_name,first_name,hire_date
1001,adams,john,2010-12-12
1234,griffin,meg,2000-01-01
2233,simpson,bart,1990-02-23
First, create a SQLite database called mydb.sqlite and import employees.csv into a SQLite table called employees.
# create sqlite database called mydb.sqlite
# import data from 'employees.csv' into a SQLite table called 'employees'
# unfortunately, sqlite assumes all fields are text fields
$ sqlite3 mydb.sqlite
sqlite> .mode csv
sqlite> .import employees.csv employees
sqlite> .quit
At this point, the data is imported as text. Let's first get the employees schema from the database and save it to employees.sql.We can use this to create a new script that would rename the table, create a new table, and copy the data into the new table.
$ sqlite3 mydb.sqlite
sqlite> .once employees.sql
sqlite> .schema employees
sqlite> .quit
You should now have employees.sql with the following schema:
CREATE TABLE employees(
"employee_id" TEXT,
"last_name" TEXT,
"first_name" TEXT,
"hire_date" TEXT
);
Let's now create a SQL filed called alterTable.sql that would rename the table, create a new table, and copy the data into the new table.
alterTable.sql
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE employees RENAME TO _employees_old;
CREATE TABLE employees
( "employee_id" INTEGER,
"last_name" TEXT,
"first_name" TEXT,
"hire_date" NUMERIC
);
INSERT INTO employees ("employee_id", "last_name", "first_name", "hire_date")
SELECT "employee_id", "last_name", "first_name", "hire_date"
FROM _employees_old;
COMMIT;
PRAGMA foreign_keys=on;
Finally, we can execute SQL in alterTable.sql and drop the old renamed table
$ sqlite3 mydb.sqlite
sqlite> .read alterTable.sql
sqlite> drop table _employees_old;
At this point, the imported employee data should have the correct data types instead of the default text field.
If you do it this way, you don't have to worry about headers in csv file being imported as data. Other methods might require you delete the header either before or after importing the csv file.
You just need to create the table first with correct types and then the CSV-import will keep this types, because the table already exists.
Here a sample:
create table table1(name TEXT, wert INT);
.mode csv
.separator ";"
.import "d:/temp/test.csv" table1
If you need to delete an imported header-line then use something like this after the import:
delete from table1 where rowid=1;
or use this in case you already did multiple imports into the same table:
delete from [table1] where "name"='name'; -- try to use a name of an INT-column for this.
at the end you can just check the correct import like this:
.header ON
select * from table1 order by wert;
In SQLite, you cannot change the type affinities of columns. Therefore you should create your table and then .import your CSV file into the table. If your CSV file has a header, that will be treated as data upon import. You can either delete the header before importing (in the CSV file), or delete the header after import (in the table). Since the typeof all the header fields will be TEXT, you can easily find this header in a table where some columns have numeric type affinities.
Import CSV file into SQLite.
Go to Database Structure and select imported CSV file
select modify table from the tab
select field one and change name to desired name of column.
Next select the desired data type from the drop down menu. You can now change from Text to Integer or Numeric depending on the data you are working with
I am using sqlite 3.39.4, I would do as follows:
as suggested above create a new table 'newtable' with the right types, then to import data from your 'mycsvtable.csv', type
.mode csv
.import --skip 1 mycsvtable.csv newtable
the --skip 1 avoids the first row if you have headers in your csv