gpt4 book ai didi

android - 将 Google Analytics 与 Robolectric(或 "Trying to use SQLite3 in Robolectric")一起使用时出现 SQLException

转载 作者:行者123 更新时间:2023-11-29 22:24:37 28 4
gpt4 key购买 nike

我正在使用 robolectric 来测试一项使用 Google Analytics 的 Activity 。不幸的是,每当我尝试启动 Activity 时,我都会遇到以下异常

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 数据库,但 robolectric 使用 H2,它支持稍微不同的 SQL 风格。

解决这个问题最简单的方法是什么?

最佳答案

Robolectric 现在提供@UsingDatabaseMap 注释,以便其他数据库可以轻松地与 Roboelectric 一起使用。 (目前这需要 Robolectric 的 SNAPSHOT 版本,Robolectric-1.0-RC5-SNAPSHOT)

如果有人希望将 SQLite 与 Robolectric 结合使用,我建议他们查看我在 github 上的项目,该项目提供了 SQLiteMap 类,因此可以将 SQLite 与 Robolectric 结合使用。

https://github.com/cessationoftime/robolectric-sqlite/wiki

结果是测试类被注释为使用 SQLite 数据库而不是 H2:

@UsingDatabaseMap(SQLiteMap.class)
@RunWith(RobolectricTestRunner.class)
public class DaoTest {
@Test
public void RobolectricSQLiteTest() {
}
}

关于android - 将 Google Analytics 与 Robolectric(或 "Trying to use SQLite3 in Robolectric")一起使用时出现 SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6283834/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com