- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using NULL Values in Perl Database Operation
Undefined values, or undef, are used to indicate NULL values in Perl’s Database Operations. You can insert and update columns with a NULL value as you would a non-NULL value. These examples insert and update the column age with a NULL value −
$sth = $dbh->prepare(qq { INSERT INTO TEST_TABLE (FIRST_NAME, AGE) VALUES (?, ?) }); $sth->execute("Joe", undef);
Here qq{} is used to return a quoted string to prepare API. However, care must be taken when trying to use NULL values in a WHERE clause. Consider −
SELECT FIRST_NAME FROM TEST_TABLE WHERE age = ?
Binding an undef (NULL) to the placeholder will not select rows, which have a NULL age! At least for database engines that conform to the SQL standard. Refer to the SQL manual for your database engine or any SQL book for the reasons for this. To explicitly select NULLs you have to say "WHERE age IS NULL".
A common issue is to have a code fragment handle a value that could be either defined or undef (non-NULL or NULL) at runtime. A simple technique is to prepare the appropriate statement as needed, and substitute the placeholder for non-NULL cases −
$sql_clause = defined $age? "age = ?" : "age IS NULL"; $sth = $dbh->prepare(qq { SELECT FIRST_NAME FROM TEST_TABLE WHERE $sql_clause }); $sth->execute(defined $age ? $age : ());
- Related Articles
- Database INSERT Operation in Perl
- Database READ Operation in Perl
- Database UPDATE Operation in Perl
- Database DELETE Operation in Perl
- Database INSERT Operation in Python
- Database READ Operation in Python
- Database Update Operation in Python
- Database DELETE Operation in Python
- How to create Database Connection in Perl?
- Setting null values with an HTML using AngularJS.
- Python Pandas – Check for Null values using notnull()
- Temporary Values via local() in Perl
- Ignore null values in MongoDB documents
- Extracting Keys and Values from Hash in Perl
- Using GET Methods in Perl
