在创建表并尝试从中检索记录时没有出现此类列错误

时间:2016-08-02 12:14:13

标签: android sqlite android-sqlite

我得到的表没有列名“变量名” 我得到的表没有列名“变量名”

表创建

在DBHelper.java中

db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_AssessmentValue.NAME + "("
                + TB_AssessmentValue.CL_1_BOOKID + " TEXT " 
                + TB_AssessmentValue.CL_2_ASMNTVALUE + " TEXT " + ");");

在LektzDB.java中

public static abstract class TB_AssessmentValue{
    public static final String NAME = "assessmentvalue";
    public static final String CL_1_BOOKID = "book_id";
    public static final String CL_2_ASMNTVALUE = "asmntvalue";
}

用于删除插入和获取的注意事项

ReaderDB.java中的

public void deleteAssessmentValue(Context context) {
    try {
        Log.i("tag123", "msgdelete1");
        DBHelper dbh = new DBHelper(context, LektzDB.DB_NAME, null,
                LektzDB.DB_VERSION);
        SQLiteDatabase db = dbh.getWritableDatabase();
        db.execSQL("DELETE FROM " + TB_AssessmentValue.NAME );
        db.close();
        dbh.close();
        Log.i("tag123", "msgdelete2");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void insertAssessmentValue(Context context, String id,String assmentvalue) {
    DBHelper dbh = new DBHelper(context, LektzDB.DB_NAME, null,
            LektzDB.DB_VERSION);
    SQLiteDatabase db = dbh.getWritableDatabase();
    try {
        ContentValues cv = new ContentValues();
        cv.put(TB_AssessmentValue.CL_1_BOOKID, id);
        cv.put(TB_AssessmentValue.CL_2_ASMNTVALUE, assmentvalue);
        db.insert(TB_AssessmentValue.NAME, null, cv);
        db.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public String getContact(Context context , String id ) {
    DBHelper dbh = new DBHelper(context , LektzDB.DB_NAME, null,
        LektzDB.DB_VERSION);
 SQLiteDatabase db = dbh.getReadableDatabase();
     String asmntValue = "";
    try{
         Cursor cursor = db.
                 rawQuery("select * from "+ TB_AssessmentValue.NAME +" where _id = 1", new String[] { id });

        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            asmntValue = cursor.getString(cursor.getColumnIndex(TB_AssessmentValue.CL_2_ASMNTVALUE));

        }
        cursor.close();
        return asmntValue;
    }finally {

    }
}

并通过

执行这些 Fragment.java中的

                        ReaderDB rdb = new ReaderDB();
                 rdb.insertAssessmentValue(getContext(),bid, asmntValue);
                 rdb.insertAssessmentValue(getContext(),bid, asmntValue);
                  String a = rdb.getContact(getContext(), bid );
Logcat中的

 08-03 10:02:07.627: E/SQLiteLog(20321): (1) table assessmentvalue has no column named asmntvalue
08-03 10:02:07.667: E/SQLiteDatabase(20321): Error inserting asmntvalue=[{"id":"1","question_type":"","added_time":"1470197096","chapter_id":"1","book_id":"64","mark":"1","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"1","matchOption":""},{"id":"2","question_type":"","added_time":"1470197098","chapter_id":"1","book_id":"64","mark":"0","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"2","matchOption":""},{"id":"3","question_type":"match","added_time":"1470197160","chapter_id":"1","book_id":"64","mark":"2","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"1","matchOption":"[5,2,1,4,3]"},{"id":"4","question_type":"drag","added_time":"1470197139","chapter_id":"1","book_id":"64","mark":"0","user_id":"1","dragValues":"[0,5,2,1,4,3]","option_id":"1","notes":"","question_id":"1","matchOption":""},{"id":"5","question_type":"hint","added_time":"1470197096","chapter_id":"1","book_id":"64","mark":"1","user_id":"1","dragValues":null,"option_id":"1","notes":"Kkkkkkkkkkkkkd,Aeldata1jjjjnjnj,Aeldata2hghgh,Aeldata333,Aeldata4hjbhjbjnj,Aeldata5,Aeldata6!bhjjnn","question_id":"1","matchOption":null}] book_id=64
08-03 10:02:07.667: E/SQLiteDatabase(20321): android.database.sqlite.SQLiteException: table assessmentvalue has no column named asmntvalue (code 1): , while compiling: INSERT INTO assessmentvalue(asmntvalue,book_id) VALUES (?,?)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1573)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.db.ReaderDB.insertAssessmentValue(ReaderDB.java:78)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.fragment.DoublePageFragment.loadPage(DoublePageFragment.java:762)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.fragment.DoublePageFragment.onCreateView(DoublePageFragment.java:254)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:720)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:592)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
08-03 10:02:07.667: E/SQLiteDatabase(20321):    at android.view.View.measure(Vie
08-03 10:02:07.667: I/tag123(20321): msginsert2
08-03 10:02:07.667: I/tag123(20321): msginsertsuccess
08-03 10:02:07.677: I/tag123(20321): msginsert1
08-03 10:02:07.677: E/SQLiteLog(20321): (1) table assessmentvalue has no column named asmntvalue
08-03 10:02:07.677: E/SQLiteDatabase(20321): Error inserting asmntvalue=[{"id":"1","question_type":"","added_time":"1470197096","chapter_id":"1","book_id":"64","mark":"1","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"1","matchOption":""},{"id":"2","question_type":"","added_time":"1470197098","chapter_id":"1","book_id":"64","mark":"0","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"2","matchOption":""},{"id":"3","question_type":"match","added_time":"1470197160","chapter_id":"1","book_id":"64","mark":"2","user_id":"1","dragValues":"","option_id":"1","notes":"","question_id":"1","matchOption":"[5,2,1,4,3]"},{"id":"4","question_type":"drag","added_time":"1470197139","chapter_id":"1","book_id":"64","mark":"0","user_id":"1","dragValues":"[0,5,2,1,4,3]","option_id":"1","notes":"","question_id":"1","matchOption":""},{"id":"5","question_type":"hint","added_time":"1470197096","chapter_id":"1","book_id":"64","mark":"1","user_id":"1","dragValues":null,"option_id":"1","notes":"Kkkkkkkkkkkkkd,Aeldata1jjjjnjnj,Aeldata2hghgh,Aeldata333,Aeldata4hjbhjbjnj,Aeldata5,Aeldata6!bhjjnn","question_id":"1","matchOption":null}] book_id=64
08-03 10:02:07.677: E/SQLiteDatabase(20321): android.database.sqlite.SQLiteException: table assessmentvalue has no column named asmntvalue (code 1): , while compiling: INSERT INTO assessmentvalue(asmntvalue,book_id) VALUES (?,?)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1573)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.db.ReaderDB.insertAssessmentValue(ReaderDB.java:78)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.fragment.DoublePageFragment.loadPage(DoublePageFragment.java:764)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at com.aeldata.eduflex.fragment.DoublePageFragment.onCreateView(DoublePageFragment.java:254)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:720)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:592)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.View.measure(View.java:16482)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
08-03 10:02:07.677: E/SQLiteDatabase(20321):    at android.view.View.measure(Vie
08-03 10:02:07.677: I/tag123(20321): msginsert2
08-03 10:02:07.677: I/tag123(20321): msginsertsuccess
08-03 10:02:07.677: I/tag123(20321): msgget1

3 个答案:

答案 0 :(得分:1)

您忘记了创建表格中的逗号。

SQL CREATE TABLE语法

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

来自http://www.w3schools.com/sql/sql_create_table.asp

的信息

它应该是这样的:

db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_AssessmentValue.NAME + "("
                + TB_AssessmentValue.CL_1_BOOKID + " TEXT, " 
                + TB_AssessmentValue.CL_2_ASMNTVALUE + " TEXT " + ")");

