Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以从Android SQLiteConstraintException获取特定的错误详细信息?_Android_Sqlite_Sqlbrite - Fatal编程技术网

是否可以从Android SQLiteConstraintException获取特定的错误详细信息?

是否可以从Android SQLiteConstraintException获取特定的错误详细信息?,android,sqlite,sqlbrite,Android,Sqlite,Sqlbrite,对于某些数据,我得到了以下错误,概念非常清楚: android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787) 解决方案1: 您可以使用adb shell dumpsys dbinfo-v命令查看有关数据库的一些信息 在我的例子中,我创建了一个小示例,它两次插入相同的记录,强制执行sqliteconstraintextException: MyDB db = new MyDB(

对于某些数据,我得到了以下错误,概念非常清楚:

android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787) 解决方案1: 您可以使用
adb shell dumpsys dbinfo-v
命令查看有关数据库的一些信息

在我的例子中,我创建了一个小示例,它两次插入相同的记录,强制执行
sqliteconstraintextException

MyDB db = new MyDB(this);

// Insert the same record twice to force a SQLiteConstraintException
db.insertIntoMyTable("1","MyName");
db.insertIntoMyTable("1","MyName");
E/SQLiteDatabase(22296): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: MyTable._id (code 1555)
这是Nexus4中adb shell dumpsys dbinfo-v命令的输出(我不确定,但我认为不同设备的输出信息可能不同):

解决方案2: 这个解决方案有点棘手,但是您可以使用
android.database.sqlite.SQLiteDebug
类通过Java代码获得类似的信息

如果查看一下,您将看到这个类被标记为
@hide
,这意味着SQLiteDebug类被从API文档中排除,但仍然可以通过反射访问它

因此,要打印SQLite数据库的调试信息,可以使用以下代码:

// The messages will be printed as Log.ERROR using the tag "DB"
Printer p = new LogPrinter(Log.ERROR, "DB");
// We will invoke the "dump" method of the "android.database.sqlite.SQLiteDebug" class
// We eill use the "-v" parameter because we want the output to be verbose
Class<?> c = Class.forName("android.database.sqlite.SQLiteDebug");
Method method = c.getMethod("dump", new Class[]{Printer.class, String[].class});
method.invoke(null, p, new String[]{"-v"});
分析输出,我得到一个
sqliteconstraintextException

MyDB db = new MyDB(this);

// Insert the same record twice to force a SQLiteConstraintException
db.insertIntoMyTable("1","MyName");
db.insertIntoMyTable("1","MyName");
E/SQLiteDatabase(22296): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: MyTable._id (code 1555)
在最近执行的
操作中:
我得到以下信息(一条记录成功插入,另一条记录失败):

希望能有所帮助。

您可以简单地将其加载到SQLite浏览器应用程序中。因此,您现在可以尝试执行相同的事务,并查看发生了什么

SQLite浏览器:


我更新了日志,在我自己的内部代码之上添加了所有日志。可能还需要sql。您可能正在尝试从一个表中删除一条记录,该表在另一个表中有引用(或者尝试通过使用空Id引用该记录来删除该记录?)嗨,我不是在试图解决这个问题,我是在尝试找到更好的工具来解决此问题和未来的问题。我确信其中一条记录有一个外键指向服务器上存在但本地数据库中不存在的行(这是在使用刚从服务器检索的数据创建新行时发生的)。我已经通过将本地表列作为文本字段而不是引用解决了这样一个问题。我只是不明白为什么访问我需要的数据(哪个表、列和数据值导致了问题)的代码不报告它。嗨,谢谢-我确实使用SQLite Studio,并且经常在本地拉DB来检查和交互。然而,所讨论的SQL可能有数百行——我正在检查生产数据,以找出其中哪一行有问题。当我将所有提交分解为单独的事务以隔离问题行时,我已经完成了我试图保存的大部分工作,并且在DB的本地副本上的一些查询确实向我显示了哪个FK引用了丢失的行。这对我来说并不完全一样,由于我有事务并且在提交时出错,但是当我没有启用DB调用的日志记录时,这是一个很好的工具。我仍然需要将插入内容分割成单独的事务来追踪问题,但我会奖励赏金,因为这是对我的工具箱的一个有用的补充。我回来并实现了一个记录器,它使用您的方法2从转储中提取信息。通过一些创造性的正则表达式操作,我可以使用查询参数(没有敏感数据,我无法在生产中跟踪)刮取有问题的行,这恰好是在它表示失败后的行,因为我使用事务。只要我将每个事务1个潜在麻烦的命令隔离出来,这看起来在生产中可能会很有用。很高兴听到该解决方案在producton环境中有所帮助,但在使用正则表达式时,请考虑到不同设备的输出可能不同(至少我测试过的两种不同设备的输出不同)。谢谢您的后续操作。
MyDB db = new MyDB(this);

// Insert the same record twice to force a SQLiteConstraintException
db.insertIntoMyTable("1","MyName");
db.insertIntoMyTable("1","MyName");

