gpt4 book ai didi

java - SQLite 数据库 : Android studio custom auto incrementation

转载 作者:行者123 更新时间:2023-11-30 10:36:50 24 4
gpt4 key购买 nike

我是 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";
}
}

患者信息提供者.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;
}
}

以下文件用于在带有自定义 TextView 的 ListView 中显示值。

患者适配器.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;
}
}

最佳答案

AUTOINCREMENT 的目的是防止重复使用以前删除的行的 ID。

患者信息.javaID 列必须是表格的第一列:

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);

患者信息提供者.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”参数带有红色下划线,表示变量可能尚未初始化。

我已经解决了所有问题:)

关于java - SQLite 数据库 : Android studio custom auto incrementation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40434600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com