答案 1 :(得分:1)

getContact()你犯了两个错误,

更改此代码

Cursor cursor = db.
                rawQuery("select * from TB_AssessmentValue where _id = 1", new String[] { id });

Cursor cursor = db.
                    rawQuery("select * from "+ TB_AssessmentValue.NAME +" where _id = 1", new String[] { id });

同样更新以下代码

asmntValue = cursor.getString(cursor.getColumnIndex(TB_AssessmentValue.CL_2_ASMNTVALUE)

最后查询错了必须以;如下

db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_AssessmentValue.NAME + "("
            + TB_AssessmentValue.CL_1_BOOKID + " TEXT " 
            + TB_AssessmentValue.CL_2_ASMNTVALUE + " TEXT " + ");");

希望这有帮助!

答案 2 :(得分:0)

完成这些更改

db.execSQL("CREATE TABLE IF NOT EXISTS " + TB_AssessmentValue.NAME + "("
            + TB_AssessmentValue.CL_1_BOOKID + " TEXT," 
            + TB_AssessmentValue.CL_2_ASMNTVALUE + " TEXT " + ")");

  Cursor  cursor = db.rawQuery("SELECT * FROM "
                + TB_AssessmentValue.NAME + " where "
                + TB_AssessmentValue.CL_1_BOOKID + "='"+ id + "'",
                null);