English 中文(简体)
Metadata for columns in SQLite v2.8 (PHP5)
原标题:

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)?

sqlite_fetch_column_types (OO: $db->fetchColumnTypes) only gets column name and datatype:
http://dk.php.net/manual/en/function.sqlite-fetch-column-types.php

SQLITE_MASTER has the info - but not as a variable. Example:
SELECT name FROM SQLITE_MASTER;

... SQLITE_MASTER only outputs an array with this structure (v2.8):

[type] => table
[name] => foo
[tbl_name] => foo
[rootpage] => 3
[sql] => CREATE TABLE foo ( id INTEGER PRIMARY KEY, name CHAR(255) )

(And what is "rootpage"?)

最佳回答

I had to do this to implement a describeTable() method for SQLite when I worked on Zend Framework. SQLite does not have any way to get very detailed information about metadata.

What I did was to run the following SQLite query:

PRAGMA tableinfo( <tablename> );

See http://www.sqlite.org/pragma.html, under the heading "Pragmas to query the database schema."

The result set returned from this query has columns for:

  • column position (integer)
  • column name (string)
  • data type (string)
  • nullable (0) vs. not null (1)
  • default value (string)
  • primary key (1)

You can view the PHP code by downloading Zend Framework and looking in class Zend_Db_Adapter_Pdo_Sqlite, method describeTable(). You can also view the source online via the code repository browser at framework.zend.com (although it is frequently not working).

FWIW, this is not like mysql_fetch_field(). That method returns metadata about a result set, which may not be the same thing as metadata about a table.

问题回答

Example code snippet that worked for me ... to illustrate Bill Karwin s solution (it s not an attempt to answer my own question!)

$db = new SQLiteDatabase("db.sqlite2");  
$rs = $db->arrayQuery("PRAGMA table_info(  my_table  );");
print_r($rs);




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签