android DBhelper 简单问题

double_文 2014-10-10 08:11:19
这是我的checkInDocumentSettingDB 类的构造函数:
public checkInDocumentSettingDB(Context context) {
rentingdb = new RentingDB(context);
}
这是我的checkInDocumentSettingDB 类的构造函数:
public checkOutDocumentSettingDB(Context context) {
rentingdb = new RentingDB(context);
}


这是我的RentingDB:

public class RentingDB extends SQLiteOpenHelper {

public RentingDB(Context context) {
super(context, "WarrenJones.db", null, 1);
// TODO Auto-generated constructor stub
}

@Override
/**
* 各种表的建立
*/
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("PRAGMA foreign_keys=ON");
String sqlhouse = "create table house (houseName char(20) primary key, number int not null);";
db.execSQL(sqlhouse);
String sqlroom = "create table room(houseName char(20),roomNumber integer ,waterConsumption int not null"
+ ",electricityConsumption int not null,primary key(houseName,roomNumber),"
+ "foreign key (houseName) references house (houseName) on delete cascade on update cascade);";
db.execSQL(sqlroom);
String sqlrenter = "create table renter(Id char(18) primary key,name char(20) not null,phoneNumber char(11) not null)";
db.execSQL(sqlrenter);
String sqlcheckindocument = "create table CheckInDocument(Id char(18),houseName char(20),roomNumber int,deposit int not null,rental int not null,startingDate Date not null,surfing int not null,waterFare float not null,electricityFare float not null,totalAmount float,primary key (houseName,roomNumber),foreign key (Id) references renter(Id) on delete cascade on update cascade,foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlcheckindocument);
String sqlcheckoutdocument = "create table checkOutDocument(houseName char(20),roomNumber char(10),checkOutDate Date not null,amount int,waterConsumption int not null,electricityConsumption int not null,"
+ "primary key (houseName,roomNumber),foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlcheckoutdocument);
String sqlrentingdocument = "create table rentingDocument(houseName char(20),roomNumber char(10),waterConsumption int not null,electricityConsumption int not null,rentingDate Date not null,amount int not null,"
+ "primary key (houseName,roomNumber),foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)";
db.execSQL(sqlrentingdocument);
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}


现在checkoutSettingDB要调用checkinSettingDB的函数:
但是如果这样
checkInSettingDB rsdb = new checkInSettingDB(null);
会导致空指针。。。。
怎么办
...全文
81 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
十三邵 2014-10-11
  • 打赏
  • 举报
回复
引用 楼主 u010594660 的回复:
这是我的checkInDocumentSettingDB 类的构造函数: public checkInDocumentSettingDB(Context context) { rentingdb = new RentingDB(context); } 这是我的checkInDocumentSettingDB 类的构造函数: public checkOutDocumentSettingDB(Context context) { rentingdb = new RentingDB(context); } 这是我的RentingDB: public class RentingDB extends SQLiteOpenHelper { public RentingDB(Context context) { super(context, "WarrenJones.db", null, 1); // TODO Auto-generated constructor stub } @Override /** * 各种表的建立 */ public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("PRAGMA foreign_keys=ON"); String sqlhouse = "create table house (houseName char(20) primary key, number int not null);"; db.execSQL(sqlhouse); String sqlroom = "create table room(houseName char(20),roomNumber integer ,waterConsumption int not null" + ",electricityConsumption int not null,primary key(houseName,roomNumber)," + "foreign key (houseName) references house (houseName) on delete cascade on update cascade);"; db.execSQL(sqlroom); String sqlrenter = "create table renter(Id char(18) primary key,name char(20) not null,phoneNumber char(11) not null)"; db.execSQL(sqlrenter); String sqlcheckindocument = "create table CheckInDocument(Id char(18),houseName char(20),roomNumber int,deposit int not null,rental int not null,startingDate Date not null,surfing int not null,waterFare float not null,electricityFare float not null,totalAmount float,primary key (houseName,roomNumber),foreign key (Id) references renter(Id) on delete cascade on update cascade,foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)"; db.execSQL(sqlcheckindocument); String sqlcheckoutdocument = "create table checkOutDocument(houseName char(20),roomNumber char(10),checkOutDate Date not null,amount int,waterConsumption int not null,electricityConsumption int not null," + "primary key (houseName,roomNumber),foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)"; db.execSQL(sqlcheckoutdocument); String sqlrentingdocument = "create table rentingDocument(houseName char(20),roomNumber char(10),waterConsumption int not null,electricityConsumption int not null,rentingDate Date not null,amount int not null," + "primary key (houseName,roomNumber),foreign key(houseName,roomNumber) references room(houseName,roomNumber) on delete cascade on update cascade)"; db.execSQL(sqlrentingdocument); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } } 现在checkoutSettingDB要调用checkinSettingDB的函数: 但是如果这样 checkInSettingDB rsdb = new checkInSettingDB(null); 会导致空指针。。。。 怎么办
我也不知道,帮你顶一下吧。。。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