1、SQLite简介
SQLite第一个Alpha版本诞生于2000年5月. 至今已经有4个年头了. 而在今年的5月SQLite也迎来了一个新的里程: SQLite 3.
下面是访问SQLite官方网站: http://www.sqlite.org/ 时第一眼看到关于SQLite的特性.
1. ACID事务 2. 零配置 – 无需安装和管理配置 3. 储存在单一磁盘文件中的一个完整的数据库 4. 数据库文件可以在不同字节顺序的机器间自由的共享 5. 支持数据库大小至2TB 6. 足够小, 大致3万行C代码, 250K 7. 比一些流行的数据库在大部分普通数据库操作要快 8. 简单, 轻松的API 9. 包含TCL绑定, 同时通过Wrapper支持其他语言的绑定 10. 良好注释的源代码, 并且有着90%以上的测试覆盖率 11. 独立: 没有额外依赖 12. Source完全的Open, 你可以用于任何用途, 包括出售它 13. 支持多种开发语言,C, PHP, Perl, Java, ASP.NET,Python
2、SQLite类型
SQLite的数据类型
首先你会接触到一个让你惊讶的名词: Typelessness(无类型). 对! SQLite是无类型的. 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中, 无论这列声明的数据类型是什么(只有在一种情况下不是, 稍后解释). 对于SQLite来说对字段不指定类型是完全有效的. 如:
Create Table ex1(a, b, c);
诚然SQLite允许忽略数据类型, 但是仍然建议在你的Create Table语句中指定数据类型. 因为数据类型对于你和其他的程序员交流, 或者你准备换掉你的数据库引擎. SQLite支持常见的数据类型, 如:
CREATE TABLE ex2 ( a VARCHAR(10), b NVARCHAR(15), c TEXT, d INTEGER, e FLOAT, f BOOLEAN, g CLOB, h BLOB, i TIMESTAMP, j NUMERIC(10,5) k VARYING CHARACTER (24), l NATIONAL VARYING CHARACTER(16) );
前面提到在某种情况下, SQLite的字段并不是无类型的. 即在字段类型为”Integer Primary Key”时.
3、如何连接SQLite?
用PHP操作sqlite数据库
a、 如何连接sqlite数据库?
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) { select * from sqlite_master; echo "数据库连接成功!n"; } else { die($sqliteerror); }
b、 如何列出数据库中所有的表?
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) { $result = sqlite_array_query($db, 'select * from sqlite_master;'); foreach ($result as $entry) { echo 'talbe name='.$entry['name']."n"; echo 'sql='.$entry['sql']."n"; echo " -----------------------------------------------------------------"; }
sqlite_close($db); } else { die($sqliteerror); }
c、 对sqlite数据库的查询,以及结果集的显示
if ($db = sqlite_open('mysqlitedb', 0666, $sqliteerror)) { $result = sqlite_array_query($db, 'select name, email from user ', SQLITE_ASSOC); echo "user表查询结果:n"; echo " n name email n"; foreach ($result as $entry) { echo ' '.$entry['name']." " $entry['email']." n"; } echo ' '; sqlite_close($db); } else { die($sqliteerror); }
d、 数据库对象记录的增加、删除、修改
sqlite_query($db, "INSERT INTO user VALUES('user".$i."'" ",'user".$i."@hichina.com')"); sqlite_query($db, "delete from user where user=’user99’"); sqlite_query($db, 'UPDATE user SET email="lilz@hichina.com" where name="user1"');
4、SQLite的管理
管理工具也有不少,建议您使用sqlitemanager,用法酷像phpmyadmin.
可以到 http://sqlitemanager.sourceforge.net/下载
About SQLite SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. Features include:
Transactions are atomic, consistent, isolated, and durable (ACID) even after system crashes and power failures. Zero-configuration - no setup or administration needed. Implements most of SQL92. (Features not supported) A complete database is stored in a single disk file. Database files can be freely shared between machines with different byte orders. Supports databases up to 2 terabytes (241 bytes) in size. Sizes of strings and BLOBs limited only by available memory. Small code footprint: less than 250KiB fully configured or less than 150KiB with optional features omitted. Faster than popular client/server database engines for most common operations. Simple, easy to use API. TCL bindings included. Bindings for many other languages available separately. Well-commented source code with over 95% test coverage. Self-contained: no external dependencies. Sources are in the public domain. Use for any purpose.
The SQLite distribution comes with a standalone command-line access program (sqlite) that can be used to administer an SQLite database and which serves as an example of how to use the SQLite library.
(参考链接: http://www.517sou.net/blogview.asp?logID=846)
|