我在创建具有 2 列的数据库时遇到问题。第一个应该有一个名字,第二个应该有一个数字。创建名称数据库没有问题,但是当我尝试添加第二列时,我收到错误消息“I/Database(13531): sqlite returned: error code = 1, msg = no such column: zahl”
我的代码在这里
首先是经理
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);
}
}
和我的数据库:
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("");
对于调试功能,第二列现在不活动。我真的希望你能帮助我。我现在被困了3天......
非常感谢