android.database.sqlite.SQLiteException:表没有列名(代码1)

时间:2015-08-12 03:07:25

标签: android

我想知道是否有人可以帮助我 我收到错误消息称column_one不存在。
但我把它包含在我的DBHandler中 我已经查看了一些其他答案,说这可能是由于错过了空格或逗号,但我似乎无法找到它。

DBHandler.java

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Database.db";
    public static final String TABLE_TEST = "test";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_ONE = "one";
    public static final String COLUMN_TWO = "two";

    public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_TEST + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_ONE + " TEXT," +
                COLUMN_TWO + " TEXT" + ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TEST);
        onCreate(db);
    }

    public void addTest(Test test){
        ContentValues values = new ContentValues();
        values.put(COLUMN_ONE, test.get_one());
        values.put(COLUMN_TWO, test.get_two());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_TEST, null, values);
        db.close();
    }

}

Test.java

public class Test {

    private int _id;
    private String _one;
    private String _two;

    public Test(){

    }

    public Test(int id){
        this._id = id;
    }

    public Test(String one, String two){
        this._one = one;
        this._two = two;
    }

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String get_one() {
        return _one;
    }

    public void set_one(String _one) {
        this._one = _one;
    }

    public String get_two() {
        return _two;
    }

    public void set_two(String _two) {
        this._two = _two;
    }

我的主要活动非常长并且还有其他多个项目,所以这里的重要位(代码中前面声明的dbHandler)

Test test = new Test ("Test One", "Test Two");
dbHandler.addTest(test);

如果有人可以查看我的代码,我会很感激。

编辑:logcat

08-12 09:39:00.358    2305-2305/com.test.test E/SQLiteLog﹕ (1) table test has no column named one
08-12 09:39:00.380    2305-2305/com.test.test E/SQLiteDatabase﹕ Error inserting one=Random two=Random
    android.database.sqlite.SQLiteException: table test has no column named one (code 1): , while compiling: INSERT INTO test(one,two) VALUES (?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            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:1469)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
            at com.test.test.DBHandler.addTest(DBHandler.java:54)
            at com.test.test.MainActivity.onClick(MainActivity.java:58)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

2 个答案:

答案 0 :(得分:4)

表名未初始化为String

public static final String TABLE_TEST = test

您实际上是在将String初始化为另一个(null)对象变量。

你需要双引号String,使其成为一个字符串:

public static final String TABLE_TEST = "test"

现在卸载并重新安装您的应用 数据库将被删除,因此在下次运行时,它将被重新创建。

或者,增加 DATABASE_VERSION 常量的值 通过这样做,onUpgrade()方法将触发,删除表并重新创建它。

答案 1 :(得分:-1)

你的sql是“创建表测试(_id整数主键自动增量,一个文本,两个文本);”,我在终端尝试了它没关系。您可以添加sql变量的一行日志来检查它是什么。您提到“column_one不存在”,也许您在插入时拼错了列名。