SQLite: near "DATABASE": syntax error

java, sqlexception, sqlite

Solution

With SQLite you just open the database. If it doesn't exist it will be created for you automatically. Then you can check if your tables exist and create them as required.

Problem

When I try to create a database programatically in SQLite using java, it generates the following error in console. ``` java.sql.SQLException: near "DATABASE": syntax error at org.sqlite.DB.throwex(DB.java:288) at org.sqlite.NativeDB.prepare(Native Method) at org.sqlite.DB.prepare(DB.java:114) at org.sqlite.PrepStmt.<init>(PrepStmt.java:37) at org.sqlite.Conn.prepareStatement(Conn.java:224) at org.sqlite.Conn.prepareStatement(Conn.java:217) at org.sqlite.Conn.prepareStatement(Conn.java:206) at com.mmcal.build.BuildDB.executeQuery(BuildDB.java:46) at com.mmcal.build.BuildDB.createDB(BuildDB.java:19) at MainGen.main(MainGen.java:16) ``` My code is : ``` public class BuildDB { Connection connection; PreparedStatement ps; ResultSet rs; String qry; public void createDB() { qry = "CREATE DATABASE IF NOT EXISTS myDb"; executeQuery(qry); } public boolean executeQuery(String qry) { int cnt = 0; boolean flag = false; connection = ConnectionMaster.connectDb(); try { ps = connection.prepareStatement(qry); cnt = ps.executeUpdate(); System.out.println(flag); } catch (SQLException e) { e.printStackTrace(); } if (cnt == 0) { flag = true; } return flag; } } ``` What's the problem in my code or is this the correct way to create database in SQLite via programatically?

Original source