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

时间:2011-10-12 21:14:59

标签: java sqlite ormlite

我正在尝试用我正在处理的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

我做错了什么?

3 个答案:

答案 0 :(得分:0)

Afaik NYI意味着尚未实施。我认为这个东西不支持参数化SQL。尝试将您想直接插入的值硬编码到更新查询字符串中,看看是否有效

答案 1 :(得分:0)

查看SQLite的源代码,看起来NYI代表尚未实现

您的版本尚未完全实施/完成。升级到3.6.20,似乎不再有这些例外。

答案 2 :(得分:0)

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

  

http://ormlite.com/docs/sqlite

引用:

  

有多个SQLite驱动程序。确保使用Xerial驱动程序,而不是不支持生成ID的Zentus驱动程序。