2011-05-01 44 views
1

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

这是我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); 


      } 
     }); 

    } 
+0

你可以像DBhelper的 单一实例//数据库对象 public static DBhelper oh = null; public static ResenaOpenHelper sharedInstance(Context context,String name,CursorFactory factory,int version)if(oh == null){0} } 返回哦; } public DBhelper(上下文上下文,字符串名称,CursorFactory工厂,int版本){超级上下文,dbAdi,null,version); } – 2017-11-28 06:52:12

回答

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(); 
+0

我试过,但它仍然无效:S – andveand 2011-05-01 12:11:35