database 我们如何保存在 sqlite3 中创建的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28024810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how we can save database created in sqlite3
提问by erdarshp
I am new to database. I am trying to create a database and table in it. but unable to save and open again after exiting from sqlite. I am using sqlite3 3.6.20 on centOS, when i will enter following command
我是数据库的新手。我正在尝试在其中创建一个数据库和表。但退出sqlite后无法保存并再次打开。我在 centOS 上使用 sqlite3 3.6.20,当我输入以下命令时
.save ex1.db or .open ex1.db
it will print following error message.
它将打印以下错误消息。
Error: unknown command or invalid arguments: "save". Enter ".help" for help
Error: unknown command or invalid arguments: "open". Enter ".help" for help
and when Print .help it wont show any command related to save and open existing database. thanks in advance.
当 Print .help 它不会显示任何与保存和打开现有数据库相关的命令。提前致谢。
回答by Mike Sherrill 'Cat Recall'
I am trying to create a database and table in it. but unable to save and open again after exiting from sqlite.
我正在尝试在其中创建一个数据库和表。但退出sqlite后无法保存并再次打开。
You don't need to save. Each transaction writes to disk. (More or less.)
你不需要保存。每个事务写入磁盘。(或多或少。)
To create the database "test.sl3", you can do this. (From the command line. Programs work about the same way.)
要创建数据库“test.sl3”,您可以这样做。(从命令行。程序的工作方式大致相同。)
$ sqlite3 test.sl3
SQLite version 3.8.7.2 2014-11-18 20:57:56
Enter ".help" for usage hints.
sqlite> create table test (test_id integer primary key);
sqlite> insert into test values (1);
sqlite> select * from test;
1
.quit
No .save
. Now load the database again.
没有.save
。现在再次加载数据库。
$ sqlite3 test.sl3
SQLite version 3.8.7.2 2014-11-18 20:57:56
Enter ".help" for usage hints.
sqlite> select * from test;
1
The data is still there.
数据还在。
回答by Erik
as Mike pointed out in his answer, you should provide a file name to put the database in.
正如迈克在他的回答中指出的那样,您应该提供一个文件名来放入数据库。
If you did a lot of work and you did not provide a file name up front and you work in a version in which the .save
command is not yet available (you quote that sqlite3 3.6.20
does not know it and I also do not see it in sqlite3 version 3.8.2
) you can use the .backup
command
如果您做了很多工作并且没有预先提供文件名,并且您在该.save
命令尚不可用的版本中工作(您引用的sqlite3 3.6.20
不知道它,我也没有在 中看到它sqlite3 version 3.8.2
),您可以使用.backup
命令
sqlite> .help
[...]
.backup ?DB? FILE Backup DB (default "main") to FILE
$ sqlite3
[...]
sqlite> create table mytable ( column1 text, column2 integer );
sqlite> insert into mytable values ( 'ENTRY1', 1 );
sqlite> insert into mytable values ( 'ENTRY2', 2 );
sqlite> .backup main temp.db
sqlite> .quit
$ sqlite3 temp.db
[...]
sqlite> .schema
CREATE TABLE mytable ( column1 text, column2 integer );
sqlite> select * from mytable;
column1 column2
---------- ----------
ENTRY1 1
ENTRY2 2
回答by bpgergo
You're supposed to provide a filename as an argument for the .save command, e.g.:
你应该提供一个文件名作为 .save 命令的参数,例如:
sqlite> .save ex1.db
回答by Superstone
Use Sqlite3 ex1.db
to open your database. After that, all queries will take effect in your DB.
使用Sqlite3 ex1.db
打开数据库。之后,所有查询都将在您的数据库中生效。