数据库插入失败

时间:2013-08-05 07:32:26

标签: android sqlite

我的程序包含一个name= countable的数据库。首先,它只包含3列,并且工作正常。然后我插入了一个名为countevent的新列,其变量名为KEY_EVENT。然后,在>程序显示错误后,因为列名countevent不存在。请帮我找一个>解决方案。

  

FirstActivity.java包含以下代码。

class clicker implements Button.OnClickListener {
        public void onClick(View v) {
            String datevalue = date.getText().toString();
            String Userselectvalue = userSelection.getText().toString();
            String Userevent = edittext.getText().toString();
            SQLiteDatabase  db = eventsData.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(classdbOpenHelper.KEY_DESC, Userselectvalue);
            cv.put(classdbOpenHelper.KEY_EVENT, Userevent);
            cv.put(classdbOpenHelper.KEY_DATE,datevalue);
            db.insert(classdbOpenHelper.DATABASE_TABLE, null, cv);
            db.close();
  

ClassdbOpenHelper

package example.showevent1;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;


public class classdbOpenHelper extends SQLiteOpenHelper {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_DESC = "countdesc";
    public static final String KEY_EVENT = "countevent";
    public static final String KEY_DATE = "countdate";

    public static final String DATABASE_NAME= "countdb";
    public static final String DATABASE_TABLE = "countable";
    public static final int DATABASE_VERSION = 1;


    public classdbOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
       /* db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
        //KEY_COUNTED + " INTEGER " +
        KEY_DESC + " TEXT NOT NULL " +
        KEY_DATE + " TEXT NOT NULL);"
                ); */
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT " + KEY_DESC + " TEXT " + KEY_EVENT + " TEXT " + KEY_DATE + " TEXT " + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
        onCreate(db);

    }
    String getContact() {
        SQLiteDatabase db = this.getReadableDatabase();

        int _id = 0;
        Cursor cursor = db.query(DATABASE_TABLE , new String[] { KEY_ROWID,
                KEY_DESC,KEY_EVENT, KEY_DATE}, KEY_ROWID + "=?",
                new String[] { String.valueOf(_id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
      String  place = cursor.getString( cursor.getColumnIndex("KEY_EVENT"));
        return place;
    }
}
  

logat

08-05 07:43:26.918: E/SQLiteLog(838): (1) table countable has no column named countevent
08-05 07:43:26.978: E/SQLiteDatabase(838): Error inserting countdesc=Yalahanka countevent=hh countdate=8-5-2013
08-05 07:43:26.978: E/SQLiteDatabase(838): android.database.sqlite.SQLiteException: table countable has no column named countevent (code 1): , while compiling: INSERT INTO countable(countdesc,countevent,countdate) VALUES (?,?,?)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at example.showevent1.FirstActivity$clicker.onClick(FirstActivity.java:148)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.view.View.performClick(View.java:4204)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.view.View$PerformClick.run(View.java:17355)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.os.Handler.handleCallback(Handler.java:725)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.os.Looper.loop(Looper.java:137)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at android.app.ActivityThread.main(ActivityThread.java:5041)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at java.lang.reflect.Method.invokeNative(Native Method)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at java.lang.reflect.Method.invoke(Method.java:511)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-05 07:43:26.978: E/SQLiteDatabase(838):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您错过了“,”在列之间登录数据库创建定义:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + DATABASE_TABLE + "("
            + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_EVENT + " TEXT, " + KEY_DATE + " TEXT " + ")";

答案 1 :(得分:0)

尝试更改数据库版本然后安装它。如果它不起作用,则卸载应用程序然后尝试安装它。检查您的sqlite表创建查询语法

答案 2 :(得分:0)

如果使用相同的数据库名称,则需要在onupgrade方法中添加新列,如果要为数据库指定新名称,则在oncreate中创建表。

db.execSQL(“create table DATABASE_TABLE(KEY_ROWID Integer primary key,KEY_DESC text,”+                     “KEY_EVENT text,KEY_DATE text);”);

希望这会对你有所帮助。无需在任何地方给出“+”,因为它会对打开和关闭引号(“”)产生更多混淆。

由于