Forum Moderators: open

Message Too Old, No Replies

Default Values

         

bumpaw

5:58 pm on Oct 10, 2006 (gmt 0)

10+ Year Member



I have the following in a php script;

[3]CREATE TABLE ftz_feed (
category VARCHAR(50) DEFAULT 'Other Collections');[/3]

It creates a database and tables then opens and INSERTS the data from an .csv file that was produced with excel. Empty cells in the .csv file under category should populate with "Other Collections" I'm thinking. If I do an INSERT with phpmyadmin leaving the 'category' field blank it works, but not when loading the datafeed .csv file. I'm thinking that those cells in Excel that appear blank have some white space or something.

aspdaddy

9:31 am on Oct 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In SQL you need to miss out the field altogether in the INSERT list to use the default.

I would think you need to check for an empty value in the csv and write the INSERT differently, otherwise youll get an empty string or null value inserted.

Or, make the csv export write "Other Collection" if the field is empty and lose the default value.

HTH

spinnercee

12:47 pm on Oct 11, 2006 (gmt 0)

10+ Year Member



Maybe adding NOT NULL to the field definition would help (?).

with a .csv, all fields exist, so the possible "value" can be NULL or an empty string, neither of which will invoke the DEFAULT field value.

CREATE TABLE ftz_feed (
category VARCHAR(50) DEFAULT 'Other Collections' NOT NULL);

coopster

2:09 pm on Oct 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can also fire the <keyword> DEFAULT into that column.

CREATE TABLE ftz_feed (category VARCHAR(50) DEFAULT 'Other Collections');

INSERT INTO ftz_feed (category) VALUES(DEFAULT);

So if you have an empty column in your .csv file and you know you want it populated with the table's DEFAULT value you can specify the DEFAULT keyword.

bumpaw

6:17 pm on Oct 11, 2006 (gmt 0)

10+ Year Member



I tried coopster's idea since I had already tried adding NOT NULL. It is the same problem. I'm thinking like aspdaddy that the problem lies in the .csv file. My database is seeing those cells that look empty as not being actually empty and that is why it won't pick up the DEFAULT on INSERT. I'll have to try harder to get the .csv file right prior to import.

spinnercee

8:20 pm on Oct 11, 2006 (gmt 0)

10+ Year Member



There's really many, many ways to solve your issue:

Since you have PHP, you can make the script tweak the data before INSERTing really easiliy. Just read the .csv line by line [fopen(),fgets()], creating an INSERT statement for each one. That way, you can also tweak any other values that are not optimized for your table. It's typically bad practice to design a database table around a limited and "flat" intermediate structure like a .csv file.

Since you know what value the empty value causes [most likely a non-NULL ""], you could also perform a MySQL REPLACE on all the records that were not filled in after everything is imported. As a matter of fact, you don't really need to do anything at all, the blank field value can mean whatever you want it to implicitly -- the added benefit here is that the empty (default) values will sort first as well, and thus not be stuffed in the middle somewhere.

Your import is simple enough, unless you have a billion gazillion records, making several passes through the MySQL database to ensure data integrity is a small price to pay.