Hi,
I am trying to use this wonderful library but I cannot see what I do wrong. I am using this library v4.42 with sqlite-jdbc-3.6.16.
I have a class book with a primary key integer auto increment and a field varchar title.
I copied your exemple code but when I try to persist my Book object,
I have the following error :
2013-01-05 17:39:51,805 [DEBUG] DaoManager created dao for class class net.bouzekri.mymediacollection.Book with reflection
2013-01-05 17:39:51,807 [INFO] TableUtils creating table 'book'
2013-01-05 17:39:51,956 [DEBUG] JdbcConnectionSource opened connection to jdbc:sqlite:data.db got #42596014
2013-01-05 17:39:51,962 [INFO] TableUtils executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `book` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `title` VARCHAR )
Jan 05, 2013 5:39:51 PM
net.bouzekri.mymediacollection.App main
SEVERE: null
java.sql.SQLException: Unable to run insert stmt on object net.bouzekri.mymediacollection.Book@5ca40b6e: INSERT INTO `book` (`title` ) VALUES (?)
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:124)
at com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:394)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:308)
at net.bouzekri.mymediacollection.App.main(App.java:37)
It seems that there is
My Book class :
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
/**
* @author jobou
*/
@DatabaseTable(tableName = "book")
public class Book {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String title;
public Book() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
And my main :
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class App
{
public static void main( String[] args )
{
try {
String databaseUrl = "jdbc:sqlite:data.db";
ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
Dao<Book, Integer> bookDao = DaoManager.createDao(connectionSource, Book.class);
TableUtils.createTableIfNotExists(connectionSource, Book.class);
Book book = new Book();
book.setTitle("My first book");
// persist the account object to the database
bookDao.create(book);
// close the connection source
connectionSource.close();
} catch (SQLException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Do you have an idea ?
Another thing, I tried to use the version 4.9 but there is no DaoManager class. Is this normal ?
Thanks in advance