简体   繁体   English

Java和SQLite [SQLITE_ERROR] SQL错误或缺少数据库()

[英]Java and SQLite [SQLITE_ERROR] SQL error or missing database ()

I'm trying SQLite with Java, this is the first time using both together, here is the code: 我正在尝试将SQLite与Java一起使用,这是第一次同时使用两者,下面是代码:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 *
 * @author Alessio
 */
public class DB {

    public static void main(String[] args){
        Connection c = null;
        Statement stmt = null;
        try{
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:test.db");
            stmt = c.createStatement();
            String sql = "CREATE TABLE COMPANY " +
                   "(ID INT PRIMARY KEY     NOT NULL," +
                   " NAME           TEXT    NOT NULL, " + 
                   " AGE            INT     NOT NULL, " + 
                   " ADDRESS        CHAR(50), " + 
                   " SALARY         REAL)"; 
            stmt.executeUpdate(sql);
            stmt.close();
            c.close();
        }
        catch(Exception e){
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Database opened succesfully!!!");
    }

}

When I run the code for the first time I have any error, my result in console is: 当我第一次运行代码时,我遇到任何错误,在控制台中的结果是:

run:
Database opened succesfully!!!
BUILD SUCCESSFUL (total time: 1 second)

while the second time I get 而第二次我得到

run:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database ()
BUILD SUCCESSFUL (total time: 1 second)

What am I doing wrong? 我究竟做错了什么?

If you are executing the exact same code, then I guess the problem is you are creating same table twice. 如果执行的是完全相同的代码,那么我想问题是您两次创建了相同的表。

It went well for the first time because there is no such table in the database. 第一次运行顺利,因为数据库中没有这样的表。 It failed on the second time because there is already an exactly same table named COMPANY . 它第二次失败,因为已经有一个完全相同的名为COMPANY表。

Your question is not so detailed. 您的问题不是那么详细。 What do you have to do for get a normal situation again? 为了再次恢复正常状态,您需要做什么? Do you have to delete you DB file? 是否需要删除数据库文件?

Anyway, I would say that the error is caused by the table already existing in the Database. 无论如何,我会说该错误是由数据库中已经存在的表引起的。 If you want to try a simple query for debug purpose, just try 如果您想尝试用于调试目的的简单查询,只需尝试

SELECT date('now');

or something not dependant on the DB structure. 或不依赖于数据库结构的东西。 Try a generic query or check if the table exists and drop it before re-creating it. 尝试进行一般查询或检查表是否存在,并在重新创建表之前将其删除。

First way your table name COMPANY as already exists so you can not create same table name(COMPANY) with same database like test 您的表名COMPANY已经存在的第一种方式因此您不能使用相同的数据库(如test)创建相同的表名(COMPANY)

and second you can write proper or full getconnection URL like 其次,您可以编写正确或完整的getconnection URL,例如

Connection con = null;
Class.forName("org.sqlite.JDBC");
//getConnection(String url)
con = DriverManager.getConnection("jdbc:sqlite:C:\\Documents and Settings\\demo\\Test_demo.db");
        //SQL error or missing database 
System.out.println("Operation done successfully");

I think you should answer this question by seeing the code.You are creating the Table on boot and again you are running the same query twice.It is bound to happened.Either you drop the table and recreate or check the table it it exist and then add. 我认为您应该通过查看代码来回答这个问题。在启动时创建表,然后再次运行相同的查询两次,这势必发生了。要么删除表并重新创建表,要么检查表是否存在并且然后加。

Other Case: This error also happens if your connection string is wrong.The URL which is pointing to SQLite db file.I suggest you place it in resource folder and access as below public static String connectionString = "jdbc:sqlite::resource:localdata.db"; 其他情况:如果您的连接字符串错误,也会发生此错误。指向SQLite数据库文件的URL。建议您将其放在资源文件夹中,并按如下所示进行访问public static String connectionString =“ jdbc:sqlite :: resource:localdata 。D b”;

The error caused by the fact that SQL, creates the exact table every time it runs. 由SQL每次运行都会创建准确表的事实引起的错误。 Which does not conform best SQL practices. 哪个不符合最佳SQL惯例。

In your SQL Query, Change the 在您的SQL查询中,更改

"CREATE TABLE COMPANY " +
       "(ID INT PRIMARY KEY     NOT NULL," +
        " NAME           TEXT    NOT NULL, " + 
        " AGE            INT     NOT NULL, " + 
        " ADDRESS        CHAR(50), " + 
        " SALARY         REAL)";

to

"CREATE TABLE COMPANY IF NOT EXIST" +
        "(ID INT PRIMARY KEY     NOT NULL," +
        " NAME           TEXT    NOT NULL, " + 
        " AGE            INT     NOT NULL, " + 
        " ADDRESS        CHAR(50), " + 
        " SALARY         REAL)";

The IF NOT EXIST statement checks if the table exists if no, it creates a new one else it skips creating the table. IF NOT EXIST语句检查表是否存在(如果不存在),然后创建一个新表,否则跳过创建表。

暂无
暂无

声明:本站的技术帖子网页,遵循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 “Name”: 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错误或缺少数据库(“ ID”附近:语法错误) - java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near “ID”:syntax error) Java [SQLITE_ERROR] SQL 错误或缺少数据库(“resi”附近:语法错误) - Java [SQLITE_ERROR] SQL error or missing database (near “resi”: syntax error) org.sqlite.SQLiteException: [SQLITE_ERROR] SQL 错误或缺少数据库(没有这样的表:帐户) - org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: account) “ [SQLITE_ERROR] SQL错误或缺少数据库(“ DATABASE”附近:语法错误)” - “[SQLITE_ERROR] SQL error or missing database (near ”DATABASE“: syntax error)” 错误代码:1(SQLITE_ERROR)原因:SQL(query)错误或数据库丢失 - Error Code:1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database SQLITE_ERROR SQL错误或数据库丢失(“ VALUES”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “VALUES”: syntax error) SQLITE_ERROR SQL错误或数据库丢失(“声明”附近:语法错误) - SQLITE_ERROR SQL error or missing database (near “Statements”: syntax error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM