SQLite Android可以';我想不出哪里出了错
我正在写一个简单的数据库应用程序,在这个程序中,您可以使用2个编辑文本输入您的姓名和标记,然后按下按钮保存数据库,并转到第二个活动,在该活动中显示数据库结果。 数据库包含4列:id、名称、标记、操作 其中“operations”列从ops中获取结果,我已经将其输入放入程序中 我在插入时放置了一个try-and-catch,它会显示一个对话框来通知我它是否成功。结果是成功的,但是当移动到查看活动时,应用程序强制关闭。这是我的数据库类代码SQLite Android可以';我想不出哪里出了错,android,cursor,android-sqlite,Android,Cursor,Android Sqlite,我正在写一个简单的数据库应用程序,在这个程序中,您可以使用2个编辑文本输入您的姓名和标记,然后按下按钮保存数据库,并转到第二个活动,在该活动中显示数据库结果。 数据库包含4列:id、名称、标记、操作 其中“operations”列从ops中获取结果,我已经将其输入放入程序中 我在插入时放置了一个try-and-catch,它会显示一个对话框来通知我它是否成功。结果是成功的,但是当移动到查看活动时,应用程序强制关闭。这是我的数据库类代码 public class DateBase { publ
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);
}
我在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
我就是想不出问题出在哪里。请帮帮我,对不起,我英语不好
谢谢您的时间和考虑。可能您稍后添加了此字段用户标记,因此系统仍在使用旧数据库尝试卸载应用程序并重新安装,否则我认为一切都已就绪。可能您稍后添加此字段用户标记,因此系统仍在使用旧数据库尝试卸载应用程序并再次安装,否则我认为一切都很正常是的,代码中没有错误问题是Aashish告诉我的。因此,请清除管理应用程序中的数据,然后重新运行。是的,代码中没有错误。问题是Aashish告诉我们的。因此,请清除管理应用程序中的数据,然后重新运行。答案很明显…检查列名用户标记检查列名用户标记如果相同,仍然会出现异常尝试更改列名它是相同的尝试将列名更改为“标记”异常变成:没有这样的列:标记:…答案很明显…检查列名user\u mark检查列名user\u mark如果相同,仍然出现异常尝试更改列名它是相同的尝试将列名更改为“marks”异常变成:没有这样的列:标记:。。。
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