android contentprovider insert 時候通過contentvalues的時候失敗

這個問題困擾了我一段時間,因爲最近一直困擾我的小問題

1: 


public class MySqliteOpenHelper extends SQLiteOpenHelper {
    
    public static final String CREATE_TABLE_STUDENT = "CREATE TABLE " + DB_TABLE_STUDENT + " ( text "+ DB_TABLE_STUDENT_NAME + "  )";

相當於 create table student ( text name )

 

2: 我插入的時候:

ContentValues contentValues = new ContentValues();
        contentValues.put(DB_TABLE_STUDENT_NAME,"hello" + MySqliteOpenHelper.num++);
        

3: 然後在我自定義的ContentProvider 

        long insert = writableDatabase.insert(MySqliteOpenHelper.DB_TABLE_STUDENT, null, values);

4: 最後報錯: 這個問題最後我發現有報錯


2019-05-22 21:47:32.968 5180-5180/bjpkten.contentproviderdemo30min E/SQLiteLog: (1) table student has no column named NAME
2019-05-22 21:47:32.972 5180-5180/bjpkten.contentproviderdemo30min E/SQLiteDatabase: Error inserting NAME=hello2
    android.database.sqlite.SQLiteException: table student has no column named NAME (code 1 SQLITE_ERROR): , while compiling: INSERT INTO student(NAME) VALUES (?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        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.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at bjpkten.contentproviderdemo30min.MyContentProvider.insert(MyContentProvider.java:63)
        at android.content.ContentProvider$Transport.insert(ContentProvider.java:265)
        at android.content.ContentResolver.insert(ContentResolver.java:1587)
        at bjpkten.contentproviderdemo30min.MainActivity.queryAndInsert(MainActivity.java:41)
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

 

 

最後發現是sql語句的問題

應該是  create table student ( name text ) 列名在前面 name 在前面,類型在後面,尷尬?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章