Master Mind
Master Mind

Reputation: 2406

SQLite: near "DATABASE": syntax error

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?

Upvotes: 2

Views: 18947

Answers (3)

jokernate
jokernate

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

Upvotes: -1

Trausti Thor
Trausti Thor

Reputation: 3774

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

Upvotes: 5

Doon
Doon

Reputation: 20232

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.

Upvotes: 7

Related Questions