我想知道是否有人可以帮助我。
我收到一条错误消息,说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)

最佳答案

表名未初始化为字符串

public static final String TABLE_TEST = test


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

您需要双引号字符串,使其成为字符串:

public static final String TABLE_TEST = "test"




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



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

07-27 23:46