3

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?

0

3 Answers 3

8

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.

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

Comments

6

SQLite doesn't create database, if the file doesn't exist that you assign to, it gets created.

You just assign a file. It doesn't have the command to create database, only create table

Comments

-1

you have to create tables only, is it? Databases are already created in sqlite.online.com eg. CREATE TABLE products ( id INT NOT NULL, price MONEY, PRIMARY KEY (id) );

//I got the same error while trying the website to run sql code for the first time but maybe my post might help u.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

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.