SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库。您不需要任何特殊的权限即可创建一个数据。
sqlite3 命令的基本语法如下:
1 |
<span class="pln">$sqlite3 </span><span class="typ">DatabaseName</span><span class="pun">.</span><span class="pln">db</span> |
通常情况下,数据库名称在 RDBMS 内应该是唯一的。
如果您想创建一个新的数据库 <testDB.db>,SQLITE3 语句如下所示:
1 2 3 4 5 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="typ">SQLite</span><span class="pln"> version </span><span class="lit">3.7</span><span class="pun">.</span><span class="lit">15.2</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">01</span><span class="pun">-</span><span class="lit">09</span> <span class="lit">11</span><span class="pun">:</span><span class="lit">53</span><span class="pun">:</span><span class="lit">05</span> <span class="typ">Enter</span> <span class="str">".help"</span> <span class="kwd">for</span><span class="pln"> instructions </span><span class="typ">Enter</span><span class="pln"> SQL statements terminated </span><span class="kwd">with</span><span class="pln"> a </span><span class="str">";"</span><span class="pln"> sqlite</span><span class="pun">></span> |
上面的命令将在当前目录下创建一个文件 testDB.db。该文件将被 SQLite 引擎用作数据库。如果您已经注意到 sqlite3 命令在成功创建数据库文件之后,将提供一个 sqlite> 提示符。
一旦数据库被创建,您就可以使用 SQLite 的 .databases 命令来检查它是否在数据库列表中,如下所示:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">databases seq name file </span><span class="pun">---</span> <span class="pun">---------------</span> <span class="pun">----------------------</span> <span class="lit">0</span><span class="pln"> main </span><span class="pun">/</span><span class="pln">home</span><span class="pun">/</span><span class="pln">sqlite</span><span class="pun">/</span><span class="pln">testDB</span><span class="pun">.</span><span class="pln">db</span> |
您可以使用 SQLite .quit 命令退出 sqlite 提示符,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">quit $</span> |
您可以在命令提示符中使用 SQLite .dump 点命令来导出完整的数据库在一个文本文件中,如下所示:
1 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="pun">.</span><span class="kwd">dump</span> <span class="pun">></span><span class="pln"> testDB</span><span class="pun">.</span><span class="pln">sql</span> |
上面的命令将转换整个 testDB.db 数据库的内容到 SQLite 的语句中,并将其转储到 ASCII 文本文件 testDB.sql 中。您可以通过简单的方式从生成的 testDB.sql 恢复,如下所示:
1 |
<span class="pln">$sqlite3 testDB</span><span class="pun">.</span><span class="pln">db </span><span class="pun"><</span><span class="pln"> testDB</span><span class="pun">.</span><span class="pln">sql</span> |
此时的数据库是空的,一旦数据库中有表和数据,您可以尝试上述两个程序。现在,让我们继续学习下一章。
from:http://www.runoob.com/sqlite/sqlite-create-database.html