gpt4 book ai didi

java - Ormlite 在 create() 上抛出 SQLException。我究竟做错了什么?

转载 作者:行者123 更新时间:2023-12-01 05:38:28 24 4
gpt4 key购买 nike

我正在尝试用我正在开发的 Java 项目中的 Ormlite 替换现有数据库代码。

但是,当我调用 create() 将对象插入 SQLite 数据库时,它会抛出 SQLException。我想我可能犯了一个明显的错误,但我不知道它是什么。这是代码:

public class OrmliteTest {
private final String DATABASE_URL = "jdbc:sqlite:test.sqlite";
private JdbcConnectionSource connectionSource = null;
private Dao<Article, Integer> articleDao;

public OrmliteTest() throws SQLException{
connectionSource = new JdbcConnectionSource(DATABASE_URL);

TableUtils.createTableIfNotExists(connectionSource, Article.class);

articleDao = DaoManager.createDao(connectionSource, Article.class);

Article article1 = new Article("articles", "idnumber", "blah blah blah", (int)(new Date().getTime()/1000));

articleDao.createOrUpdate(article1); // throws SQLException - why?
}
}

@DatabaseTable(tableName = "articles")
class Article {
public static final String CATEGORY_FIELD_NAME = "category";
public static final String ARTICLEID_FIELD_NAME = "articleid";
public static final String BODY_FIELD_NAME = "body";
public static final String ADDED_FIELD_NAME = "added";

@DatabaseField(generatedId = true)
public int id;

@DatabaseField(columnName = CATEGORY_FIELD_NAME)
public String category;

@DatabaseField(columnName = ARTICLEID_FIELD_NAME)
public String articleId;

@DatabaseField(columnName = BODY_FIELD_NAME)
public String body;

@DatabaseField(columnName = ADDED_FIELD_NAME)
public int added;

public Article() {}

public Article(String category, String articleId, String body, int added) {
setCategory(category);
setArticleId(articleId);
setBody(body);
setAdded(added);
}

...getters and setters...
}

调用 articleDao.createOrUpdate(article1) 时会引发异常。这是回溯:

java.sql.SQLException: Unable to run insert stmt on object com.myapp.tester.Article@7a36824: INSERT INTO `articles` (`category` ,`storyid` ,`body` ,`added` ) VALUES (?,?,?,?)
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:90)
at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:325)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:287)
at com.j256.ormlite.dao.BaseDaoImpl.createOrUpdate(BaseDaoImpl.java:312)
at com.myapp.tester.OrmliteBugTester.<init>(OrmliteBugTester.java:128)
at com.myapp.tester.Tester.main(Tester.java:24)
Caused by: java.sql.SQLException: NYI
at org.sqlite.Conn.prepareStatement(Conn.java:217)
at com.j256.ormlite.jdbc.JdbcDatabaseConnection.insert(JdbcDatabaseConnection.java:105)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:63)
... 5 more

我做错了什么?

最佳答案

请确保您使用正确的 SQLite 驱动程序 ORMLite 。我知道 Zentus 驱动程序经常抛出 NYI 异常。这是手册:

http://ormlite.com/docs/sqlite

引用:

There are multiple SQLite drivers out there. Make sure you use the Xerial driver and not the Zentus driver which does not support generated ids.

关于java - Ormlite 在 create() 上抛出 SQLException。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com