gpt4 book ai didi

android - SQLite 数据库列不会使用 Update 方法进行更新

转载 作者:行者123 更新时间:2023-11-29 14:59:48 24 4
gpt4 key购买 nike

我的应用程序使用 SQLite 数据库,我想将来自多个 Activity 的数据保存到一个表中。在第一个 Activity 中,我使用 add 方法在表中创建一行。在下一个 Activity 中,我使用 update 方法更新现有行中的列。
我使用 DB Browser for SQLite 应用程序检查我的数据库,它显示我从第二个 Activity 中保存的数据不在数据库中(空)。不知道是什么问题。
这是我的类(class):
SQLiteHelper :

    public class SQLiteHelper extends SQLiteOpenHelper implements ProjectDAO {

public SQLiteHelper(Context context) {
super(context, "my_db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE tbl_project_info (id INTEGER PRIMARY KEY," +
"name TEXT," +
"company_name TEXT," +
"address TEXT," +
"length1 TEXT," +
"length2 TEXT," +
"length3 TEXT," +
"length4 TEXT," +
"length5 TEXT," +
"diameter1 Text,"+
"diameter2 Text,"+
"diameter3 Text,"+
"diameter4 Text,"+
"diameter5 Text)");
} catch (SQLiteException e) {
Log.e("SQLITE", "onCreate: " + e.toString());
}

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

@Override
public boolean addProject(Project project) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", project.getName());
contentValues.put("company_name", project.getCompany_name());
contentValues.put("address", project.getAddress());
contentValues.put("length1",project.getLength1());
contentValues.put("length2",project.getLength2());
contentValues.put("length3",project.getLength3());
contentValues.put("length4",project.getLength4());
contentValues.put("length5",project.getLength5());
contentValues.put("diameter1",project.getDiameter1());
contentValues.put("diameter2",project.getDiameter2());
contentValues.put("diameter3",project.getDiameter3());
contentValues.put("diameter4",project.getDiameter4());
contentValues.put("diameter5",project.getDiameter5());
long result = db.insert("tbl_project_info", null, contentValues);
db.close();
return result != -1;
}

@Override
public int getProjectsCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tbl_project_info", null);
int count = cursor.getCount();
cursor.close();
db.close();
return count;
}

@Override
public boolean updateProject(Project project) {
SQLiteDatabase db = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("length1",project.getLength1());
contentValues.put("length2",project.getLength2());
contentValues.put("length3",project.getLength3());
contentValues.put("length4",project.getLength4());
contentValues.put("length5",project.getLength5());
contentValues.put("diameter1",project.getDiameter1());
contentValues.put("diameter2",project.getDiameter2());
contentValues.put("diameter3",project.getDiameter3());
contentValues.put("diameter4",project.getDiameter4());
contentValues.put("diameter5",project.getDiameter5());
db.update("tbl_project_info",contentValues,"id = ?", new String[]{String.valueOf(project.getId())});
db.close();
return true;
}

@Override
public List<Project> getAllProjects() {
List<Project> projects = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tbl_project_info", null);
if (cursor.moveToFirst()) {
do {
Project project = new Project();
project.setName(cursor.getString(0));
project.setCompany_name(cursor.getString(1));
project.setAddress(cursor.getString(2));
projects.add(project);
} while (cursor.moveToNext());
}
return projects;
}
}

新项目 Activity :

public class NewProjectActivity extends AppCompatActivity {
private ProjectDAO projectDAO;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_project);
projectDAO = DBInjector.provideProjectDao(this);
setupViews();
}

