Java 和 SQLite [SQLITE_ERROR] SQL 错误或缺少数据库 ()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23157184/
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
Java and SQLite [SQLITE_ERROR] SQL error or missing database ()
提问by Mitro
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?
我究竟做错了什么?
采纳答案by mordack550
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.
或不依赖于数据库结构的东西。尝试通用查询或检查表是否存在并在重新创建它之前删除它。
回答by Bowen
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
.
回答by Chetan Bhagat
First way your table name COMPANY as already existsso 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 URLlike
其次,您可以编写正确或完整的 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");
回答by Mohammed Sarfaraz
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 db 文件的 URL。我建议您将其放在资源文件夹中并按如下方式访问 public static String connectionString = "jdbc:sqlite::resource:localdata 。D b”;
回答by Geeky64
The error caused by the fact that SQL, creates the exact table every time it runs. Which does not conform best SQL practices.
由 SQL 每次运行时创建精确表这一事实引起的错误。这不符合最佳 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 语句检查表是否存在,如果不存在,它会创建一个新表,否则它会跳过创建表。