我正在编写一个简单的数据库应用程序,您可以在其中输入您的姓名并使用 2 编辑文本进行标记,然后按下按钮保存数据库并移至显示数据库结果的第二个活动。数据库包含 4 列:id、名称、标记、操作,其中“操作”列从操作中获取结果,我已经将其输入放入程序中。
我在插入时尝试并捕获,它会显示一个对话框,通知我它是否成功。结果是成功的,但是当移动到查看活动时应用程序强制关闭。这是我的数据库类代码
public class DateBase {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "user_name";
public static final String KEY_MARK = "user_mark";
public static final String KEY_OPERATION = "operation";
private static final String DATABASE_NAME = "Statistic";
private static final String DATABASE_TABLE = "user_interface";
private static final int DATABASE_VERSION = 1;
private DBInterface MyInterface;
private final Context MyContext;
private SQLiteDatabase MyDB;
private static class DBInterface extends SQLiteOpenHelper
{
public DBInterface(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_MARK + " TEXT NOT NULL, " +
KEY_OPERATION + " TEXT NOT NULL );"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE );
onCreate(db);
}
}
public DateBase(Context c)
{
MyContext = c;
}
public DateBase open() throws SQLException
{
MyInterface = new DBInterface(MyContext);
MyDB = MyInterface.getWritableDatabase();
return this;
}
public void close()
{
MyInterface.close();
}
public long InsertEntry(String name, String mark, String ops) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_MARK, mark);
cv.put(KEY_OPERATION, ops);
return MyDB.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_MARK, KEY_OPERATION};
Cursor c = MyDB.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iMark = c.getColumnIndex(KEY_MARK);
int iOperation = c.getColumnIndex(KEY_OPERATION);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iMark) + " " + c.getString(iOperation) + "\n";
}
return result;
}
}
这是单击按钮时的插入代码:
public void onClick(View v) {
// TODO Auto-generated method stub
boolean diditwork = true;
try {String name = ed1.getText().toString();
String mark = ed2.getText().toString();
String ops = "";
for (int i = 0; i < ops.length(); i++)
{
switch (op[i]) // int[] op = new int[]{1,3};
{
case 1:
ops = ops + "+ ";
break;
case 2:
ops = ops + "- ";
break;
case 3:
ops = ops + "* ";
break;
case 4:
ops = ops + "/ ";
break;
}
}
DateBase entry = new DateBase(MainActivity.this);
entry.open();
entry.InsertEntry(name, mark, ops);
entry.close();
} catch (Exception e)
{
diditwork = false;
String error = e.toString();
Dialog d = new Dialog (this);
d.setTitle("NO WAY!!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
} finally {
if (diditwork)
{
Dialog d = new Dialog (this);
d.setTitle("Heck YA!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
Intent i = new Intent();
i.setClass(MainActivity.this, DatabaseView.class);
startActivity(i);
}
最后是我获取数据的代码: public class DatabaseView extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.database_view);
tv = (TextView) findViewById(R.id.textView1);
DateBase info = new DateBase(this);
info.open();
String Data = info.getData();
info.close();
tv.setText(Data);
}
}
我在 LogCAT 上遇到的例外是:
E/AndroidRuntime(490): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.database/com.database.DatabaseView}:
android.database.sqlite.SQLiteException: no such column: user_mark: , while compiling: SELECT id, user_name, user_mark, operation FROM user_interface
我只是无法弄清楚问题出在哪里。请帮帮我,对不起我的英语不好
感谢您的时间和考虑。