8

I have to simply change name of database, but it seems the ALTER DATABASE command is not valid in SQLite.

My scenario is that I have a SQLite encrypted database, and now I want to rename it. After manually renaming the db file, encryption is not working. So I tried to change the database's name using command. It looks like problem is due to encryption.

3
  • Why do you need it? can you share the scenorio? Commented Jun 1, 2012 at 7:20
  • @YaqubAhmad, if you're building queries in a multi-database environment, you need to explicitly specify the name. Copying those queries to a single-database environment would require removing the database name, unless you could give the single-database a name matching the one the queries are using. Commented Dec 15, 2013 at 2:01
  • For main specifically: stackoverflow.com/questions/7313190/can-i-name-sqlite-databases Commented Nov 22, 2015 at 21:45

3 Answers 3

16

Each sqlite file contains one and only one database. This database bears no name. For what I can remember, the first opened database has a 'virtual' name main for when you want to refer to it say when you attach supplementary databases. You get to choose the 'virtual' name of databases you attach.

[someone@somewhere tmp]$ sqlite3 bla.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /tmp/bla.db                                  
sqlite> attach database 'example.db' as another_db;
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /tmp/bla.db                                  
2    another_db       /tmp/example.db       

Otherwise Laurent is correct that you can rename the database file if that is what you are trying to do here.

Sign up to request clarification or add additional context in comments.

Comments

12

There's no database name in SQLite because there's only one database per file. Maybe what you want to do is rename the SQLite file instead.

3 Comments

I tried that but the original file is encrypted and when I change filename same password is not working for it.
This is not true, sqlite allow you attach another database. And .databases would list them.
The file name has nothing to do with the database name in SQLite. By default there are two databases main and temp.
2

In recent versions of Sqlite3 there is the .backup command.

If the database is not big this could be a solution:

S:\>sqlite3 encrypted.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .backup new.db
sqlite> .q

then

S:\>sqlite3 new.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.