获取未创建列的错误

时间:2012-03-16 20:09:57

标签: android sqlite

我正在创建一个包含2列的表,但是我收到的错误是未创建一列(我的代码中的attendance列)。我不知道为什么会出现这个错误。

DbAdapter.java

    package com.android.layout;

   import android.content.ContentValues;
   import android.content.Context;
   import android.database.Cursor;
   import android.database.SQLException;
   import android.database.sqlite.SQLiteDatabase;
   import android.database.sqlite.SQLiteOpenHelper;


   public class DBAdapter 
   {
public static final String KEY_ROLLNO = "rollno";
public static final String KEY_ATTENDANCE = "attendance";//this column is not getting created    


private static final String DATABASE_NAME = "mcr";
private static final String DATABASE_TABLE = "student";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table student(rollno text not null,attendance integer);";

private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {

    }
}    

//---opens the database---
public DBAdapter open() throws SQLException 
{  DBHelper=new DatabaseHelper(context);
   db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}



  public long insertRow(String rollno,int mark) 
  {
  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_ROLLNO, rollno);
  initialValues.put(KEY_ATTENDANCE, mark);
  return db.insert(DATABASE_TABLE, null, initialValues);
  }
  public Cursor getAllRows() 
 {
  return db.query(DATABASE_TABLE, new String[] {
        KEY_ROLLNO}, 
        null, 
        null, 
        null, 
        null, 
        null);
 }
 public boolean update(String rollno,int mark){
ContentValues args=new ContentValues();
args.put(KEY_ROLLNO, rollno);
args.put(KEY_ATTENDANCE, mark);
return db.update(DATABASE_TABLE, args, KEY_ROLLNO + "="+ rollno,null)>0;

 }
 }

DynamicActivity 类如下     包com.android.layout;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

    public class DynamicActivity extends Activity {
        /** Called when the activity is first created. */
        DBAdapter db = new DBAdapter(this);
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            char[] save={'s','a','v','e'};

            db.open();        
            long id;
            for(int i=0;i<25;i++){
            id = db.insertRow("08241A05"+(i+10),1);

            }
            Cursor c=db.getAllRows();
            startManagingCursor(c);        
            c.moveToFirst();

            ScrollView sv=new ScrollView(this);
            TableLayout tl=new TableLayout(this);

            for(int i=0;i<25;i++){
                TableRow tr=new TableRow(this);

                TextView tv= new TextView(this);
                tv.setText(c.getString(c.getColumnIndexOrThrow(db.KEY_ROLLNO)));
                tv.setPadding(0, 1, 10, 1);
                tr.addView(tv);
                c.moveToNext();
                CheckBox cb=new CheckBox(this);
                cb.setChecked(true);
                cb.setId(i+10);

                tr.addView(cb);

                tl.addView(tr);
            }
            Button saveButton=new Button(this);

            saveButton.setText(save,0,4);
            saveButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    update();
                }
            });
            tl.addView(saveButton);
            sv.addView(tl);
            setContentView(sv);

        }

        protected void update() {

        CheckBox cb1=(CheckBox)findViewById(11);
        if(cb1.isChecked()){
            db.update("08241A0511", 1);
        }
        else{db.update("08241A0511",0);}

        db.close();
        }


           }

0 个答案:

没有答案