我是Android studio和SQLiteDatabases的新手。我需要以下方面的帮助:
数据库,成功创建并可以显示值。但是,我正在尝试为每位患者创建自定义唯一ID。 ID应介于000000-999999(6位唯一ID)之间。
应用程序的工作方式是:
1. User can "add patient"
2. User enters firstName and lastName
3. User than selects a department from a spinner
4. once the user has completed the above steps, he/she can click "Add Patient"
5. Then the values are displayed in a TabbedView, however the textView is horizontal rather than vertical.
6. and its displayed in a ListView
我已经能够实现上述所有步骤。我现在需要帮助为患者创建一个独特的id。非常感谢任何帮助
请在下面查看代码:
PatientDbHelper.java // Creates the tables and database
package tanav.sharma;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Tanav on 11/4/2016.
*/
public class PatientDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "PATIENTINFO.db";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE "+PatientInfo.NewPatientInfo.TABLE_NAME+" ("+ PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"+PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"+PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";
public PatientDbHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created / opened ...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created...");
}
public void addInformations(String fname, String lname, String department, SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);
db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);
Log.e("DATABASE OPERATIONS","One in row inserted...");
}
public Cursor getInformations(SQLiteDatabase db){
Cursor cursor;
String[] projections = {PatientInfo.NewPatientInfo.PATIENT_FNAME, PatientInfo.NewPatientInfo.PATIENT_LNAME, PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT};
cursor = db.query(PatientInfo.NewPatientInfo.TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
PatientInfo.java //这是列等的名称。
package tanav.sharma;
/**
* Created by Tanav on 11/4/2016.
*/
public class PatientInfo {
public static abstract class NewPatientInfo{
//public static final String PATIENT_ID = "patient_id";
public static final String PATIENT_FNAME = "patient_fname";
public static final String PATIENT_LNAME = "patient_lname";
public static final String PATIENT_DEPARTMENT = "patient_department";
public static final String TABLE_NAME = "patient_info";
}
}
PatientInfoProvider.java
package tanav.sharma;
/**
* Created by Tanav on 11/4/2016.
*/
public class PatientInfoProvider {
private String fname;
private String lname;
private String departments;
public PatientInfoProvider(String fname, String lname, String departments){
this.fname = fname;
this.lname = lname;
this.departments = departments;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDepartments() {
return departments;
}
public void setDepartments(String departments) {
this.departments = departments;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}
以下文件是使用customText视图在listView中显示值。
PatientAdapter.java
package tanav.sharma;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Tanav on 11/4/2016.
*/
public class PatientAdapter extends ArrayAdapter {
List list = new ArrayList();
public PatientAdapter(Context context, int resource) {
super(context, resource);
}
static class LayoutHandler{
TextView FNAME,LNAME,Department;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.display_all_patients,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.FNAME = (TextView)row.findViewById(R.id.patientNameFirst);
layoutHandler.LNAME = (TextView)row.findViewById(R.id.patientNameLast);
layoutHandler.Department = (TextView) row.findViewById(R.id.patientDepartment);
row.setTag(layoutHandler);
}else{
layoutHandler = (LayoutHandler)row.getTag();
}
PatientInfoProvider patientInfoProvider = (PatientInfoProvider)this.getItem(position);
layoutHandler.FNAME.setText(patientInfoProvider.getFname());
layoutHandler.LNAME.setText(patientInfoProvider.getLname());
layoutHandler.Department.setText(patientInfoProvider.getDepartments());
return row;
}
}
答案 0 :(得分:1)
AUTOINCREMENT的目的是防止从以前删除的行重用ID。
<强> PatientInfo.java 强> ID列必须是表的第一列:
public static final String PATIENT_ID = "_id";
<强> PatientDbHelper.java 强>
private static final String CREATE_QUERY = "CREATE TABLE " + PatientInfo.NewPatientInfo.TABLE_NAME + " ("
+ PatientInfo.NewPatientInfo.PATIENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PatientInfo.NewPatientInfo.PATIENT_FNAME+ " TEXT,"
+ PatientInfo.NewPatientInfo.PATIENT_LNAME+" TEXT,"
+ PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT+" TEXT);";
在addInformations()
中if ( id != 0 ) contentValues.put(PatientInfo.NewPatientInfo.PATIENT_ID,id);
PatientInfoProvider.java
private int id;
和
public void setId(int id) {
this.id= id;
}
或/并更改构造函数
public PatientInfoProvider(int id, String fname, String lname, String departments){
this.id = id;
this.fname = fname;
this.lname = lname;
this.departments = departments; }
你必须在某个地方填写ID字段。如果您需要,我可以放置整个PatientInfoProvider.java
好的,那我的addInformation()会是这样的吗?
public void addInformations(int id, String fname, String lname, String department, SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
if ( id != 0 ) contentValues.put(PatientInfo.NewPatientInfo.PATIENT_ID, id);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_FNAME,fname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_LNAME,lname);
contentValues.put(PatientInfo.NewPatientInfo.PATIENT_DEPARTMENT,department);
db.insert(PatientInfo.NewPatientInfo.TABLE_NAME, null,contentValues);
Log.e("DATABASE OPERATIONS","One in row inserted...");
}
它还给我以下错误:
Error:(50, 28) error: method addInformations in class PatientDbHelper cannot be applied to given types;
required: int,String,String,String,SQLiteDatabase
found: String,String,String,SQLiteDatabase
reason: actual and formal argument lists differ in length
所以我试着通过在我的代码中应用以下行来修复它。
public void addPatient(View view){
int id; // Added this line
String fname = PatientFirstName.getText().toString();
String lname = PatientLastName.getText().toString();
String dept = departments.getSelectedItem().toString();
patientDbHelper = new PatientDbHelper(this);
sqLiteDatabase = patientDbHelper.getWritableDatabase();
patientDbHelper.addInformations(id,fname,lname,dept,sqLiteDatabase); //added 'id' in arguments..
Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
patientDbHelper.close();
}
但是,addInformations中的'id'参数带有加下划线的红色,表示Variable可能尚未初始化。
我修复了一切:)
答案 1 :(得分:0)
1-在java中用Random()类生成一个随机数
2-如果存在此编号,则在数据库中搜索如果不存在则生成新编号使用此编号插入数据。
注意:字段 ID 是主要字段,但不是自动增量。