gpt4 book ai didi

java - 如何使用 android 在 SQLite 中添加更多列

转载 作者:行者123 更新时间:2023-12-01 13:23:42 26 4
gpt4 key购买 nike

我需要在 android 中使用 SQLite。我已经可以添加数据和查看数据了。但现在我需要添加更多列,在这种情况下我设置了“COL_TIME”。首先,我尝试将记录数据的列从“COL_LASTNAME”更改为“COL_TIME”,但日志猫显示列“COL_TIME”不存在。尽管我在 Databasestudent.Java 中声明 public static final String COL_TIME = "date";或者我做错了什么,请提供一些建议。这是添加数据的类和数据库类。

数据库Student.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();
}
}

最佳答案

如果您想在创建数据库后修改它。您假设通过更改版本代码将数据库的旧版本升级到新版本。

您需要从设备上卸载旧版本的数据库,然后它才会显示更改后的数据库版本的反射(reflect)。

因此,请确保在从设备安装新应用程序数据之前更改数据库版本并卸载旧应用程序数据。

private static final int DB_VERSION = 1;

private static final int DB_VERSION = 2;

关于java - 如何使用 android 在 SQLite 中添加更多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898618/

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