在我的项目中有3个注册表格。我提供了3个单独的数据库文件,并相应地在各自的注册文件中调用我在所有数据库文件中都给出了相同的数据库名称,并且表格名称因表格而异。
面临的问题是数据库允许我仅存储单个表单注册详细信息(即:如果有User和Doctor注册,并且我有相同的数据库名称但表名称不同)。申请允许存储用户或医生注册。
用户注册:
if (username.trim().equals("")&& userage.trim().equals("") && usergender.trim().equals("") && userdob.trim().equals("") && useraddress.trim().equals("") && userpincode.trim().equals("") && userid.trim().equals("") && userpass.trim().equals(""))
{
Toast.makeText(getBaseContext(),"Fields should not empty",Toast.LENGTH_LONG).show();
/* UserName.setText("");
UserAge.setText("");
UserGender.setText("");
Userdob.setText("");
Useraddress.setText("");
Userpincode.setText("");
Userid.setText("");
UserPass.setText(""); */
}
else
{
try {
DatabaseHandler3 db=new DatabaseHandler3(userregistration.this);
db.addContact(new Contact(username,userage,usergender,userdob,useraddress,userpincode,userid,userpass));
Toast.makeText(getBaseContext(),"Registration Success",Toast.LENGTH_LONG).show();
List<Contact> contacts=db.getAllContacts();
for(Contact cn:contacts){
String log=cn.getNAME()+"\n"+cn.getAGE()+"\n"+cn.getGENDER()+"\n"+cn.getDOB()+"\n"+cn.getADDRESS()+"\n"+cn.getPINCODE();
Toast.makeText(userregistration.this,log,Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
// msq.putinformation(msq,username,userage,usergender,userdob,useraddress,userpincode,userid,userpass);
finish();
}
}
}) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_userregistration, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Database Handler3 - Userdb:
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DOCROID";
// Contacts table name
private static final String TABLE_CONTACTS = "UserRegistration";
// Contacts Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_AGE = "age";
private static final String KEY_GENDER = "gender";
private static final String KEY_DOB = "dob";
private static final String KEY_ADDRESS = "address";
private static final String KEY_PINCODE = "pincode";
private static final String KEY_PH_NO = "phone";
private static final String KEY_PASSWORD = "password";
private static final String LOGCAT = null;
private static Context context;
public DatabaseHandler3(Context context) {
super(context,"/sdcard/DOCROID/"+DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_NAME + " TEXT," + KEY_AGE + " TEXT,"
+ KEY_GENDER + " TEXT," + KEY_DOB + " TEXT,"
+ KEY_ADDRESS + " TEXT," + KEY_PINCODE + " TEXT,"
+ KEY_PH_NO + " TEXT," + KEY_PASSWORD + " TEXT" + ");";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getNAME()); // Contact Name
values.put(KEY_AGE, contact.getAGE());
values.put(KEY_GENDER, contact.getGENDER()); // Contact Phone Number
values.put(KEY_DOB, contact.getDOB()); // Contact Name
values.put(KEY_ADDRESS, contact.getADDRESS());
values.put(KEY_PINCODE, contact.getPINCODE());
values.put(KEY_PH_NO, contact.getPHONE()); // Contact Name
values.put(KEY_PASSWORD, contact.getPASSWORD());
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close();
}
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setNAME(cursor.getString(0));
contact.setAGE(cursor.getString(1));
contact.setGENDER(cursor.getString(2));
contact.setDOB(cursor.getString(3));
contact.setADDRESS(cursor.getString(4));
contact.setPINCODE(cursor.getString(5));
contact.setPHONE(cursor.getString(6));
contact.setPASSWORD(cursor.getString(7));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
String selectQuery = "DELETE FROM " + TABLE_CONTACTS;
db.execSQL(selectQuery);
}
医生注册:
if (docname.trim().equals("") && docage.trim().equals("") && docgender.trim().equals("") && docdob.trim().equals("") && docarea.trim().equals("") && docaddress.trim().equals("") && docpincode.trim().equals("") && docid.trim().equals("") && docpass.trim().equals("")) {
Toast.makeText(getBaseContext(), "Fields should not empty", Toast.LENGTH_LONG).show();
/* Doc_name.setText("");
Doc_age.setText("");
Doc_gender.setText("");
Doc_dob.setText("");
Doc_area.setText("");
Doc_address.setText("");
Doc_pincode.setText("");
Doc_id.setText("");
Doc_pass.setText(""); */
} else {
try {
DatabaseHandler4 db1 = new DatabaseHandler4(docregistration.this);
db1.addContact(new Contact1(docname, docage, docgender, docdob, docarea, docaddress, docpincode, docid, docpass));
Toast.makeText(getBaseContext(), "Registration Success", Toast.LENGTH_LONG).show();
List<Contact1> contacts = db1.getAllContacts();
for (Contact1 cn : contacts) {
String log = cn.getDNAME() + "\n" + cn.getDAGE() + "\n" + cn.getDGENDER() + "\n" + cn.getDDOB() + "\n" + cn.getDAREA() + "\n" + cn.getDADDRESS() + "\n" + cn.getDPINCODE();
Toast.makeText(docregistration.this, log, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
finish();
}
DatabaseHandler4 - 医生db:
// Database Name
private static final String DATABASE_NAME = "DOCROID";
// Contacts table name
private static final String TABLE_CONTACTS1 = "docregistration";
// Contacts Table Columns names
private static final String KEY_DNAME = "name";
private static final String KEY_DAGE = "age";
private static final String KEY_DGENDER = "gender";
private static final String KEY_DDOB = "dob";
private static final String KEY_DAREA = "area";
private static final String KEY_DADDRESS = "address";
private static final String KEY_DPINCODE = "pincode";
private static final String KEY_DPH_NO = "phone";
private static final String KEY_DPASSWORD = "password";
private static final String LOGCAT = null;
private static Context context;
public DatabaseHandler4(Context context) {
super(context,"/sdcard/DOCROID/"+DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db1) {
// TODO Auto-generated method stub
String CREATE_CONTACTS_TABLE1 = "CREATE TABLE " + TABLE_CONTACTS1 + "("
+ KEY_DNAME + " TEXT," + KEY_DAGE + " TEXT,"
+ KEY_DGENDER + " TEXT," + KEY_DDOB + " TEXT,"
+ KEY_DAREA + " TEXT," + KEY_DADDRESS + " TEXT,"
+ KEY_DPINCODE + " TEXT,"
+ KEY_DPH_NO + " TEXT," + KEY_DPASSWORD + " TEXT" + ");";
db1.execSQL(CREATE_CONTACTS_TABLE1);
}
@Override
public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addContact(Contact1 contact) {
SQLiteDatabase db1 = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DNAME, contact.getDNAME());
values.put(KEY_DAGE, contact.getDAGE());
values.put(KEY_DGENDER, contact.getDGENDER());
values.put(KEY_DDOB, contact.getDDOB());
values.put(KEY_DAREA, contact.getDAREA());
values.put(KEY_DADDRESS, contact.getDADDRESS());
values.put(KEY_DPINCODE, contact.getDPINCODE());
values.put(KEY_DPH_NO, contact.getDPHONE());
values.put(KEY_DPASSWORD, contact.getDPASSWORD());
// Inserting Row
db1.insert(TABLE_CONTACTS1, null, values);
db1.close();
}
public List<Contact1> getAllContacts() {
List<Contact1> contactList = new ArrayList<Contact1>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS1;
SQLiteDatabase db1 = this.getWritableDatabase();
Cursor cursor = db1.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact1 contact = new Contact1();
contact.setDNAME(cursor.getString(0));
contact.setDAGE(cursor.getString(1));
contact.setDGENDER(cursor.getString(2));
contact.setDDOB(cursor.getString(3));
contact.setDAREA(cursor.getString(4));
contact.setDADDRESS(cursor.getString(5));
contact.setDPINCODE(cursor.getString(6));
contact.setDPHONE(cursor.getString(7));
contact.setDPASSWORD(cursor.getString(8));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
return contactList;
}
答案 0 :(得分:0)
最好使用包含多个表的1个数据库。要做到这一点并不难,也更容易处理。
如果你需要一个示例代码,互联网上有很多东西,但如果你需要它,这里有一个(它有5个表)。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TERMS + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");
db.execSQL("CREATE TABLE " + PERIODS + " (" + colPeriodID + " INTEGER PRIMARY KEY , " + colPeriodClass + " TEXT)");
db.execSQL("CREATE TABLE " + STATUS + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");
db.execSQL("CREATE TABLE " + ACCOUNTS + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
colName + " TEXT, " +
colAmount + " Integer, " +
colPurpose + " TEXT, " +
colTerms + " INTEGER NOT NULL, " +
colPeriod +" INTEGER NOT NULL, " +
colBalance +" INTEGER, "+
colStatus + " INTEGER NOT NULL," +
colDate + " TEXT, " +
colEditDate + " TEXT, " +
"FOREIGN KEY (" + colTerms + ") REFERENCES " + TERMS + " (" + colTermsID + " )" + "," +
"FOREIGN KEY (" + colPeriod + ") REFERENCES " + PERIODS + " (" + colPeriodID + ") " + ","+
"FOREIGN KEY (" + colStatus + ") REFERENCES " + STATUS + " (" + colStatusID + "));");
db.execSQL("CREATE TABLE " + PAYMENTS + " (" + colPayID + " INTEGER PRIMARY KEY , " +
colGroupID + " INTEGER NOT NULL, " +
colPayBal + " TEXT, " +
colInterest + " TEXT, " +
colPayDue + " TEXT, " +
colDateDue + " TEXT, " +
colPaid + " Integer, " +
"FOREIGN KEY (" + colGroupID + ") REFERENCES " + ACCOUNTS + " (" + colID + ") ON DELETE CASCADE);");