如何调试" SQL(查询)错误或丢失数据库"在Android应用中?

时间:2017-10-01 20:45:19

标签: android kotlin

10-02 01:31:57.697 22242-22242/com.example.android.mynotes E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.mynotes, PID: 22242
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
        at android.view.View.performClick(View.java:5697)
        at android.widget.TextView.performClick(TextView.java:10826)
        at android.view.View$PerformClick.run(View.java:22526)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7224)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
        at android.view.View.performClick(View.java:5697) 
        at android.widget.TextView.performClick(TextView.java:10826) 
        at android.view.View$PerformClick.run(View.java:22526) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7224) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
     Caused by: android.database.sqlite.SQLiteException: near "EXISTName": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTName(ID INTEGER PRIMARY KEY,TitleTEXT,DescriptionTEXT);
    #################################################################
    Error Code : 1 (SQLITE_ERROR)
    Caused By : SQL(query) error or missing database.
       (near "EXISTName": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTName(ID INTEGER PRIMARY KEY,TitleTEXT,DescriptionTEXT);)
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
        at com.example.android.mynotes.DbManager$DatabaseHelperNotes.onCreate(DbManager.kt:32)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at com.example.android.mynotes.DbManager.<init>(DbManager.kt:22)
        at com.example.android.mynotes.AddNotes.BtnAdd(AddNotes.kt:19)
           ... 12 more

2 个答案:

答案 0 :(得分:1)

CREATE TABLE IF NOT EXISTName

您在关键字EXIST和表名Name之间缺少空格。

答案 1 :(得分:0)

您只需在try-catch块中添加代码块,然后在其上放置调试点并确定异常并进行更正。

e.g

try
{
 //block of code in which you have an error or you want to debug
}
catch(e: Exception)
{
  // you can use log for seeing error or you can simply view errors by 
  //putting debug point and pressing alt+mouse_left_click after executing 
  //line to get value of exception instance
}