how we can save database created in sqlite3

2019-04-05 07:32发布

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

.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.

3条回答
神经病院院长
2楼-- · 2019-04-05 07:46

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

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         
查看更多
一纸荒年 Trace。
3楼-- · 2019-04-05 08:02

I am trying to create a database and table in it. but unable to save and open again after exiting from 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.)

$ 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.

$ 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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-05 08:03

You're supposed to provide a filename as an argument for the .save command, e.g.:

sqlite> .save ex1.db

docs: http://www.sqlite.org/cli.html

查看更多
登录 后发表回答