简体   繁体   English

“ [SQLITE_ERROR] SQL错误或缺少数据库(“ DATABASE”附近:语法错误)”

[英]“[SQLITE_ERROR] SQL error or missing database (near ”DATABASE“: syntax error)”

I am trying to create a new database file named test in the folder D:\\sqlite , using JDBC, as follows: 我正在尝试使用JDBC在文件夹D:\\ sqlite中创建一个名为test的新数据库文件,如下所示:

import java.sql.*;

public class Class1 {

    private static void createNewDataBase(){

        String url = "jdbc:sqlite:D:sqlite/";
        Connection conn = null;
        Statement statement = null;

        try{

            conn = DriverManager.getConnection(url);
            System.out.println("Connection Established");
            statement = conn.createStatement();

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        try {
            statement.execute("CREATE DATABASE test");

        } catch (SQLException e) {
            System.out.println(e.getMessage());

        } finally{
            try{
                if(statement != null)
                    statement.close();

            }catch(SQLException e){
                System.out.println(e.getMessage());
            }

            try{
                if(conn != null)
                    conn.close();

            }catch(SQLException e){
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args){
        createNewDataBase();
    }

}

When I run the project, I get the following output: 运行项目时,将得到以下输出:

Connection Established
[SQLITE_ERROR] SQL error or missing database (near "DATABASE": syntax error)

Process finished with exit code 0

It says the syntax is wrong but I can't find the error. 它说语法是错误的,但我找不到错误。 I've looked for answers for similar questions, but none solved my problem. 我一直在寻找类似问题的答案,但没有一个能解决我的问题。 Can anyone tell me what's the problem? 谁能告诉我这是什么问题? Thanks in advance! 提前致谢!

The database exists, it has been connected ie the database is the connection, which is basically the file. 数据库存在,它已经被连接,即数据库就是连接,基本上就是文件。 Hence there is no SQL for CREATE DATABASE . 因此,没有用于CREATE DATABASE SQL。

Inside the database you would typically create tables (and perhaps other components). 在数据库内部,通常会创建表(也许还有其他组件)。

eg CREATE TABLE mytable (mycolumn TEXT, myothercolumn INTEGER) which would create a table named mytable with 2 columns mycolumn and myothercolumn (the former being defined with a column type of TEXT, the latter with a column type of INTEGER). 例如CREATE TABLE mytable (mycolumn TEXT, myothercolumn INTEGER)将创建一个名为mytable的表,该表包含2列mycolumnmyothercolumn (前者定义为TEXT的列类型,后者定义为INTEGER的列类型)。

As such, if you were to change :- 因此,如果您要更改:-

     statement.execute("CREATE DATABASE test");

to :- 至 :-

    statement.execute("CREATE TABLE mytable (mycolumn TEXT, myothercolumn INTEGER)");
  • Note you'd only want to do this once, otherwise it would fail as the table already exists, so you could use CREATE TABLE IF NOT EXISTS mytable (mycolumn TEXT, myothercolumn INTEGER) , of cousre it depends upon your requirements. 请注意,您只希望执行一次此操作,否则将因为表已经存在而失败,因此您可以使用CREATE TABLE IF NOT EXISTS mytable (mycolumn TEXT, myothercolumn INTEGER) ,这取决于您的要求。

You may find that it will work. 您可能会发现它会起作用。 Obviously you will need to do other things such as add some data as the table will be empty. 显然,您将需要做其他事情,例如添加一些数据,因为表将为空。

Perhaps then try 也许然后尝试

    statement.execute("INSERT INTO mytable VALUES('fred',100)");

Every time this is run a new ROW will be added to the table name mytable. 每次运行时,都会将新的ROW添加到表名mytable中。

As already stated by @Andreas, there is no CREATE DATABASE SQL statement for SQLite. 如@Andreas所述,SQLite没有CREATE DATABASE SQL语句。 To create a new SQLite database you need to merely make a connection to one and it is automatically created (you do however need to ensure that the D:/sqlite/ path already exists within the local file system ). 要创建一个新的SQLite数据库,您只需要建立一个连接并自动创建一个连接即可(但是,您需要确保本地文件系统中已经存在D:/ sqlite /路径)。

The following code should create an empty (no tables) SQLite Database named MyNewDatabase.sqlite within the folder sqlite located in the root of drive D of your local file system: 以下代码应在本地文件系统驱动器D的根目录下的sqlite文件夹中创建一个名为MyNewDatabase.sqlite (无表)SQLite数据库:

String dbPath = "D:/sqlite/MyNewDatabase.sqlite";
Connection conn = null;
try {
   Class.forName("org.sqlite.JDBC");
   conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath;);
}
catch (ClassNotFoundException ex) { 
    ex.printStackTrace();
}
catch (SQLException ex) { 
    ex.printStackTrace();
}
finally {
    try { 
        if (conn != null) { 
            conn.close(); 
        } 
    } 
    catch (SQLException e) { e.printStackTrace(); }
}

Now you need to create one or more Tables for your new database to make it useful. 现在,您需要为新数据库创建一个或多个表以使其有用。 SQLite does accept the CREATE TABLE SQL statement and you could do this through the same connection if desired. SQLite确实接受CREATE TABLE SQL语句,如果需要,您可以通过相同的连接来完成此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLITE_ERROR SQL错误或数据库丢失(“(”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “(”: syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(接近“=”:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “=”:syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“名称”附近:语法错误) - java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near “Name”: syntax error) java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(“ ID”附近:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “ID”:syntax error) SQLITE_ERROR SQL错误或数据库丢失(“声明”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “Statements”: syntax error) SQLITE_ERROR-SQL错误或缺少数据库(“ AUTOINCREMENT”附近:语法错误) - SQLITE_ERROR - SQL error or missing database (near “AUTOINCREMENT”: syntax error) Java [SQLITE_ERROR] SQL 错误或缺少数据库(“resi”附近:语法错误) - Java [SQLITE_ERROR] SQL error or missing database (near “resi”: syntax error) SQLITE_ERROR SQL错误或数据库丢失(“ VALUES”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “VALUES”: syntax error) Java和SQLite [SQLITE_ERROR] SQL错误或缺少数据库() - Java and SQLite [SQLITE_ERROR] SQL error or missing database () 当我尝试在 dbeaver 中运行此代码时,它给了我此错误“[SQLITE_ERROR] SQL 错误或缺少数据库(靠近“fk_GRADES_ID”:语法错误)” - when i try to run this code in dbeaver it give me this error " [SQLITE_ERROR] SQL error or missing database (near "fk_GRADES_ID": syntax error)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM