14

I am using the SQLite driver from http://code.google.com/p/sqlite-jdbc/wiki/Introduction Examples shown in the above doc shows how to connect an existing database.

In my application I need to create an SQLite database. How to do that? Is it sufficient to create a file with extension .db? Also is there a function called createFile(). If so how to use it? I googled and nobody is giving a clear answer.

7ochem
  • 2,183
  • 1
  • 34
  • 42
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90

1 Answers1

36

SQLite creates new database file on first attempt to connect if file did not exist already.

So, simply use jdbc:sqlite:filename.db as JDBC connection string, and provided that you have permission to create filename.db, it will be created automatically. You can also manually pre-create this file with 0 size if you want.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • where is it created please? – shareef Feb 23 '14 at 20:05
  • 1
    It simply treats filename.db according to normal filename rules: in current process directory by default, or fully qualified path if file name starts with / (or \ on Windows). – mvp Feb 24 '14 at 08:17
  • Thanks in my case its in bin foldet in tomcat folder in program files I searched my computer@ – shareef Feb 24 '14 at 14:27
  • 1
    Saving db in tomcat bin folder sounds like bad idea. It is very much possible that tomcat process will not have enough permissions to actually create your database file there. If it does, your application is likely to be vulnerable to some security exploit. If I were you, I would find better place for SQLite databases. – mvp Feb 24 '14 at 21:41
  • I'd just like to add for future readers, I kept getting the `opening db: './filename.db': Access is denied` message. I fixed it by giving my IDE administrator access on opening. – Abhishek Divekar Apr 02 '16 at 05:46
  • @abhidivekar: try using fully qualified file path. Your IDE is trying to create this file in some directory where it does not have permission to write or create files, most likely IDE installation directory. – mvp Apr 02 '16 at 05:49
  • @mvp Thanks for the answer, but I already solved it, see my updated comment :) – Abhishek Divekar Apr 02 '16 at 05:56
  • @abhidivekar: giving admin access is not necessarily good advice. You need to know where exactly you are creating your database. If you are OK with keeping it in some random location, you probably don't care about your data in first place. – mvp Apr 02 '16 at 06:00