How can I use OrmLite with Android's default SQLite

江枫思渺然 提交于 2019-12-08 08:54:16

问题


I have a big problem using the default SQLite database via JDBC driver.I would like to use ORMLite with this.Let me expose my code:

String databaseUrl = "jdbc:sqlite:/data/data/my.package.name/db.sqlite";

Class.forName("SQLite.JDBCDriver");
DriverManager.getConnection(databaseUrl);

dataSource = DatabaseTypeUtils.createSimpleDataSource(databaseUrl);
databaseType = DatabaseTypeUtils.createDatabaseType(dataSource);
databaseType.loadDriver();

UpDao = new UserProfileJdbcDao(databaseType);
UpDao.setDataSource(dataSource);
UpDao.initialize();

I downloaded the ormlite2.8.jar (src) and modified the SqliteDatabaseType class so that the private final static String DRIVER_CLASS_NAME = "SQLite.JDBCDriver" . However when I imported all of the classes from the ormlite2.8.jar(src) , I found errors concerning the logger, specifically the CommonsLoggingLog and Log4jLog classes.Someone advised my to write my own Logger class that uses the Android logger , but I do not know how to do that. This is the ORMLite I am using for Android : http://ormlite.sourceforge.net/sqlite_java_android_orm.html

I am highly appreciating any help. Thank you in advance.

Regads, Andrew


回答1:


This is an old question Andrew, but for posterity, ORMLite has native support for the Android OS database calls. I'd recommend upgrading and trying it out. There are sample projects, documentation, and downloads here:

http://ormlite.com/sqlite_java_android_orm.shtml




回答2:


In the documentation of ORMlite is mentioned, that there are no direct dependencies and Log4J will be only used if provided in the class path.

Are you sure, that importing the complete code is the right way using ORMlite?

What about that: http://ormlite.sourceforge.net/javadoc/doc-files/ormlite_5.html#SEC25 You should do whats mentioned in the first sentence:

DatabaseType databaseType = new SqliteAndroidDatabaseType();
databaseType.loadDriver();
// change this for your path and application name
String databaseUrl = "jdbc:" + databaseType.getDriverUrlPart() + ":" +
    getFilesDir() + "/test.db";
SimpleDataSource dataSource =
    DatabaseTypeUtils.createSimpleDataSource(databaseUrl);

// configure and use your dao as in previous examples


来源:https://stackoverflow.com/questions/3493605/how-can-i-use-ormlite-with-androids-default-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!