Leser: 21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/perl use 5.010; use warnings; use strict; use DBI; my $table = 'null_value_table'; open my $fh, '>', $table or die $!; while ( my $row = <DATA> ) { print $fh $row; } close $fh or die $!; my $dbh = DBI->connect( "DBI:CSV:" ); my $sth; say "\nselect all\n"; $sth = $dbh->prepare( "SELECT * FROM $table" ); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { say "@row"; } say "\n\n\nselect where col1 = Y\n"; $sth = $dbh->prepare( "SELECT * FROM $table WHERE col1 = 'Y'" ); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { say "@row"; } say "\n\n\nselect where col1 <> Y\n"; $sth = $dbh->prepare( "SELECT * FROM $table WHERE col1 <> 'Y'" ); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { say "@row"; } $dbh->disconnect; __DATA__ col0,col1 1,Y 2,N 3,NULL 4,undef 5,0 6,'' 7,
Gast
Quotecsv_null
With this option set, all new statement handles will set always_quote and blank_is_undef in the CSV parser and writer, so it knows how to distinquish between the empty string and undef or NULL. You cannot reset it with a false value. You can pass it to connect, or set it later:
QuoteThe DBI is a database access module for the Perl programming language. It defines a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used.
QuoteWarum wird in meinem Skript in der dritte Ausgabe ( where col1 <> Y ) alles außer 'Y' ausgegeben, warum gibt es keinen UNKNOWN Status?
QuoteIn relational operations, unlike normal binary, there are THREE states that a operation can result in, TRUE, FALSE, and UNKNOWN. [...]
The UNKNOWN state occurs for any logical operation where the result cannot be evaluated as either TRUE or FALSE, and is connected with the NULL concept.
A NULL in a SQL table, can be regarded as a placeholder for missing information, its not a VALUE as such, just a marker to indicate the lack of information.
1 2
my $ref = [ [...], [...] ]; $dbh->do( "CREATE TEMP TABLE $table AS IMPORT( ? )", {}, $ref );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/perl use 5.010; use warnings; use strict; use DBI; my $dbh = DBI->connect( "DBI:CSV:", undef, undef, { RaiseError => 1, csv_null => 1, } ); # csv_null => 0 my $table = 'Table1.csv'; my( $sth, $sql ); sub output { my $sql = shift; say $sql; $sth = $dbh->prepare( $sql ); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { say DBI::neat_list( \@row ) } say ""; } $dbh->do( "DROP TABLE IF EXISTS $table" ); $dbh->do( "CREATE TABLE $table ( id INT, nachname CHAR(64), vorname CHAR(64) )" ); $sth = $dbh->prepare("INSERT INTO $table( id, nachname, vorname ) VALUES (?,?,?)"); $sth->execute( 1, 'Lohan', 'Lindsay' ); $sth->execute( 2, '', '' ); $sth->execute( 3, undef, undef ); $sql = "SELECT * FROM $table"; output( $sql ); $sql = "SELECT * FROM $table WHERE vorname = 'Lindsay'"; output( $sql ); $sql = "SELECT * FROM $table WHERE vorname <> 'Lindsay'"; output( $sql ); $dbh->disconnect;
Quoteid,nachname,vorname
1,Lohan,Lindsay
2,,
3,,
1
2
3
4
5
6
7
8
9
10
11
SELECT * FROM Table1.csv
'1', 'Lohan', 'Lindsay'
'2', '', ''
'3', '', ''
SELECT * FROM Table1.csv WHERE vorname = 'Lindsay'
'1', 'Lohan', 'Lindsay'
SELECT * FROM Table1.csv WHERE vorname <> 'Lindsay'
'2', '', ''
'3', '', ''
1
2
3
4
5
6
7
8
9
10
SELECT * FROM Table1.csv
'1', 'Lohan', 'Lindsay'
'2', '', ''
'3', undef, undef
SELECT * FROM Table1.csv WHERE vorname = 'Lindsay'
'1', 'Lohan', 'Lindsay'
SELECT * FROM Table1.csv WHERE vorname <> 'Lindsay'
'2', '', ''
1 2 3 4 5 6 7 8 9 10
my $nn; my $vn = Paul; my $array_ref = [ [ qw( id nachname vorname ) ], [ 1, 'Lohan', 'Lindsay' ], [ 2, undef, 'Karl' ], # gut [ 3, $nn, $vn ], # auch gut [ 4, ,'Peter' ], # nicht gut ]; $dbh->do( "CREATE TEMP TABLE $table AS import( ? )", {}, $array_ref );