Question

I have a problem creating a database with 2 columns. In the first one there should be a name and in the second one a number. Creating the name database is no problem, but when I try to add the second column I get the error message "I/Database(13531): sqlite returned: error code = 1, msg = no such column: zahl"

my code is here

first the Manager

package my.studienarbeit.bestimmung.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatenbankManager extends SQLiteOpenHelper {

private static final String DB_NAME = "carbohydrates.db";
private static final int DB_VERSION = 1;

private static final String WEIGHT_CREATE = 
         "CREATE TABLE weight (" 
        + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
        +   "name TEXT NOT NULL, " +
    //  +   "zahl INTEGER" +
        ")";


public DatenbankManager(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(WEIGHT_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DatenbankManager.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS SCANITEM");
    onCreate(db);
}

}

and my database:

package my.studienarbeit.bestimmung.database;

import my.studienarbeit.bestimmung.R;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Datenbanken extends ListActivity {

private SQLiteDatabase mDatenbank;
private DatenbankManager mHelper;
/*/ private static final String WEIGHT_SELECT_RAW = 
        "SELECT _id, name, zahl FROM weight" ; //*/

//*/
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.daten_datenbanken);
    mHelper = new DatenbankManager(this);
    mHelper.getWritableDatabase();

}

@Override
protected void onPause() {
    super.onPause();
    mDatenbank.close();
    Toast.makeText(this, 
                 getResources().getString(R.string.tx_daten_datenbanken_db_geschlossen), 
            Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
    super.onResume();
    mDatenbank = mHelper.getWritableDatabase();
    Toast.makeText(
            this, 
            getResources().getString(
                    R.string.tx_daten_datenbanken_db_geoeffnet
                    ), 
            Toast.LENGTH_SHORT)
    .show();
    ladeDaten();

}

private void ladeDaten() {
     // Cursor weightCursor = mDatenbank.rawQuery(WEIGHT_SELECT_RAW, null);
Cursor weightCursor = mDatenbank.query("weight",
            new String [] {
                    "_id",
                    "name",
                //  "zahl",
            },
            null, null, null, null, null                
        ); 

    startManagingCursor(weightCursor);  
    SimpleCursorAdapter weightAdapter =
            new SimpleCursorAdapter(this, 
                    android.R.layout.simple_list_item_2, 
                    weightCursor, 
                    new String [] {
                    "name", 
                //  "zahl"
                    }, 
                    new int[] {
                        android.R.id.text1
                    }

            );

setListAdapter(weightAdapter);
}

public void onNavButtonClick(View view) {
    EditText et_c = 
            (EditText) findViewById(R.id.editText_name_c);
//  EditText et_w = 
    //      (EditText) findViewById(R.id.editText_weight_c);

    ContentValues werte = new ContentValues();
    werte.put("name", et_c.getText().toString());
//  werte.put("zahl", et_w.getText().toString());
    mDatenbank.insert("weight", null, werte);
    ladeDaten();
    et_c.setText("");
//  et_w.setText("");

The 2nd column is not active right now for debug functions. I really hope you can help me. I am stucked on that for 3 days now...

Thanks a lot

Was it helpful?

Solution

If you first created your table without the "zahl" column, then added the "zahl" column and rerun your application, the OpenHelper did not do the "onCreate" again with the new values. My suggestion is that you first drop the table (on emulator, go to Settings > Applications > Yourapp > Click "Clear Data" button), then uncomment the "zahl" lines (especially in DatenbankManager.java), then recompile + run. Running your app again, with an empty database, will recreate the table with column "zahl". Thomas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top