一个小时内学习 SQLite 数据库

来源:互联网 发布:双11数据直播 编辑:程序博客网 时间:2024/05/16 04:31

点击打开链接

1. 介绍

SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。

2. 安装

SQLite on Windows

  1. 进入 SQL 下载页面:http://www.sqlite.org/download.html
  2. 下载 Windows 下的预编译二进制文件包:
    • sqlite-shell-win32-x86-<build#>.zip
    • sqlite-dll-win32-x86-<build#>.zip
    注意: <build#> 是 sqlite 的编译版本号
  3. 将 zip 文件解压到你的磁盘,并将解压后的目录添加到系统的 PATH 变量中,以方便在命令行中执行 sqlite 命令。
  4. 可选: 如果你计划发布基于 sqlite 数据库的应用程序,你还需要下载源码以便编译和利用其 API
    • sqlite-amalgamation-<build#>.zip

SQLite on Linux

在 多个 Linux 发行版提供了方便的命令来获取 SQLite:

view source
print?
1/* For Debian or Ubuntu /*
2$ sudoapt-getinstallsqlite3 sqlite3-dev
3  
4/* For RedHat, CentOS, or Fedora/*
5$ yum installSQLite3 sqlite3-dev

SQLite on Mac OS X

如果你正在使用 Mac OS 雪豹或者更新版本的系统,那么系统上已经装有 SQLite 了。

3. 创建首个 SQLite 数据库

现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为  test.db 的数据库。

view source
print?
1sqlite3 test.db

创建表:

view source
print?
1sqlite> create table mytable(idinteger primary key, value text);
2  
32 columns were created.

该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。

注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据库保存到磁盘中,否则数据库不会被创建。

接下来往表里中写入一些数据:

view source
print?
1sqlite> insertintomytable(id, value)values(1,'Micheal');
2sqlite> insertintomytable(id, value)values(2,'Jenny');
3sqlite> insertintomytable(value)values('Francis');
4sqlite> insertintomytable(value)values('Kerk');

查询数据:
view source
print?
1sqlite> select*fromtest;
21|Micheal
32|Jenny
43|Francis
54|Kerk
设置格式化查询结果:
view source
print?
1sqlite> .mode column;
2sqlite> .header on;
3sqlite> select*fromtest;
4id          value
5----------- -------------
61           Micheal
72           Jenny
83           Francis
94           Kerk

.mode column 将设置为列显示模式,.header 将显示列名。

修改表结构,增加列:

view source
print?
1sqlite> altertablemytableaddcolumnemail textnot null '' collatenocase;;

创建视图:
view source
print?
1sqlite> createviewnameviewasselect*frommytable;
创建索引:
view source
print?
1sqlite> createindextest_idxonmytable(value);

4. 一些有用的 SQLite 命令

显示表结构:
sqlite> .schema [table]

获取所有表和视图:
sqlite > .tables

获取指定表的索引列表:
sqlite > .indices [table ]

导出数据库到 SQL 文件:
sqlite > .output [filename ]
sqlite > .dump
sqlite > .output stdout

从 SQL 文件导入数据库:
sqlite > .read [filename ]

格式化输出数据到 CSV 格式:
sqlite >.output [filename.csv ]
sqlite >.separator ,
sqlite > select * from test;
sqlite >.output stdout

从 CSV 文件导入数据到表中:
sqlite >create table newtable ( id integer primary key, value text );
sqlite >.import [filename.csv ] newtable

备份数据库:
/* usage: sqlite3 [database] .dump > [filename] */
sqlite3 mytable.db .dump > backup.sql

恢复数据库:
/* usage: sqlite3 [database ] < [filename ] */
sqlite3 mytable.db < backup.sql 

php例子
<?php


class CPhpSqlite3 {


    private static $_instance = null;
    private static $_sqlite3_instance = null;
    //阻止用户复制对象实例
    public function __clone(){}
    
    private function __construct(){  
    
        $basedir = dirname(Yii::app()->BasePath);
        $dir = $basedir.'/db/vm-logs.db';
        //echo $dir;
        self::$_sqlite3_instance = new SQLite3($dir); 
        
    }
    
    public static function getInstance() {
        
        if (is_null(self::$_instance)) {
        
          self::$_instance = new CPhpSqlite3();


        }
        return self::$_instance;
    } 
    
    


    public function __search($sql){


        $result = self::$_sqlite3_instance->query($sql);
        while($row = $result -> fetchArray(SQLITE3_ASSOC)){
            $ret[] = $row;
            //print_r($row);
        }        
        return $ret;


    }
    
    function __insert($sql){
        self::$_sqlite3_instance -> exec($sql);
        $id = self::$_sqlite3_instance -> lastInsertRowID();
        return $id;
    }


    function __delete($sql){
        self::$_sqlite3_instance -> exec($sql);
    }


    function __update($sql){
        self::$_sqlite3_instance -> exec($sql);
    }


    public function __close()
    {
        self::$_sqlite3_instance->close();
    }
    


}


?>

http://www.yiibai.com/sqlite/sqlite_php.html


http://blog.leniy.org/php-sqlite.html


http://www.blogjava.net/myqiao/archive/2011/07/13/354298.html


http://www.oschina.net/question/12_53183



0 0
原创粉丝点击