使用时的SQLException谷歌分析与Robolectric(或QUOT;尝试在Robolectric&QUOT使用sqlite3的;) [英] SQLException when using Google Analytics with Robolectric (or "Trying to use SQLite3 in Robolectric")
问题描述
我使用robolectric测试活动,使得使用谷歌分析。不幸的是,每当我试图启动活动我得到下面的异常
I'm using robolectric to test an activity that makes use of Google Analytics. Unfortunately, whenever I try to start up the activity I get the following exception
android.database.SQLException
at com.xtremelabs.robolectric.shadows.ShadowSQLiteDatabase.execSQL(ShadowSQLiteDatabase.java:149)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java)
at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.onCreate(Unknown Source)
at com.xtremelabs.robolectric.shadows.ShadowSQLiteOpenHelper.getWritableDatabase(ShadowSQLiteOpenHelper.java:52)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java)
at com.google.android.apps.analytics.PersistentEventStore.<init>(Unknown Source)
at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
at com.google.android.apps.analytics.GoogleAnalyticsTracker.start(Unknown Source)
...
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE EVENTS ( 'event_id'[*] BIGINT(19) PRIMARY KEY AUTO_INCREMENT NOT NULL, 'user_id' BIGINT(19) NOT NULL, 'account_id' CHAR(256) NOT NULL, 'random_val' BIGINT(19) NOT NULL, 'timestamp_first' BIGINT(19) NOT NULL, 'timestamp_previous' BIGINT(19) NOT NULL, 'timestamp_current' BIGINT(19) NOT NULL, 'visits' BIGINT(19) NOT NULL, 'category' CHAR(256) NOT NULL, 'action' CHAR(256) NOT NULL, 'label' CHAR(256), 'value' BIGINT(19), 'screen_width' BIGINT(19), 'screen_height' BIGINT(19)); "; expected "identifier"; SQL statement:
CREATE TABLE events ( 'event_id' bigint(19) PRIMARY KEY auto_increment NOT NULL, 'user_id' bigint(19) NOT NULL, 'account_id' CHAR(256) NOT NULL, 'random_val' bigint(19) NOT NULL, 'timestamp_first' bigint(19) NOT NULL, 'timestamp_previous' bigint(19) NOT NULL, 'timestamp_current' bigint(19) NOT NULL, 'visits' bigint(19) NOT NULL, 'category' CHAR(256) NOT NULL, 'action' CHAR(256) NOT NULL, 'label' CHAR(256), 'value' bigint(19), 'screen_width' bigint(19), 'screen_height' bigint(19)); [42001-147]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.getSyntaxError(DbException.java:192)
at org.h2.command.Parser.readColumnIdentifier(Parser.java:2694)
at org.h2.command.Parser.parseCreateTable(Parser.java:4975)
at org.h2.command.Parser.parseCreate(Parser.java:3705)
at org.h2.command.Parser.parsePrepared(Parser.java:320)
at org.h2.command.Parser.parse(Parser.java:275)
at org.h2.command.Parser.parse(Parser.java:247)
at org.h2.command.Parser.prepare(Parser.java:201)
at org.h2.command.Parser.prepareCommand(Parser.java:214)
at org.h2.engine.Session.prepareLocal(Session.java:425)
at org.h2.engine.Session.prepareCommand(Session.java:374)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1056)
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:165)
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:153)
at com.xtremelabs.robolectric.shadows.ShadowSQLiteDatabase.execSQL(ShadowSQLiteDatabase.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.xtremelabs.robolectric.bytecode.ShadowWrangler.methodInvoked(ShadowWrangler.java:87)
at com.xtremelabs.robolectric.bytecode.RobolectricInternals.methodInvoked(RobolectricInternals.java:110)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java)
at com.google.android.apps.analytics.PersistentEventStore$DataBaseHelper.onCreate(Unknown Source)
at com.xtremelabs.robolectric.shadows.ShadowSQLiteOpenHelper.getWritableDatabase(ShadowSQLiteOpenHelper.java:52)
的问题是,Android使用SQLite数据库,但使用H 2支持略有不同风味的SQL robolectric是
The problem is that Android uses SQLite databases, but robolectric is using H2 which support a slightly different flavor of SQL.
什么是解决这个问题的最简单的方法?
What's the easiest way to get around this problem?
推荐答案
Robolectric现在提供@UsingDatabaseMap注解,这样其他数据库可以方便地与Roboelectric使用。 (目前,这需要Robolectric的快照版本,Robolectric-1.0-RC5-SNAPSHOT)
Robolectric now provides the @UsingDatabaseMap annotation so that other databases can easily be used with Roboelectric. (Currently this requires the SNAPSHOT version of Robolectric, Robolectric-1.0-RC5-SNAPSHOT)
如果有人愿意使用SQLite与Robolectric,我建议他们看看我在github上项目,该项目提供了SQLiteMap类,以便可以使用SQLite与Robolectric。
If anyone wishes to use SQLite with Robolectric, I recommend they check out my project on github which provides the SQLiteMap class so one can use SQLite with Robolectric.
https://github.com/cessationoftime/robolectric-sqlite/wiki
结果是注释使用的SQLite数据库,而不是H2测试类:
The result is test classes annotated to use the SQLite database instead of H2:
@UsingDatabaseMap(SQLiteMap.class)
@RunWith(RobolectricTestRunner.class)
public class DaoTest {
@Test
public void RobolectricSQLiteTest() {
}
}
这篇关于使用时的SQLException谷歌分析与Robolectric(或QUOT;尝试在Robolectric&QUOT使用sqlite3的;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!