我对 Android 和 java 完全陌生。以前在 vb.net 工作过。我正在尝试创建 vb.net 应用程序的 Android 版本。Android中的数据库功能让我很生气。这是我需要做的事情的简短描述。- 创建一个“项目”数据库,其中包含项目的单独记录。我需要从这些记录中添加、更新、删除和检索数据。- 创建一个“环境”数据库,其中包含环境变量(温度、海拔等,以及“项目”数据库中当前正在使用的项目。我需要更新此记录。
我看过几个教程,并试图模仿它们。在我的代码中一切似乎都是正确的,但我在光标函数上遇到了错误(也有其他错误,但这似乎是它的根源)。
为了让我开始解决这个问题,让我们从环境数据库开始。我创建了一个 SQLHelper、数据库管理器和一个与之交互的 Activity。三个人都在这里合影。
我想有一个更具体的问题,但是在尝试将其与其他帖子等进行比较 3 天之后,我完全迷失了。感谢任何可以帮助我的人!
HELPER
package com.becrs.becrsmobile;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class EnvironmentSQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_Environment = "envVar";
public static final String ENV_COLUMN_ID = "_id";
public static final String COLUMN_elevation = "elevation";
public static final String COLUMN_temperature = "temperature";
public static final String COLUMN_ScopeIncrement = "ScopeIncrement";
public static final String COLUMN_R1_Var = "R1_Var";
public static final String COLUMN_R2_Var = "R2_Var";
public static final String COLUMN_R3_Var = "R3_Var";
public static final String COLUMN_R4_Var = "R4_Var";
public static final String COLUMN_R5_Var = "R5_Var";
public static final String COLUMN_R6_Var = "R6_Var";
public static final String COLUMN_R7_Var = "R7_Var";
public static final String COLUMN_R8_Var = "R8_Var";
public static final String COLUMN_R9_Var = "R9_Var";
public static final String COLUMN_MaxRange_Var = "MaxRange_Var";
public static final String COLUMN_currentCartridge = "currentCartridge";
private static final String DATABASE_NAME = "environmentdb.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_Environment + " (" + ENV_COLUMN_ID + " integer primary key autoincrement not null,"
+ COLUMN_elevation + " INTEGER, "
+ COLUMN_ScopeIncrement + " REAL, "
+ COLUMN_R1_Var + " INTEGER, "
+ COLUMN_R2_Var + " INTEGER, "
+ COLUMN_R3_Var + " INTEGER, "
+ COLUMN_R4_Var + " INTEGER, "
+ COLUMN_R5_Var + " INTEGER, "
+ COLUMN_R6_Var + " INTEGER, "
+ COLUMN_R7_Var + " INTEGER, "
+ COLUMN_R8_Var + " INTEGER, "
+ COLUMN_R9_Var + " INTEGER, "
+ COLUMN_MaxRange_Var + " INTEGER, "
+ COLUMN_currentCartridge + " INTEGER"
+ ");";
public EnvironmentSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(EnvironmentSQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Environment);
onCreate(db);
}
}
DATABASE MANAGER
package com.becrs.becrsmobile;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class EnvironmentDataSource {
// Database fields
private SQLiteDatabase database;
private EnvironmentSQLiteHelper dbHelper;
private String[] allColumns = {
EnvironmentSQLiteHelper.ENV_COLUMN_ID,
EnvironmentSQLiteHelper.COLUMN_elevation,
EnvironmentSQLiteHelper.COLUMN_temperature,
EnvironmentSQLiteHelper.COLUMN_ScopeIncrement,
EnvironmentSQLiteHelper.COLUMN_R1_Var,
EnvironmentSQLiteHelper.COLUMN_R2_Var,
EnvironmentSQLiteHelper.COLUMN_R3_Var,
EnvironmentSQLiteHelper.COLUMN_R4_Var,
EnvironmentSQLiteHelper.COLUMN_R5_Var,
EnvironmentSQLiteHelper.COLUMN_R6_Var,
EnvironmentSQLiteHelper.COLUMN_R7_Var,
EnvironmentSQLiteHelper.COLUMN_R8_Var,
EnvironmentSQLiteHelper.COLUMN_R9_Var,
EnvironmentSQLiteHelper.COLUMN_MaxRange_Var,
EnvironmentSQLiteHelper.COLUMN_currentCartridge,
};
public EnvironmentDataSource(Context context) {
dbHelper = new EnvironmentSQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Environment updateEnvironment(int elevation, int temperature, Double ScopeIncrement, int R1, int R2, int R3, int R4, int R5, int R6, int R7, int R8, int R9, int MaxRng, int currentCartridge) {
ContentValues values = new ContentValues();
values.put(EnvironmentSQLiteHelper.COLUMN_elevation, elevation);
values.put(EnvironmentSQLiteHelper.COLUMN_temperature, temperature);
values.put(EnvironmentSQLiteHelper.COLUMN_ScopeIncrement, ScopeIncrement);
values.put(EnvironmentSQLiteHelper.COLUMN_R1_Var, R1);
values.put(EnvironmentSQLiteHelper.COLUMN_R2_Var, R2);
values.put(EnvironmentSQLiteHelper.COLUMN_R3_Var, R3);
values.put(EnvironmentSQLiteHelper.COLUMN_R4_Var, R4);
values.put(EnvironmentSQLiteHelper.COLUMN_R5_Var, R5);
values.put(EnvironmentSQLiteHelper.COLUMN_R6_Var, R6);
values.put(EnvironmentSQLiteHelper.COLUMN_R7_Var, R7);
values.put(EnvironmentSQLiteHelper.COLUMN_R8_Var, R8);
values.put(EnvironmentSQLiteHelper.COLUMN_R9_Var, R9);
values.put(EnvironmentSQLiteHelper.COLUMN_MaxRange_Var, MaxRng);
values.put(EnvironmentSQLiteHelper.COLUMN_currentCartridge, currentCartridge);
long insertID;
try {
database.update(EnvironmentSQLiteHelper.TABLE_Environment, values, "_id = 0 ", allColumns);
insertID = 0;
}
catch (Exception e){
insertID = database.insert(EnvironmentSQLiteHelper.TABLE_Environment, null, values);
}
Cursor cursor = database.query(EnvironmentSQLiteHelper.TABLE_Environment,
allColumns, EnvironmentSQLiteHelper.ENV_COLUMN_ID + " = " + insertID, null,
null, null, null, null);
cursor.moveToFirst();
Environment newEnvironment = cursorToEnvironment(cursor);
cursor.close();
return newEnvironment;
}
public Environment getEnvironment() {
Cursor cursor = null;
try {
cursor = database.query(EnvironmentSQLiteHelper.TABLE_Environment,
allColumns, EnvironmentSQLiteHelper.ENV_COLUMN_ID + " = 0", allColumns, null, null, null, null);
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
Environment environment = null;
if (cursor.moveToFirst()){
environment = cursorToEnvironment(cursor);
}
cursor.close();
return environment;
}
private Environment cursorToEnvironment(Cursor cursor) {
Environment environment = new Environment();
environment.setelevation(cursor.getInt(0));
environment.settemperature(cursor.getInt(1));
environment.setlongRangeScopeIncrement(cursor.getDouble(2));
environment.setR1_Var(cursor.getInt(3));
environment.setR2_Var(cursor.getInt(4));
environment.setR3_Var(cursor.getInt(5));
environment.setR4_Var(cursor.getInt(6));
environment.setR5_Var(cursor.getInt(7));
environment.setR6_Var(cursor.getInt(8));
environment.setR7_Var(cursor.getInt(9));
environment.setR8_Var(cursor.getInt(10));
environment.setR9_Var(cursor.getInt(11));
environment.setMaxRange_Var(cursor.getInt(12));
environment.setcurrentCartridge(cursor.getString(13));
return environment;
}
}
INTERFACE ACTIVITY
package com.becrs.becrsmobile;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EnvironmentEditActivity extends Activity
{
private EnvironmentDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editenvironment);
datasource = new EnvironmentDataSource(this);
datasource.open();
Button save = (Button)findViewById(R.id.ButtonSaveEnvironment);
save.setOnClickListener(startsaveListener);
Environment environment = datasource.getEnvironment();
String TempVal;
TextView mTextView;
TempVal = Integer.toString(environment.getelevation());
mTextView = (TextView) findViewById(R.id.input_Elevation);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.gettemperature());
mTextView = (TextView) findViewById(R.id.input_Temperature);
mTextView.setText(TempVal);
TempVal = Double.toString(environment.getlongRangeScopeIncrement());
mTextView = (TextView) findViewById(R.id.input_ClickIncrement);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR1_Var());
mTextView = (TextView) findViewById(R.id.input_R1);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR2_Var());
mTextView = (TextView) findViewById(R.id.input_R2);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR3_Var());
mTextView = (TextView) findViewById(R.id.input_R3);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR4_Var());
mTextView = (TextView) findViewById(R.id.input_R4);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR5_Var());
mTextView = (TextView) findViewById(R.id.input_R5);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR6_Var());
mTextView = (TextView) findViewById(R.id.input_R6);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR7_Var());
mTextView = (TextView) findViewById(R.id.input_R7);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR8_Var());
mTextView = (TextView) findViewById(R.id.input_R8);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getR9_Var());
mTextView = (TextView) findViewById(R.id.input_R9);
mTextView.setText(TempVal);
TempVal = Integer.toString(environment.getMaxRange_Var());
mTextView = (TextView) findViewById(R.id.input_MaxRng);
mTextView.setText(TempVal);
}
private OnClickListener startsaveListener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText elevation = (EditText)findViewById(R.id.input_Elevation);
int elevationVal = Integer.parseInt(elevation.getText().toString());
EditText temperature = (EditText)findViewById(R.id.input_Temperature);
int temperatureVal = Integer.parseInt(temperature.getText().toString());
EditText clickIncrement = (EditText)findViewById(R.id.input_ClickIncrement);
Double clickIncrementVal = Double.parseDouble(clickIncrement.getText().toString());
EditText R1 = (EditText)findViewById(R.id.input_R1);
int R1Val = Integer.parseInt(R1.getText().toString());
EditText R2 = (EditText)findViewById(R.id.input_R2);
int R2Val = Integer.parseInt(R2.getText().toString());
EditText R3 = (EditText)findViewById(R.id.input_R3);
int R3Val = Integer.parseInt(R3.getText().toString());
EditText R4 = (EditText)findViewById(R.id.input_R4);
int R4Val = Integer.parseInt(R4.getText().toString());
EditText R5 = (EditText)findViewById(R.id.input_R5);
int R5Val = Integer.parseInt(R5.getText().toString());
EditText R6 = (EditText)findViewById(R.id.input_R6);
int R6Val = Integer.parseInt(R6.getText().toString());
EditText R7 = (EditText)findViewById(R.id.input_R7);
int R7Val = Integer.parseInt(R7.getText().toString());
EditText R8 = (EditText)findViewById(R.id.input_R8);
int R8Val = Integer.parseInt(R8.getText().toString());
EditText R9 = (EditText)findViewById(R.id.input_R9);
int R9Val = Integer.parseInt(R9.getText().toString());
EditText MaxRng = (EditText)findViewById(R.id.input_MaxRng);
int MaxRngVal = Integer.parseInt(MaxRng.getText().toString());
int RecordNum = 0;
datasource.updateEnvironment(elevationVal, temperatureVal, clickIncrementVal, R1Val, R2Val, R3Val, R4Val, R5Val, R6Val, R7Val, R8Val, R9Val, MaxRngVal, RecordNum);
setResult(RESULT_OK);
finish();
}
};
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
环境对象
package com.becrs.becrsmobile;
public class Environment {
private static int elevation;
private static int temperature;
private static Double longRangeScopeIncrement;
private static int R0_Var;
private static int R1_Var;
private static int R2_Var;
private static int R3_Var;
private static int R4_Var;
private static int R5_Var;
private static int R6_Var;
private static int R7_Var;
private static int R8_Var;
private static int R9_Var;
private static int MaxRange_Var;
private static int currentCartridge;
public static int getelevation() {
return elevation;
}
public void setelevation(int elevation) {
Environment.elevation = elevation;
}
public static int gettemperature() {
return temperature;
}
public void settemperature(int temperature) {
Environment.temperature = temperature;
}
public static double getlongRangeScopeIncrement() {
return longRangeScopeIncrement;
}
public void setlongRangeScopeIncrement(Double longRangeScopeIncrement) {
Environment.longRangeScopeIncrement = longRangeScopeIncrement;
}
public static int getR0_Var() {
return R0_Var;
}
public void setR0_Var(int R0_Var) {
Environment.R0_Var = R0_Var;
}
public static int getR1_Var() {
return R1_Var;
}
public void setR1_Var(int R1_Var) {
Environment.R1_Var =R1_Var;
}
public static int getR2_Var() {
return R2_Var;
}
public void setR2_Var(int R2_Var) {
Environment.R2_Var = R2_Var;
}
public static int getR3_Var() {
return R3_Var;
}
public void setR3_Var(int R3_Var) {
Environment.R3_Var = R3_Var;
}
public static int getR4_Var() {
return R4_Var;
}
public void setR4_Var(int R4_Var) {
Environment.R4_Var = R4_Var;
}
public static int getR5_Var() {
return R5_Var;
}
public void setR5_Var(int R5_Var) {
Environment.R5_Var = R5_Var;
}
public static int getR6_Var() {
return R6_Var;
}
public void setR6_Var(int R6_Var) {
Environment.R6_Var = R6_Var;
}
public static int getR7_Var() {
return R7_Var;
}
public void setR7_Var(int R7_Var) {
Environment.R7_Var = R7_Var;
}
public static int getR8_Var() {
return R8_Var;
}
public void setR8_Var(int R8_Var) {
Environment.R8_Var = R8_Var;
}
public static int getR9_Var() {
return R9_Var;
}
public void setR9_Var(int R9_Var) {
Environment.R9_Var = R9_Var;
}
public static int getMaxRange_Var() {
return MaxRange_Var;
}
public void setMaxRange_Var(int MaxRange_Var) {
Environment.MaxRange_Var = MaxRange_Var;
}
public static int getcurrentCartridge() {
return currentCartridge;
}
public void setcurrentCartridge(int currentCartridge) {
Environment.currentCartridge = currentCartridge;
}
}