简体   繁体   中英

Ormlite throwing SQLException on create(). What am I doing wrong?

I'm trying to replace existing database code with Ormlite in a Java project I'm working on.

However, when I call create() to insert an object into the SQLite database, it throws a SQLException. I figure I'm probably making an obvious mistake, but I can't figure out what it is. Here is the code:

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...
}

The exception is thrown on the call to articleDao.createOrUpdate(article1) . Here is the traceback:

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

What am I doing wrong?

Well afaik NYI means Not Yet Implemented. I think this thing doesn't support parametrised SQL. Try hardcoding the values you wanna insert directly in your update query String and see if that works

Looking at the source code of SQLite it looks like NYI stands for not yet implemented .

Your version simply isn't completely implemented/done. Upgrade to 3.6.20 which doesn't seem to have these exceptions anymore.

Please make sure that you are using the right SQLite driver with ORMLite . I know the Zentus driver throws NYI exceptions a lot. Here's the manual:

http://ormlite.com/docs/sqlite

To quote:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM