qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          一個小時內學習SQLite數據庫

            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 的編譯版本號
            將 zip 文件解壓到你的磁盤,并將解壓后的目錄添加到系統的 PATH 變量中,以方便在命令行中執行 sqlite 命令。
            可選: 如果你計劃發布基于 sqlite 數據庫的應用程序,你還需要下載源碼以便編譯和利用其 API
            sqlite-amalgamation-<build#>.zip
            SQLite on Linux
            在 多個 Linux 發行版提供了方便的命令來獲取 SQLite:
            /* For Debian or Ubuntu /*
            $ sudo apt-get install sqlite3 sqlite3-dev
            /* For RedHat, CentOS, or Fedora/*
            $ yum install SQLite3 sqlite3-dev
            SQLite on Mac OS X
            如果你正在使用 Mac OS 雪豹或者更新版本的系統,那么系統上已經裝有 SQLite 了。
            3. 創建首個 SQLite 數據庫
            現在你已經安裝了 SQLite 數據庫,接下來我們創建首個數據庫。在命令行窗口中輸入如下命令來創建一個名為 test.db 的數據庫。
            sqlite3 test.db
            創建表:
            sqlite> create table mytable(id integer primary key, value text);
            2 columns were created.
            該表包含一個名為 id 的主鍵字段和一個名為 value 的文本字段。
            注意: 最少必須為新建的數據庫創建一個表或者視圖,這么才能將數據庫保存到磁盤中,否則數據庫不會被創建。
            接下來往表里中寫入一些數據:
            sqlite> insert into mytable(id, value) values(1, 'Micheal');
            sqlite> insert into mytable(id, value) values(2, 'Jenny');
            sqlite> insert into mytable(value) values('Francis');
            sqlite> insert into mytable(value) values('Kerk');
            查詢數據:
            sqlite> select * from test;
            1|Micheal
            2|Jenny
            3|Francis
            4|Kerk
            設置格式化查詢結果:
          sqlite> .mode column;
          sqlite> .header on;
          sqlite> select * from test;
          id          value
          ----------- -------------
          1           Micheal
          2           Jenny
          3           Francis
          4           Kerk


           .mode column 將設置為列顯示模式,.header 將顯示列名。
            修改表結構,增加列:
            sqlite> alter table mytable add column email text not null '' collate nocase;;
            創建視圖:
            sqlite> create view nameview as select * from mytable;
            創建索引:
            sqlite> create index test_idx on mytable(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

          posted on 2014-07-18 10:01 順其自然EVO 閱讀(168) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南安市| 民丰县| 镇坪县| 霍邱县| 青阳县| 崇礼县| 财经| 响水县| 留坝县| 荆州市| 澄迈县| 宁明县| 托里县| 绩溪县| 武义县| 长沙县| 栖霞市| 通化市| 塘沽区| 康马县| 平度市| 红安县| 浏阳市| 东乡县| 怀仁县| 牟定县| 淅川县| 疏附县| 黎平县| 罗定市| 海城市| 宣汉县| 衡阳县| 陇西县| 青田县| 丽江市| 甘泉县| 且末县| 磐安县| 德化县| 五大连池市|