1

I was wondering if someone could help me.
I'm getting an error saying that column_one doesn't exist.
But I included it in my DBHandler.
I've looked through a few other answers that say it's likely due to a missed space or comma, but I can't seem to find it.

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;
    }

My main Activity is extremely long and has multiple other items, so here's the important bit (dbHandler declared earlier in the code)

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

I'd appreciate if someone could take a look at my code.

Edit: 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
  • 2
    This String is not initialized to a "string": public static final String TABLE_TEST = test Commented Aug 12, 2015 at 3:10
  • Thanks for the replies. Posted the logcat and changed test to "test" but still getting the same error. Commented Aug 12, 2015 at 13:43

2 Answers 2

4

The table name is not initialized to a String

public static final String TABLE_TEST = test

You are actually initializing the String to another (null) object variable.

You need to double quote a String, to make it a String:

public static final String TABLE_TEST = "test"

Now uninstall and reinstall your app.
The db will be removed, so at next run, it will be recreated.


Alternatively, increase the value of the DATABASE_VERSION constant.
By doing so, the onUpgrade() method will fire, deleting the table and recreating it new.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply. I've double quoted test, but unfortunately am still getting the same error.
Now uninstall and reinstall your app. The db will be removed, so at next run, it will be recreated.
Strangely, it works when I connect a device but not when I use an emulator. Thanks for the help.
Great explanation and answer. Thank you!
I believe second part is very helpful. Once you face some error during the db connection program, you need to first uninstall the app (or increase the db version) to see the new changes. I was applying correct changes but still not getting the results because I was not uninstalling the app. Thanks!
|
-1

Your sql is "create table test (_id integer primary key autoincrement,one text,tow text);", and I tried it in terminal and it is ok. You can add a line of log of the sql variable to inspect what it is. You referred that "column_one doesn't exists", perhaps you misspell the column name when insert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.