SQLite : discrepancy when importing CSV file into table - sqlite

I have a table with last column of integer type and I am trying to import data from a csv file into this table.
Some records have no value for this last column. When importing, I see that the last record is getting handled differently than others. For all other such records, this column is populated with empty string ''. However, in case of last record, it is getting populated with null.
Why is this happening?
Perhaps, if I include a newline character after the last line, it could solve the problem, but thats not an option here. What else can I do to address this?

Assuming every record having a missing third column value other than the last row in the file had empty string, you could just try the following update:
UPDATE yourTable
SET col3 = ''
WHERE col3 IS NULL;
This would work, assuming that at most only the final row would match the above update. The best thing to do here would probably be to fix your CSV file and put a CRLF onto the final line.

Related

How do I populate an empty column in my sql database?

How do I populate an empty column in my sql database?
I have a list that I want in the last column of my database. If I use executemany it just adds rows on the bottom of my database. Is there a way to fill it from the top? The column is currently populated with NULL values.
Just to be clear. I want the first item in the list to be put in the first row and item n on the list to be put into row n.
Thanks in advance
*Always take backup of your database, just incase you want to revert it back to original state.
Each row in your database table should have a unique primary key. As you mentioned you have a list to be uploaded in the last column, I assume you have some kind of relationship that distinguishes which list item goes to which row in the last column of the table.
You an update a single row by simple update syntax as below:
UPDATE table_name
SET last_column = somevalue_from_list
WHERE id = unique_row_id;
If you want to update all the rows with the same value then
UPDATE table_name
SET last_column = one_value_for_all_rows;
Follow the below suggestion only when you are using phpmyadmin to manage your tables:
If you want to update each row with the unique value from the list then my suggestion will be to export your table in csv format, open in excel like software and add your last column values in last column of each row and then import the changed table back to the database.
Hope this help.

LinqToExcel blank rows

I am using LinqToExcel to easily import Excel data into SQL Server.
var fileName = ConfigurationManager.AppSettings["ExcelFileLocation"];
var excel = new ExcelQueryFactory(fileName);
var employees = excel.Worksheet<Employee>().ToList();
Everything's fine, there is only 1 problem, the fields mapped exactly to the database table fields, and in the database they are NOT NULL.
Having said that, if you look at this screenshot of Excel file, some rows below row 3 are actually not empty. There are no spaces, but somehow LinqToExcel reads them as well and of course I get exception thrown by EntityFramework saying the field cannot be null.
I need to select all the blank rows below 3 up to 8980 something, and delete them.
Only then I can use LinqToExcel not trying to import blank rows.
Any idea how to solve the problem?
Thanks.
You can add a condition to the LINQ statement so the empty rows are not included.
var employees = excel.Worksheet<Employee>().Where(x => x.VS2012 != null).ToList();
And if checking for not null does not work, then you can check for an empty string
var employees = excel.Worksheet<Employee>().Where(x => x.VS2012 != "").ToList();
but somehow LinqToExcel reads them as well
This is a quirk of Excel. It remembers how many rows and columns where used when the Sheet was at it largest size. You can see this by typing Ctrl-End. This will select the cell in the last row and column ever used.
Office support describes how to reset the last cell: Locate and reset the last cell on a worksheet
Basically, you delete excess rows and columns, clear formatting, save the workbook and reopen it.
This work-around could be useful if you have Excel-files waiting to be imported and no time to deploy your fixed Linq2Excel client.

SqliteDataReader and duplicate column names when using LEFT JOIN

Is there a documentation/specification about Sqlite3 that would describe would is supposed to happen in the following case?
Take this query:
var cmd = new SqliteCommand("SELECT Items.*, Files.* FROM Items LEFT JOIN Files ON Files.strColName = Items.strColName");
Both Items and Files have a column name "strColName". If an entry exists in Files, it will be joined to the result, if not, it will be NULL.
Let's assume I always need the value of strColName, no matter if it is coming from Items or from Files. If I execute a reader:
var reader = cmd.ExecuteReader();
If there is a match in Files, reader["strColName"] will obviously contain the correct result because the value is set and it is the same in both tables. But if there wasn't a match in Files, will the NULL value of Files overwrite the non-NULL value of Items?
I'm really looking for some specification that defines how a Sqlite3 implementation has to deal with this case, so that I can trust either result.
SQLite has no problem returning multiple columns labelled with the same name.
However, the columns will always be returned in exactly the same order they are written in the SELECT statement.
So, when you are searching for "strColName", you will find the first one, from Items.
It is recommended to use explicit column names instead of * so that the order is clear, and you can access values by their column index, if needed (and you detect incompatible changes in the table structure).

Empty fields extracted from excel

This is my problem:
I'm reading data from an Excel file on a .NET MVC app, what I'm doing is to read all data from the excel and then loop over each record inserting the data contained in the record into my business model.
All works perfectly. However, I've found that one field, sometimes, return an empty string when retrieved from the excel. Curiously this field can contain a simple string or a string that will be treated as an array (it can include '|' characters to build the array) on some excel files the field returns empty when the '|' char is present and in others when it isn't, and this behaviour is consistent all along that file.
There are other fields that can receive the separator and work always ok. The only difference between both fields are that the working ones are pure strings and the one that's failing is a string of numbers with possibles '|' separating them.
I've tried to change the separator character (I tried with '#' with same results) and to specifically format the cells as text without any success.
This is the method that extracts data from the excel
private DataSet queryData(OleDbConnection objConn) {
string strConString = "SELECT * FROM [Hoja1$] WHERE NUMACCION <> ''";
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset = new DataSet();
objAdapter1.Fill(objDataset, "ExcelData");
return objDataset;
}
I first check the fields from the excel with:
fieldsDictionary.Add("Hours", table.Columns["HOURS"].Ordinal);
And later, when looping through the DataSet I extract data with:
string hourString = row.ItemArray[fieldsDictionary["Hours"]].ToString();
This hourString is empty in some records. In some Excel files it's empty when the record contains '|', on others it's empty when it doesn't. I haven't found yet a file where it returns empty on records of both classes.
I'm quite confused about this. I'm pretty sure it has to be related to the numerical nature of field data, but cannot understand why it doesn't solve when I force the cells on the excel file to be "text"
Any help will be more than welcome.
Ok. I finally solved this.
It seems like Excel isn't able to recognize a whole column as same data type if it contains data of possibly different classes. This happens even if you force the cell format to be text on the workbook, as when you query the data it will recognize the field as a determinated type according to the first record it receives; that was the reason why different files emptied different type of records, files starting with a plain text emptied numeric values and vice versa.
I've found a solution to this just changing the connection string to Excel.
This was my original connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pathToFile;Extended Properties="Excel 8.0;HDR=Yes;"
And this the one that fixes the problem
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pathToFile;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"
The parameter IMEX=1 states to excel that it must manage all mixed data columns as plain text. This won't work for you if you need to edit the excel file, as this parameter also opens it on read-only mode. However it was perfect for my situation.

SQLite insert or replace into

Assuming I have the Database structure with 2 colums + rowid column. Whenever i do this sql statement:
REPLACE INTO player (id,credits) VALUES ('123123',10);
However i seen for values that already exist,the data gets updated but the ROW ID gets incremented. Why is this so? Is it possible to keep the ROW ID consistent?
Thanks
with replace, the row gets deleted then reinserted, you'll more than likely have to do update
REPLACE stands for INSERT OR REPLACE, and practically, is an INSERT statement. You need to use UPDATE.

Resources