我有一个单独的 Web 服务来获取应用程序所需的数据(txt 数据 + 图像 URL 以下载图像),这是我的应用程序中唯一的服务,我在应用程序启动时调用此服务并设置 SQLite 数据库(使用 ORMLite),一旦一切完成,用户就可以进入主屏幕,因此在下次启动时,如果数据在数据库中,则此服务不会调用。到目前为止一切正常。
我的问题是如何在 android 应用程序之外构建这个 SQLite DB 并将 SQLite 数据库文件捆绑到应用程序;我正在使用 maven 构建 android 应用程序。
我是否需要一个单独的 android 项目来调用 Web 服务并构建 SQLite DB,或者我可以使用 java 项目来构建 SQLite Db,
如果我使用 android 项目如何获取数据库文件?
如何指示 maven 运行上述项目并将 db 文件复制到 android 应用程序。
我从一个 maven 项目开始,以下是我的 pom xml 文件的快照
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>4.41</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>4.41</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.6.16</version>
</dependency>
</dependencies>
当我尝试将数据插入数据库时出现以下异常。
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:363)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:306)
Caused by: java.sql.SQLException: NYI
at org.sqlite.Conn.prepareStatement(Conn.java:443)
at com.j256.ormlite.jdbc.JdbcDatabaseConnection.insert(JdbcDatabaseConnection.java:147)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:89)
... 4 more
我的域对象
@DatabaseTable
public class Widget implements Comparable<Widget>{
@DatabaseField(id = true)
private String id = null;
@DatabaseField
private String title = null;
....More fileds.
@ForeignCollectionField
private ForeignCollection<WidgetAttribute> db_widget_attribute;
@DatabaseField(canBeNull = true, foreign = true)
private AdvancedSettings advanced_settings = null;
... Getters /setters....
}
@DatabaseTable
public class WidgetAttribute {
@DatabaseField(id = true)
private String id = null;
@DatabaseField
private String value = null;
....More fileds.
@DatabaseField(foreign=true,foreignAutoRefresh=true)
private Widget widget;
... Getters /setters....
}
@DatabaseTable
public class AdvancedSettings {
@DatabaseField(id = true)
private String id = null;
public static final String DB_COLUMN_SHOW_AS_TAB_BAR = "show_as_tab_bar";
@DatabaseField
private String show_as_tab_bar = null;
....More fileds
@DatabaseField(canBeNull = true, foreign = true)
private AdvancedSettingsImages advanced_settings_images = null;
@DatabaseField(foreign=true,foreignAutoRefresh=true)
private Widget widget;
public String getId() {
return id;
}
... Getters /setters....
}
@DatabaseTable
public class AdvancedSettingsImages {
@DatabaseField(generatedId=true)
private int id;
....More fileds
@DatabaseField(canBeNull = true, foreign = true)
private AdvancedSettings advancedSettings = null;
... Getters /setters....
}
当我运行应用程序时,我在控制台中看到以下日志消息
DEBUG [com.j256.ormlite.dao.DaoManager] created dao for class class com.xxx.domain.Widget with reflection
DEBUG [com.j256.ormlite.dao.DaoManager] created dao for class class com.xxx.domain.WidgetAttribute with reflection
DEBUG [com.j256.ormlite.dao.DaoManager] created dao for class class com.xxx.domain.AdvancedSettings with reflection
DEBUG [com.j256.ormlite.dao.DaoManager] created dao for class class com.xxx.domain.AdvancedSettingsImages with reflection
INFO [com.j256.ormlite.table.TableUtils] creating table 'widget'
INFO [com.j256.ormlite.table.TableUtils] executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `widget` (`id` VARCHAR , `title` VARCHAR , `widget_type_id` VARCHAR , `app_xxx_id` VARCHAR , `created_at` VARCHAR , `updated_at` VARCHAR , `position` VARCHAR , `group_widget` VARCHAR , `parent_id` VARCHAR , `image_link` VARCHAR , `advanced_settings_id` VARCHAR , `widgets_id` INTEGER NOT NULL , PRIMARY KEY (`id`) )
INFO [com.j256.ormlite.table.TableUtils] creating table 'widgetattribute'
INFO [com.j256.ormlite.table.TableUtils] executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `widgetattribute` (`id` VARCHAR , `value` VARCHAR , `attribute_type` VARCHAR , `name` VARCHAR , `created_at` VARCHAR , `updated_at` VARCHAR , `image_link` VARCHAR , `widget_id` VARCHAR , PRIMARY KEY (`id`) )
INFO [com.j256.ormlite.table.TableUtils] creating table 'advancedsettings'
INFO [com.j256.ormlite.table.TableUtils] executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `advancedsettings` (`id` VARCHAR , `show_widget_on_home_screen` VARCHAR , `highlight_cell` VARCHAR , `hide_menu` VARCHAR , `show_as_tab_bar` VARCHAR , `icon_color` VARCHAR , `live_tile` VARCHAR , `text_color` VARCHAR , `vertical_alignment` VARCHAR , `horizontal_alignment` VARCHAR , `created_at` VARCHAR , `updated_at` VARCHAR , `advanced_settings_images_id` INTEGER , `widget_id` VARCHAR , PRIMARY KEY (`id`) )
INFO [com.j256.ormlite.table.TableUtils] creating table 'advancedsettingsimages'
INFO [com.j256.ormlite.table.TableUtils] executed create table statement changed 0 rows: CREATE TABLE IF NOT EXISTS `advancedsettingsimages` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `tab_bar_icon` VARCHAR , `tab_bar_icon_on_press` VARCHAR , `widget_icon` VARCHAR , `widget_icon_on_press` VARCHAR , `advancedSettings_id` VARCHAR )
java.sql.SQLException: Unable to run insert stmt on object
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:352)
at com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:306)
Caused by: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)
at org.sqlite.DB.newSQLException(DB.java:374)
at org.sqlite.DB.newSQLException(DB.java:378)
at org.sqlite.DB.throwex(DB.java:365)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.PrepStmt.<init>(PrepStmt.java:44)
at org.sqlite.Conn.prepareStatement(Conn.java:403)
at org.sqlite.Conn.prepareStatement(Conn.java:398)
at org.sqlite.Conn.prepareStatement(Conn.java:382)
at org.sqlite.Conn.prepareStatement(Conn.java:386)
at com.j256.ormlite.jdbc.JdbcDatabaseConnection.insert(JdbcDatabaseConnection.java:147)
at com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:89)
... 4 more
有人可以帮我解决上述问题。