try {
    Printer p = new LogPrinter(Log.ERROR, "DB");
    Class<?> c = Class.forName("android.database.sqlite.SQLiteDebug");
    Method method = c.getMethod("dump", new Class[]{Printer.class, String[].class});
    method.invoke(null, p, new String[]{"-v"});
}
catch (Exception e) {
    Log.e("MyTag", e.getMessage());
}
E/SQLiteLog(22296): (1555) abort at 10 in [INSERT INTO MyTable(name,_id) VALUES (?,?)]: UNIQUE constraint failed: MyTable._id
E/SQLiteDatabase(22296): Error inserting name=MyName _id=1
E/SQLiteDatabase(22296): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: MyTable._id (code 1555)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
E/SQLiteDatabase(22296):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
E/SQLiteDatabase(22296):    at com.example.antonio.prueba3.MyDB.insertIntoMyTable(MyDB.java:32)
E/SQLiteDatabase(22296):    at com.example.antonio.prueba3.MainActivity.onCreate(MainActivity.java:24)
E/SQLiteDatabase(22296):    at android.app.Activity.performCreate(Activity.java:5990)
E/SQLiteDatabase(22296):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
E/SQLiteDatabase(22296):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
E/SQLiteDatabase(22296):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
E/SQLiteDatabase(22296):    at android.app.ActivityThread.access$800(ActivityThread.java:151)
E/SQLiteDatabase(22296):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
E/SQLiteDatabase(22296):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/SQLiteDatabase(22296):    at android.os.Looper.loop(Looper.java:135)
E/SQLiteDatabase(22296):    at android.app.ActivityThread.main(ActivityThread.java:5254)
E/SQLiteDatabase(22296):    at java.lang.reflect.Method.invoke(Native Method)
E/SQLiteDatabase(22296):    at java.lang.reflect.Method.invoke(Method.java:372)
E/SQLiteDatabase(22296):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
E/SQLiteDatabase(22296):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
E/DB      (22296): Connection pool for /data/data/com.example.antonio.prueba3/databases/MyDB:
E/DB      (22296):   Open: true
E/DB      (22296):   Max connections: 1
E/DB      (22296):   Available primary connection:
E/DB      (22296):     Connection #0:
E/DB      (22296):       connectionPtr: 0xffffffffb883aaf0
E/DB      (22296):       isPrimaryConnection: true
E/DB      (22296):       onlyAllowReadOnlyOperations: false
E/DB      (22296):       Most recently executed operations:
E/DB      (22296):         0: [2015-11-13 17:59:16.227] executeForLastInsertedRowId took 1ms - failed, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)", bindArgs=["MyName", "1"], exception="UNIQUE constraint failed: MyTable._id (code 1555)"
E/DB      (22296):         1: [2015-11-13 17:59:16.227] prepare took 0ms - succeeded, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)"
E/DB      (22296):         2: [2015-11-13 17:59:16.209] executeForLastInsertedRowId took 18ms - succeeded, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)", bindArgs=["MyName", "1"]
E/DB      (22296):         3: [2015-11-13 17:59:16.209] prepare took 0ms - succeeded, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)"
E/DB      (22296):         4: [2015-11-13 17:59:16.208] executeForLong took 1ms - succeeded, sql="PRAGMA user_version;"
E/DB      (22296):         5: [2015-11-13 17:59:16.208] prepare took 0ms - succeeded, sql="PRAGMA user_version;"
E/DB      (22296):         6: [2015-11-13 17:59:16.208] executeForString took 0ms - succeeded, sql="SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1"
E/DB      (22296):         7: [2015-11-13 17:59:16.207] execute took 1ms - succeeded, sql="CREATE TABLE IF NOT EXISTS android_metadata (locale TEXT)"
E/DB      (22296):         8: [2015-11-13 17:59:16.207] executeForLong took 0ms - succeeded, sql="PRAGMA wal_autocheckpoint=100"
E/DB      (22296):         9: [2015-11-13 17:59:16.207] executeForLong took 0ms - succeeded, sql="PRAGMA wal_autocheckpoint"
E/DB      (22296):         10: [2015-11-13 17:59:16.207] executeForLong took 0ms - succeeded, sql="PRAGMA journal_size_limit=524288"
E/DB      (22296):         11: [2015-11-13 17:59:16.207] executeForLong took 0ms - succeeded, sql="PRAGMA journal_size_limit"
E/DB      (22296):         12: [2015-11-13 17:59:16.206] executeForString took 0ms - succeeded, sql="PRAGMA synchronous"
E/DB      (22296):         13: [2015-11-13 17:59:16.206] executeForString took 0ms - succeeded, sql="PRAGMA journal_mode=PERSIST"
E/DB      (22296):         14: [2015-11-13 17:59:16.205] executeForString took 1ms - succeeded, sql="PRAGMA journal_mode"
E/DB      (22296):         15: [2015-11-13 17:59:16.204] executeForLong took 0ms - succeeded, sql="PRAGMA foreign_keys"
E/DB      (22296):         16: [2015-11-13 17:59:16.204] executeForLong took 0ms - succeeded, sql="PRAGMA page_size"
E/DB      (22296):       Prepared statement cache:
E/DB      (22296):         0: statementPtr=0xffffffffb8839558, numParameters=0, type=1, readOnly=true, sql="SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1"
E/DB      (22296):   Available non-primary connections:
E/DB      (22296):     <none>
E/DB      (22296):   Acquired connections:
E/DB      (22296):     <none>
E/DB      (22296):   Connection waiters:
E/DB      (22296):     <none>
E/SQLiteDatabase(22296): android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: MyTable._id (code 1555)
E/DB      (22296):         0: [2015-11-13 17:59:16.227] executeForLastInsertedRowId took 1ms - failed, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)", bindArgs=["MyName", "1"], exception="UNIQUE constraint failed: MyTable._id (code 1555)"
E/DB      (22296):         2: [2015-11-13 17:59:16.209] executeForLastInsertedRowId took 18ms - succeeded, sql="INSERT INTO MyTable(name,_id) VALUES (?,?)", bindArgs=["MyName", "1"]