当前位置:数据库 > SQLite >>

PHP中的SQlite数据库应用介绍

答案:  相信PHP的开发人员一定不会对SQLite感到陌生,因为在PHP5中已经集成了这个轻巧的内嵌式数据库产品。SQLite在操作语句上更类似关系型数据库的产品。绝大多数标准的SQL92语句SQLite都能支持。简单的说,嵌入式数据库是一种具备了基本数据库特性的数据文件。嵌入式数据库与其它数据库产品的区别是,前者是程序驱动式,而后者是引擎响应式。嵌入式数据库的一个很重要的特点是它们的体积非常小,编译后的产品也不过几十K。这不但对桌面程序的数据存储方案是一个很好的选择,也使得它们可以应用到一些移动设备上。同时,很多嵌入式数据库在性能上也优于其它数据库,所以在高性能的应用上也常见嵌入式数据库的身影。SQLite的版权允许无任何限制的应用,包括商业性的产品.下面就介绍一下SQLite数据库,及其在PHP中的一些应用!

  1. PHP4下的安装  (php5已经集成,无需安装):
       参见 “怎样扩展PHP的其它功能
       此时你已经无需再安装任何东西了, 而你也已经完全支持SQLite了, 一个简单, 快速, 可靠的数据库.
  2.SQlite数据库几个重要的内建语法:
 create table 表名 (字段名1 字段1类型,字段名2 字段2类型);//创建新表
     例:create table test (id int ,name char(20)) //创建表test,拥有两个字段 id 和 name 他们别为整型和定长字符型
 drop table 表名 ;//删除表
     例:drop table test;//删除test表
 create index 索引名 on 表名(字段名); //为给定表或视图创建索引。
     例:create index main on test(id); //为test表以id字段创建索引,索引名为main
 drop index 索引名;//删除索引
     例:drop index main //删除main索引
 insert into 表名 [(字段列表)]values (与字段相对的值列表);//向表中插入新行.[]中为可选内容
     例:insert into test values(1,'hfly2005')|insert into test (id,name) values (1,'hfly2005');
 delete from 表名 [where 删除条件]//无条件限定的情况下删除表中所有数据,[]中为可选内容
     例:delete from test where name='hfly2005';//删除name等于hfly2005的行
 select *[|字段列表]from 表名 [ where 选择条件]//从表中选择所有或符合条件的数据。
     例:select * from test //从test表中选出所有内容
 replace into 表名 (字段列表) values (与字段相对的值列表) //类似insert的功能
     例:replace into test (name) values('hfly2005')//插入新行,id的值为空,name的值为hfly2005
 alert  table  //(3.2.0以上版本支持)修改已存在表的结构  ,支持rename table 和 add columngj 但是不支持 drop column alert colume 等
      有关SQLite内建语法的更多内容请访问:http://www.sqlite.org/
  3.SQlite的数据类型:
       参见:http://www.sqlite.org/datatype3.html
        没有Auto Increment(自增)字段, 但是SQLite其实是支持Auto Increment的, 即在将该字段设置为” INTEGER PRIMARY KEY”的时候.
  4.php中相关SQLite函数:
 sqlite_array_query —— 发送一条 SQL 查询,并返回一个数组。
 sqlite_busy_timeout —— 设置超时时间(busy timeout duration),或者频繁的用户失去权限(disable busy handlers)。
 sqlite_changes —— 返回被最新的SQL 查询(changed by the most recent SQL statement)改变的行数。
 sqlite_close —— 关闭一个打开的SQLite数据库。
 sqlite_column —— 在当前的行中取得一列(a column from the current row of a result set)。
 sqlite_create_aggregate —— Register an aggregating UDF for use in SQL statements。
 sqlite_create_function —— Registers a "regular" User Defined Function for use in SQL statements。
 sqlite_current —— 在返回的数组中取得当前的行(the current row from a result set as an array)。
 sqlite_error_string —— 返回错误代码的原始描述(the textual description of an error code)。
 sqlite_escape_string —— 释放一个用于查询的字符串(Escapes a string for use as a query parameter)。
 sqlite_fetch_array —— 取得下一行并设置成一个数组(the next row from a result set as an array)。
 sqlite_fetch_single —— 取得第一列并设置成一个字符串(Fetches the first column of a result set as a string)。
 sqlite_fetch_string —— sqlite_fetch_single()的别名。
 sqlite_field_name —— 取得结果中指定字段的字段名。
 sqlite_has_more —— 返回是否有更多可用的行(whether or not more rows are available)。
 sqlite_last_error —— 返回数据库的最新的错误代码(the error code of the last error for a database)。
 sqlite_last_insert_rowid —— 返回最新插入的行的行号(the most recently inserted row)。
 sqlite_libencoding —— 返回SQLite库(SQLite library)的编码(encoding)。
 sqlite_libversion —— 返回SQLite库(SQLite library)的版本。
 sqlite_next —— 返回下一行的行号。
 sqlite_num_fields —— 取得结果集中字段的数目。
 sqlite_num_rows —— 取得结果集中行的数目。
 sqlite_open —— 打开一个SQLite数据库。如果文件不存在则尝试创建之。
 sqlite_popen —— 用永久连接的方式打开一个SQLite数据库。如果文件不存在则尝试创建之。
 sqlite_query —— 发送一条 SQL 查询,并返回一个结果句柄(a result handle)。
 sqlite_rewind —— 倒回第一行(Seek to the first row number)。
 sqlite_seek —— 在缓存结果中查找特定的行号(Seek to a particular row number of a buffered result set)。
 sqlite_udf_decode_binary —— Decode binary data passed as parameters to an UDF。
 sqlite_udf_encode_binary —— Encode binary data before returning it from an UDF。
 sqlite_unbuffered_query —— 发送一条 SQL 查询,并不获取和缓存结果的行。
 
  5.第一个sqlite程序:

   $db=sqlite_open("db.sqlite"); //打开db.sqlite数据库,如果不存在则尝试创建。
 sqlite_query($db,"drop table test");
 sqlite_query($db,"create table test (id INTEGER PRIMARY KEY,name text);"); //创建test表,id字段为自动递增主键
 sqlite_query($db,"insert into test (name) values('hello');"); //插入一行内容
 sqlite_query($db,"insert into test (name) values('world');");//插入一行内容
 $result=sqlite_query($db,"select * from test"); //取得test表的所有内容
 while($row=sqlite_fetch_array($result)) { //通过while循环表中所有内容
   print "
";
   print_r($row); /*返回下面的内容
   Array
   (
    [0] => 1
    [id] => 1
    [1] => hello
    [name] => hello
   )
   Array
   (
    [0] => 2
    [id] => 2
    [1] => world
    [name] => world
   )
   */
   print "
";
 }
 sqlite_close($db);//关闭数据库的连接
 ?>

   参考文章:
      (1):http://www.zend.com/php5/articles/php5-sqlite.php
      (2):http://www.linuxhero.com/html/showarticle.php?action=showarticle&id=3777
      (3):http://www.loveunix.net/bbs/index.php?showtopic=35051
      (4):http://art.055.cn/htmldata/2005_01/229/138/96/article_85203_1.html

   详细的SQL支持可以访问: PHP+SQLITE制作简单的视频点播程序
下一个:在VC6.0中使用C++访问sqlite数据库

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,