tags:

views:

83

answers:

4

Hello

I need your help please.

We have a PHP application running on MySQL, and we need to use PostgreSQL for a new customer.

the problem is that when we insert empty strings in a field of type numeric, we get an error.

I think we should actually use NULL instead of empty string.

However we would like to avoid having to recode all the SQL code of our application, my question is whether it is possible in the structure of a table (or elsewhere) to ensure that our numeric column accepts empty values? or convert them to NULL?

In the documentation I have seen that it is possible to define your own types, it is possible for example to create a type based on numeric and ensure that it accepts empty values? If yes how? I found no solution

Thank you

+5  A: 

the problem is that when we insert empty strings in a field of type numeric, we get an error.

That's correct, a string isn't a number. Not even an empty string.

I think we should actually use NULL instead of empty string.

That's correct, your current SQL is wrong. If your MySQL-configuration would use a better SQL_MODE, your queries will also fail on MySQL. Fix your code, it's by far the best option you have.

Hacking around by creating dummy datatypes is just a hack, it's not a solution for bad SQL. The problem is your SQL, not your database.

Frank Heikens
A: 

Thank you for your anwser Frank.

I admit that the SQL is bad.

For the moment i do not have time to rewrite all SQL code of the application.

I can accept a hack solution before to write a new version of the application.

Can we temporarily fix that with a hack ? please explain how.

Thank you

Esab75

Esab75
Just use/abuse varchar's. Creating datatypes and all their operators is just way too much work. But you will get into new trouble, even a + (plus) or - (minus) won't work! Fix your SQL, that's the problem you have. Use pg_query_params(), it's safe against SQL injection and handles NULL's very nice.
Frank Heikens
A: 

Thank you all for your anwsers.

So, if i understand, postgreSQL don't have any parameter or simple solution to allow inserting empty values in numeric fields ? that's right ?

@ Frank Heikens : I cannot use pg_query_params , we don't use parameters , i have to rewrite all the code of the application, no time no money for this for the moment.

if i use/abuse varchar like you say, the database will accept inserting non numeric values in my numeric fields, i just want to convert '' to NULL ON Insert/Update to get no database error.

@ FastAI :

try 1/3 : Maybe it can work, but i have to write stored procedures for my 200 tables. It's too much time.

try 2/3 : we have consider this, but like Frank said, we cannot know when it's real empty value, this is not simple. Sometimes we have INSERT INTO mytable (num, label) VALUES (10, ''Hello'') to insert 'Hello' into the field with quotes.

try 3/3 : Attach mysql tables on postgreSQL, I didn't undertand how to do this.

Thank you again

Esab75
"I cannot use pg_query_params , we don't use parameters" How do you prevent SQL injection?
Frank Heikens
The application is old and uses it's own functions to control input data and avoid this problem
Esab75
"and avoid this problem" Hmmmm, heard that before... Could you show us how?
Frank Heikens
A: 

Thank you again for your anwsers, and try to help us.

As we can see there is no simple/fast solution for this problem.

So we gonna see with our customer if we can change database to MySQL,Oracle or MSSQL.

We will correct the SQL code in a future version. Thank your for your advices.

Best regards.

Esab75
Oracle and MSSQL will show you the same problems, no DBMS should ever except illegal data. MySQL is the exception and is a major problem, as you have discovered. Even MySQL has nowadays a SQL_MODE to reject corrupt data. They woke up, it bit late, but they did.
Frank Heikens
The code works on MySQL, Oracle and MSSQL mister Heikens, give a try!One more time, I know and i completely agree that no DBMS should ever except illegal data.But how many times will you repeat this to me?i don't need you lecturing me with that neither to learn me how to protect code from sql injection or other, even if you are senior DBA..., i just asked politely if it's possible to do the same in postgreSQL.Thank you anyway.
Esab75