private void setupViews() {
final EditText projectNameET = findViewById(R.id.et_newProject_projectName);
final EditText companyNameET = findViewById(R.id.et_newProject_companyName);
final EditText addressET = findViewById(R.id.et_newProject_address);
Button saveInfoBTN = findViewById(R.id.btn_newProject_saveInfo);

saveInfoBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (projectNameET.length() > 0) {
if (companyNameET.length() > 0) {
if (addressET.length() > 0) {
Project project = new Project();
project.setName(projectNameET.getText().toString());
project.setCompany_name(companyNameET.getText().toString());
project.setAddress(addressET.getText().toString());
if (projectDAO.addProject(project)){
Toast.makeText(NewProjectActivity.this, "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(NewProjectActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
}
}
}

Intent intent = new Intent(NewProjectActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}

主要 Activity :

public class MainActivity extends AppCompatActivity {
private ProjectDAO projectDAO;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
projectDAO = DBInjector.provideProjectDao(this);

final EditText lengthET1 = findViewById(R.id.et_main_length1);
final EditText lengthET2 = findViewById(R.id.et_main_length2);
final EditText lengthET3 = findViewById(R.id.et_main_length3);
final EditText lengthET4 = findViewById(R.id.et_main_length4);
final EditText lengthET5 = findViewById(R.id.et_main_length5);
final EditText diameterET1 = findViewById(R.id.et_main_diameter1);
final EditText diameterET2 = findViewById(R.id.et_main_diameter2);
final EditText diameterET3 = findViewById(R.id.et_main_diameter3);
final EditText diameterET4 = findViewById(R.id.et_main_diameter4);
final EditText diameterET5 = findViewById(R.id.et_main_diameter5);

Button calculateButton = findViewById(R.id.btn_main_calculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

float Le1 = 0;
if (lengthET1.length() > 0) {
String L1 = lengthET1.getText().toString();
Le1 = Float.parseFloat(L1);
}
float Di1 = 0;
if (diameterET1.length() > 0) {
String D1 = diameterET1.getText().toString();
Di1 = Float.parseFloat(D1);
}
float Le2 = 0;
if (lengthET2.length() > 0) {
String L2 = lengthET2.getText().toString();
Le2 = Float.parseFloat(L2);
}
float Di2 = 0;
if (diameterET2.length() > 0) {
String D2 = diameterET2.getText().toString();
Di2 = Float.parseFloat(D2);
}
float Le3 = 0;
if (lengthET3.length() > 0) {
String L3 = lengthET3.getText().toString();
Le3 = Float.parseFloat(L3);
}
float Di3 = 0;
if (diameterET3.length() > 0) {
String D3 = diameterET3.getText().toString();
Di3 = Float.parseFloat(D3);
}
float Le4 = 0;
if (lengthET4.length() > 0) {
String L4 = lengthET4.getText().toString();
Le4 = Float.parseFloat(L4);
}
float Di4 = 0;
if (diameterET4.length() > 0) {
String D4 = diameterET4.getText().toString();
Di4 = Float.parseFloat(D4);
}
float Le5 = 0;
if (lengthET5.length() > 0) {
String L5 = lengthET5.getText().toString();
Le5 = Float.parseFloat(L5);
}
float Di5 = 0;
if (diameterET5.length() > 0) {
String D5 = diameterET5.getText().toString();
Di5 = Float.parseFloat(D5);
}

final float Surface1 = (float) (Le1 * Di1 * Math.PI);
final float Surface2 = (float) (Le2 * Di2 * Math.PI);
final float Surface3 = (float) (Le3 * Di3 * Math.PI);
final float Surface4 = (float) (Le4 * Di4 * Math.PI);
final float Surface5 = (float) (Le5 * Di5 * Math.PI);

final float Surface = Surface1 + Surface2 + Surface3 + Surface4 + Surface5;

Intent intent = new Intent(MainActivity.this, IntensityActivity.class);
if (Surface == 0) {
Toast.makeText(MainActivity.this, "No numbers are entered", Toast.LENGTH_SHORT).show();
} else {
intent.putExtra("Result", Surface);
startActivity(intent);
}
PersonalInfoSharedPrefManager manager = new PersonalInfoSharedPrefManager(MainActivity.this);
manager.setSuface(Surface);

Project project = new Project();

project.setLength1(lengthET1.getText().toString());
project.setDiameter1(diameterET1.getText().toString());

project.setLength2(lengthET2.getText().toString());
project.setDiameter2(diameterET2.getText().toString());

project.setLength3(lengthET3.getText().toString());
project.setDiameter3(diameterET3.getText().toString());

project.setLength4(lengthET4.getText().toString());
project.setDiameter4(diameterET4.getText().toString());

project.setLength5(lengthET5.getText().toString());
project.setDiameter5(diameterET5.getText().toString());

projectDAO.updateProject(project);
}
});
}
}

最佳答案

MainActivity 中,您应该设置您的项目 ID 以进行更新:

 Project project = new Project();
project.setId(yourIdValue);

关于android - SQLite 数据库列不会使用 Update 方法进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49596441/

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