我需要在android上使用SQLite。我可以添加数据和查看数据。但是现在我需要在这种情况下添加更多列我设置“COL_TIME”。首先我尝试更改列以将数据从“COL_LASTNAME”记录到“COL_TIME”,但是日志cat显示列“COL_TIME”不存在。尽管我在Databasestudent.Java中声明了public static final String COL_TIME = "date";
或者我做错了,请提供一些建议。
这是添加数据和数据库类的类。
DatabaseStudent.Java
class DatabaseStudent extends SQLiteOpenHelper {
private static final String DB_NAME = "MyStudent";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "Student";
public static final String COL_NAME = "name";
public static final String COL_LASTNAME = "last_name";
public static final String COL_SCHOOL = "school";
public static final String COL_TIME = "date";
public DatabaseStudent(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_NAME + " TEXT, " + COL_LASTNAME
+ " TEXT, " + COL_SCHOOL + " TEXT);");
db.execSQL("INSERT INTO " + TABLE_NAME + " ("
+ COL_NAME + ", " + COL_LASTNAME
+ ", " + COL_SCHOOL + ") VALUES ('Sleeping'"
+ ", 'For Less', 'Android School');");
//db.execSQL("INSERT INTO " + TABLE_NAME + " ("
// + COL_DATE + ") VALUES ('Android School');");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion
, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
AddStudent.Java
public class AddStudent extends Activity {
DatabaseStudent mHelper;
SQLiteDatabase mDb;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
mHelper = new DatabaseStudent(this);
mDb = mHelper.getWritableDatabase();
final EditText editName = (EditText)findViewById(R.id.editName);
final EditText editLastName = (EditText)findViewById(R.id.editLastName);
final EditText editSchool = (EditText)findViewById(R.id.editSchool);
Button buttonAdd = (Button)findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name = editName.getText().toString();
String lastname = editLastName.getText().toString();
double school = getIntent().getDoubleExtra("Intent", 5.322);
//Date&Time
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
if(name.length() != 0 && lastname.length() != 0
) {//&& school.length() != 0
Cursor mCursor = mDb.rawQuery("SELECT * FROM "
+ DatabaseStudent.TABLE_NAME + " WHERE "
+ DatabaseStudent.COL_NAME + "='" + name + "'"
+ " AND " + DatabaseStudent.COL_LASTNAME + "='"
+ currentTime + "'" + " AND "
+ DatabaseStudent.COL_SCHOOL + "='" + school //add COL_SCHOOL = currentTime
+ "'", null);
if(mCursor.getCount() == 0) {
mDb.execSQL("INSERT INTO " + DatabaseStudent.TABLE_NAME
+ " (" + DatabaseStudent.COL_NAME
+ ", " + DatabaseStudent.COL_LASTNAME
+ ", " + DatabaseStudent.COL_SCHOOL
+ ") VALUES ('" + name + "', '" + currentTime //result ไม่มา
+ "', '" + school + "');");
editName.setText("");
editLastName.setText("");
editSchool.setText("");
}
});
}
public void onStop() {
super.onStop();
mHelper.close();
mDb.close();
}
}
答案 0 :(得分:1)
如果要在创建后修改数据库。您可以通过更改其版本代码将旧版本的数据库升级到新版本。
您需要从设备中卸载旧版本的数据库,然后才会显示已更改的数据库版本的反映。
因此,在从设备安装新的应用程序之前,请确保更改数据库版本并卸载旧的应用程序数据。
private static final int DB_VERSION = 1;
到
private static final int DB_VERSION = 2;
答案 1 :(得分:0)
如果要升级数据库,即更改表结构,则必须增加数据库版本,以便android知道必须升级它。为此,您需要增加此值。
private static final int DB_VERSION = 1;
到
private static final int DB_VERSION = 2;