2012-01-14 12 views
7

Sto usandoPerché tutti i tipi di dati sono restituiti come stringhe con sqlite3 fetchAll (PDO: FETCH_ASSOC) con PHP?

$rows = $statement->fetchAll(PDO::FETCH_ASSOC); 

per ottenere tutte le righe del mio tavolo.

Lo schema è definito come "id INTEGER PRIMARY KEY, Testo titolo, anno intero, reale prezzo"

Una fila da lui risultati del fetchAll è

array(4) { 
    [0]=> 
    string(1) "1" 
    [1]=> 
    string(14) "The Dark Night" 
    [2]=> 
    string(4) "2008" 
    [3]=> 
    string(5) "19.95" 
    } 

Perché sono tutti i tipi di dati essere restituito come stringhe? Voglio che vengano restituiti come definito nello schema. Capisco la natura "senza caratteri" di SQLite, ma definiscono i tipi di dati limitati come TEXT, INTEGER e REAL. Come posso ottenere i dati da restituire con i tipi di dati specificati? Non voglio scorrere tutte le righe e convertirlo con PHP - sembra solo che sia troppo lento.

The Complete Codice test come segue:

<? 
class sqlite_test { 
    function __construct() 
    { 
     $this->dbase_filename = "test.sqlite"; 
     $this->init_dbase(); 

    } 

    function init_dbase() 
    { 
     $this->pdo = new PDO("sqlite:".$this->dbase_filename); 
    } 

    function open_table() 
    { 
     $table = "dvds"; 

     if (! ($test_to_see_if_table_exists = $this->pdo->query("SELECT 1 from $table"))) { 
      $schema = "id INTEGER PRIMARY KEY, title TEXT, year INTEGER, price REAL"; 
      $query = "CREATE TABLE $table ($schema)"; 

      $this->pdo->exec($query); 
     } 

     return $test_to_see_if_table_exists; 
    } 

    function add_test_records() 
    { 
     $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Dark Night", 2008, 19.95)'; 
     $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "The Wizard of Oz", 1939, 9.95)'; 
     $query[]='INSERT INTO dvds (id, title, year, price) VALUES (null, "Jaws", 1977, 6.95)'; 

     $this->pdo->exec(join(';', $query)); 
    } 

    function dump_test_records() 
    { 
     $query = "SELECT * FROM dvds"; 

     if ($statement = $this->pdo->prepare($query)) { 
      $statement->execute(); 

      $rows = $statement->fetchAll(PDO::FETCH_ASSOC); 

      echo "<pre>"; 
      var_dump($rows); 
      echo "</pre>"; 
     } 
    } 

    function main() 
    { 
     if (! $this->open_table()) { 
      $this->add_test_records(); 
     } 

     $this->dump_test_records(); 
    } 
} 

$test = new sqlite_test(); 
$test->main(); 

risposta

3

Questo non ha nulla a che fare con SQLite; tutti i record verranno restituiti come raccolte di stringhe indipendentemente da ciò che si usa per interrogare un database. È solo la natura degli adattatori/driver di database usati da PHP.

Se si desidera che i valori siano dei tipi desiderati, è necessario eseguire il cast manualmente.

+0

Devo dire, questa è una dichiarazione molto triste sul design di PDO. Ho lavorato con il driver PDO di Microsoft per SQL Server e si comporta esattamente allo stesso modo: tutti i tipi di dati vengono restituiti come stringhe e interrogando i vari indici disponibili tramite getColumnMeta restituisce invariabilmente "string" come tipo di dati indipendentemente dal tipo di dati in il database di origine. –

+1

Questo è vero. Ma il driver SQLITE3 nativo funziona diversamente. Qui non devi eseguire il cast manualmente. I tipi sono così come sono memorizzati in sqlite3. Come rowid = (int), text = (string) ecc ... – AppGeer

Problemi correlati