调用onClick函数时创建SQLite

时间:2016-06-18 16:30:48

标签: android sqlite android-sqlite sqliteopenhelper

我想将我的应用程序与SQLite数据库连接。只要有人点击“创建”按钮,就应立即生成数据库。我正在使用onClick函数,但是在调用函数时出现错误。这是调用onClick函数时执行的代码:

 public void createDb(View view)
    {
        DataHandler db = new DataHandler(this);
        db.addValue();
        Toast.makeText(getApplicationContext(),"DataBase Created Successfully.", Toast.LENGTH_SHORT).show();

    }

函数addValueDataHandler类中使用以下代码定义:

void addValue()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values=new ContentValues();
        Random rand=new Random();
        //for (int i=0;i<15;i++)
        {
            values.put(KEY_VALUE,rand.nextInt(2));
            db.insert(TABLE_DATA,null,values);

        }
        db.close();
        return ;

    }

我收到了这个错误:

06-18 09:19:33.429 3200-3200/com.example.mobileworkstation.database E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.mobileworkstation.database, PID: 3200
                                                                                  java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
                                                                                      at android.view.View.performClick(View.java:5198)
                                                                                      at android.view.View$PerformClick.run(View.java:21147)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                   Caused by: java.lang.reflect.InvocationTargetException
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                      at android.view.View.performClick(View.java:5198) 
                                                                                      at android.view.View$PerformClick.run(View.java:21147) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                   Caused by: android.database.sqlite.SQLiteException: near "TABLEDATA": syntax error (code 1): , while compiling: CREATE TABLEDATA(VALUEINTEGER )
                                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                                      at com.example.mobileworkstation.database.DataHandler.onCreate(DataHandler.java:28)
                                                                                      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                      at com.example.mobileworkstation.database.DataHandler.addValue(DataHandler.java:38)
                                                                                      at com.example.mobileworkstation.database.MainActivity.createDb(MainActivity.java:71)
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                                                                                      at android.view.View.performClick(View.java:5198) 
                                                                                      at android.view.View$PerformClick.run(View.java:21147) 
                                                                                      at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我已经阅读了很多教程,但我仍然无法让它工作。关于如何解决这个问题的任何建议?

0 个答案:

没有答案