java SQLException: 没有这样的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4478003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:33:58  来源:igfitidea点击:

SQLException: no such table

javasqlitejdbc

提问by Bnjmn

now I got some trouble connecting to my database. I know the tables i am looking for exist because when I access them with the command line they can be queried.

现在我在连接到我的数据库时遇到了一些问题。我知道我要查找的表存在,因为当我使用命令行访问它们时可以查询它们。

Probably some minor oversight but I would love some help.

可能是一些小疏忽,但我希望得到一些帮助。

This is where I make my connection to my database package persistence;

这是我连接到我的数据库包持久性的地方;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


public class DBRegistry {

    private static DBRegistry db = null;
    private static Connection connection = null;

    private DBRegistry() {};

    public static synchronized DBRegistry getUniqueInstance() {
        if (db == null) {
                db = new DBRegistry();
                return db;
        }
        else return db;
    }

    public synchronized Connection getDBConnection() {
            try {
                Class.forName("org.sqlite.JDBC");
                connection = DriverManager.getConnection("jdbc:sqlite:src/database/taskMan.db");
                return connection;
            } 
            catch (SQLException e) {e.printStackTrace();} 
            catch (ClassNotFoundException e) {e.printStackTrace();}
            return null;
    }

    public synchronized void closeConnection() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Here is how I query it

这是我查询它的方式

public void create(UUID oid, Object obj) {
    Task t = (Task)obj;
    String statement = "INSERT INTO `complexTask` (`oid`,`description`,`status`) VALUES (?, ?, ?)";
    try {
        PreparedStatement dbStatement = db.prepareStatement(statement);
        dbStatement.setString(1, oid.toString());
        dbStatement.setString(2, t.getDescription());
        dbStatement.setBoolean(3, t.getStatus());
        dbStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

and finally a stack trace:

最后是堆栈跟踪:

java.sql.SQLException: no such table: complexTask
    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:231)
    at org.sqlite.Conn.prepareStatement(Conn.java:224)
    at org.sqlite.Conn.prepareStatement(Conn.java:213)
    at persistence.framework.ComplexTaskRDBMapper.create(ComplexTaskRDBMapper.java:23)
    at persistence.PersistanceFacade.create(PersistanceFacade.java:49)
    at persistence.persistanceStates.NewState.commit(NewState.java:10)
    at persistence.PersistentObject.commit(PersistentObject.java:23)
    at domain.objects.Task.commitToDB(Task.java:89)
    at domain.TaskRepository.commitToDB(TaskRepository.java:60)
    at domain.TaskController.persistanceCommit(TaskController.java:97)
    at presentation.TaskControlsJPanel.actionPerformed(TaskControlsJPanel.java:127)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6175)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:5940)
    at java.awt.Container.processEvent(Container.java:2105)
    at java.awt.Component.dispatchEventImpl(Component.java:4536)
    at java.awt.Container.dispatchEventImpl(Container.java:2163)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.java:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4362)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

And some JUnit code for good measure, the first test passes and the second fails with a similar error to the one above

还有一些 JUnit 代码可以很好地衡量,第一个测试通过,第二个测试失败,并出现与上面类似的错误

package test.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import persistence.DBRegistry;
import junit.framework.TestCase;

public class TestDBRegistry extends TestCase {

    public void testDBRegistryConnection() {
        Connection con =  DBRegistry.getUniqueInstance().getDBConnection();
        assertNotNull(con);
    }

    public void testTableQuery() throws SQLException {
        Connection con =  DBRegistry.getUniqueInstance().getDBConnection();
        PreparedStatement dbStatement = con.prepareStatement("SELECT COUNT(*) FROM `singleTask`");
        assertEquals("should be 1 for successful query", 1, dbStatement.executeQuery());
    }

回答by Marplesoft

I notice that in both your unit test and your other code you are using back-ticks around the table name. In the latest version of sqlite, this is fine but in older versions it wasn't handled as well I believe. Can you try removing the ticks around the table name or maybe changing them to regular quotes rather than back-ticks?

我注意到在您的单元测试和其他代码中,您都在表名周围使用了反引号。在最新版本的 sqlite 中,这很好,但我认为在旧版本中它没有得到很好的处理。您可以尝试删除表名称周围的勾号,或者将它们更改为常规引号而不是反勾号吗?

If that doesn't solve it I would check to be absolutely sure that you are pointing to the correct db file. If you specify a filename that doesn't exist you won't get an error, it will simply create a new database there. I'm not sure what the "current directory" is under the context of your app or unit test but be sure it is pointing to where you think it is. To test this, you could change the db file name to foo.db, run the unit test, then search your machine for foo.dbto see where it got created. That will tell you where your app is working off of.

如果这不能解决它,我会检查以绝对确定您指向正确的 db 文件。如果您指定一个不存在的文件名,您将不会收到错误消息,它只会在那里创建一个新数据库。我不确定您的应用程序或单元测试上下文中的“当前目录”是什么,但请确保它指向您认为的位置。要对此进行测试,您可以将 db 文件名更改为foo.db,运行单元测试,然后搜索您的机器foo.db以查看它的创建位置。这会告诉你你的应用程序在哪里工作。

回答by Luke Woodward

I'm not sure your JDBC connection string is quite right. The connection string you use ends with taskMan.db, but your comment above implies that the name of the database file is taskManDb.db(note the extra Db).

我不确定您的 JDBC 连接字符串是否正确。您使用的连接字符串以 结尾taskMan.db,但您上面的注释暗示数据库文件的名称是taskManDb.db(注意额外的Db)。

回答by duffymo

It might not fix your problem with the SQL INSERT, but I don't care at all for your DBRegistry implementation. I'd write it like this:

它可能无法解决您使用 SQL INSERT 的问题,但我根本不关心您的 DBRegistry 实现。我会这样写:

package persistence;

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


public class DBRegistry
{
    public static Connection getConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
    {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection)
    {
        try
        {
            if (connection != null)
            {
                connection.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }


    public static void close(Statement statement)
    {
        try
        {
            if (statement != null)
            {
                statement.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet)
    {
        try
        {
            if (resultSet != null)
            {
                resultSet.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}

回答by code511788465541441

Maybe your table is in a package. so you might have do something like

也许你的桌子在一个包裹里。所以你可能做了类似的事情

select * from Tasks.ComplextTask

('Tasks' being the package)

(“任务”是包)

回答by Tiger

Now just try to find a file with the same name in c:\windows\system32 and you will find it. It tells to us that your path is not correct. Have a nice day;

现在只要尝试在 c:\windows\system32 中找到一个同名的文件,你就会找到它。它告诉我们您的路径不正确。祝你今天过得愉快;

回答by Durgesh Gupta

Please Commit your Connection after created table and after insertion.

请在创建表和插入后提交您的连接。