android SQLiteDatabase变量问题

时间:2011-05-01 10:46:43

标签: android database

我正在为我的项目使用数据库,我已经为数据库完成了大部分工作。但是,当我声明'SQLiteDatabase db'变量使用方法“getWritableDatabase”时,程序停止运行并发出错误。我尝试了不同的方法解决问题,但任何人都没有工作。

这是我的DBhelper类:

public class DBhelper extends SQLiteOpenHelper {

// Database name
static final String dbAdi = "data.db";
static final Integer version = 1;

static final String userTable = "Personal Info";
static final String userID = "User_ID";
static final String userName = "Name";
static final String userAge = "Age";
static final String userHeight = "Height";
static final String userWeight = "Weight";

public DBhelper(Context context, String name, CursorFactory factory,int version) {
    super(context, dbAdi, null, version);

}

@Override
public void onCreate(SQLiteDatabase db) {

      db.execSQL("CREATE TABLE "+ userTable +" (" + userID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
              + userName +" TEXT NOT NULL, " + userAge +" INTEGER NOT NULL," 
              + userHeight + "INTEGER NOT NULL, " + userWeight +" REAL NOT NULL," );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + kullaniciTablo );
    db.execSQL("DROP TABLE IF EXISTS" + kayitTablo );
    onCreate(db);

}

}

这是我的DBquery类:

public class DBquery extends PersonalInfoRecord {

   private DBhelper helper;

   private SQLiteDatabase db;


    public void save(PersonalInfo person)
    { 

        helper = new DBhelper(null, DBhelper.dbAdi, null, DBhelper.versiyon);
                db = helper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(DBhelper.userName, person.getName());
        values.put(DBhelper.userHeight, person.getHeight());
        values.put(DBhelper.userWeight, person.getWeight());
        values.put(DBhelper.userAge, person.getAge());

        try
        {
           db.insert(DBhelper.userTable, null, values);
           db.close();
           return 1;
        }
        catch(Exception e)
        {
           return 0;
        }

    } 

我的活动课程:

public class PersonalInforRecord extends Activity implements OnClickListener{

    public static final int USER_OPTIONS_SESSION = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kisisel);  // layout name


        Button record = (Button) findViewById(R.id.btnrcd);   
        final EditText nameEdit = (EditText) findViewById(R.id.editname);
        final EditText ageEdit = (EditText) findViewById(R.id.editage);
        final EditText weightEdit = (EditText) findViewById(R.id.editwgt);
        final EditText heightEdit = (EditText) findViewById(R.id.edithgt);


        record.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                DBquery dbq = new DBquery();

                PersonalInfo person = new PersonalInfo();
                person.setName(nameEdit.getText().toString());
                person.setAge(Integer.parseInt(ageEdit.getText().toString()));
                person.setWeight(Double.parseDouble(weightEdit.getText().toString()));
                person.setHeight(Integer.parseInt(heightEdit.getText().toString()));


                dbq.save(person);


            }
        });

    }

1 个答案:

答案 0 :(得分:0)

看起来你传递了一个空上下文。您需要将上下文传递给DBQuery类。另请注意DBHelper.version上的拼写

实例化帮助程序时,请更改以下代码:

helper = new DBhelper(null, DBhelper.dbAdi, null, DBhelper.versiyon);
        db = helper.getWritableDatabase();

到此:

helper = new DBhelper(context, DBhelper.dbAdi, null, DBhelper.version);
        db = helper.getWritableDatabase();