SQLite 教程
1. SQLite 教程 2. SQLite 简介 3. SQLite 安装 4. SQLite 命令 5. SQLite 语法 6. SQLite 数据类型 7. SQLite 创建数据库 8. SQLite 附加数据库 9. SQLite 分离数据库 10. SQLite 创建表 11. SQLite 删除表 12. SQLite Insert 语句 13. SQLite Select 语句 14. SQLite 运算符 15. SQLite 表达式 16. SQLite Where 子句 17. SQLite AND/OR 运算符 18. SQLite Update 语句 19. SQLite Delete 语句 20. SQLite Like 子句 21. SQLite Glob 子句 22. SQLite Limit 子句 23. SQLite Order By 24. SQLite Group By 25. SQLite Having 子句 26. SQLite Distinct 关键字 27. SQLite PRAGMA 28. SQLite 约束 29. SQLite Join 30. SQLite Unions 子句 31. SQLite NULL 值 32. SQLite 别名 33. SQLite 触发器 34. SQLite 索引 35. SQLite Indexed By 36. SQLite Alter 命令 37. SQLite Truncate Table 38. SQLite 视图 39. SQLite 事务 40. SQLite 子查询 41. SQLite Autoincrement 42. SQLite 注入 43. SQLite Explain 44. SQLite Vacuum 45. SQLite 日期 & 时间 46. SQLite 常用函数 47. SQLite – C/C++ 48. SQLite – Java 49. SQLite – PHP 50. SQLite – Perl 51. SQLite – Python

SQLite 创建数据库

SQLite 创建数据库

SQLite 的 sqlite3 命令被用来创建新的 SQLite 数据库。您不需要任何特殊的权限即可创建一个数据。

语法

sqlite3 命令的基本语法如下:


$sqlite3 DatabaseName.db

通常情况下,数据库名称在 RDBMS 内应该是唯一的。

实例

如果您想创建一个新的数据库 <testDB.db>,SQLITE3 语句如下所示:


$sqlite3 testDB.db

SQLite version 3.7.15.2 2013-01-09 11:53:05

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

上面的命令将在当前目录下创建一个文件 testDB.db。该文件将被 SQLite 引擎用作数据库。如果您已经注意到 sqlite3 命令在成功创建数据库文件之后,将提供一个 sqlite> 提示符。

一旦数据库被创建,您就可以使用 SQLite 的 .databases 命令来检查它是否在数据库列表中,如下所示:


sqlite>.databases

seq  name             file

---  ---------------  ----------------------

0    main             /home/sqlite/testDB.db

您可以使用 SQLite .quit 命令退出 sqlite 提示符,如下所示:


sqlite>.quit

$

.dump 命令

您可以在命令提示符中使用 SQLite .dump 点命令来导出完整的数据库在一个文本文件中,如下所示:


$sqlite3 testDB.db .dump > testDB.sql

上面的命令将转换整个 testDB.db 数据库的内容到 SQLite 的语句中,并将其转储到 ASCII 文本文件 testDB.sql 中。您可以通过简单的方式从生成的 testDB.sql 恢复,如下所示:


$sqlite3 testDB.db < testDB.sql

此时的数据库是空的,一旦数据库中有表和数据,您可以尝试上述两个程序。现在,让我们继续学习下一章。