我刚刚开始使用SQlite数据库并遇到了问题。每当我尝试更新或删除数据时,应用程序崩溃并显示错误"没有这样的列:s_id"。我试图解决它好几天但没有运气。如果有人能告诉我问题是什么以及为什么会发生这种情况,我将不胜感激。谢谢。
这是我的数据库:
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "sqlite.db";
public static final int VERSION = 1;
public static final String TABLE_NAME = "tbl_reminder";
public static final String S_ID = "s_id";
public static final String S_TITLE = "s_title";
public static final String S_DATE = "s_date";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
S_ID +" TEXT)";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP_TABLE" + TABLE_NAME);
onCreate(db);
}
public void insertData (String s_title){
ContentValues contentValues = new ContentValues();
contentValues.put(S_TITLE, s_title);
SQLiteDatabase sqLiteDB = this.getWritableDatabase();
sqLiteDB.insert(TABLE_NAME, null, contentValues);
sqLiteDB.close();
}
public ArrayList<ViewHolderHelper> getAllData() {
ArrayList<ViewHolderHelper> list = new ArrayList<>();
String SQL = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(SQL, null);
if (cursor.moveToFirst()) {
do {
ViewHolderHelper vhh = new ViewHolderHelper();
vhh.setiD(cursor.getInt(0) + "");
vhh.setTitle(cursor.getString(1));
list.add(vhh);
} while (cursor.moveToNext());
}
return list;
}
public void updateData(int id, String title){
ContentValues contentValues = new ContentValues();
contentValues.put(S_TITLE, title);
SQLiteDatabase sqliteDB = this.getWritableDatabase();
sqliteDB.update(TABLE_NAME, contentValues, S_ID + "=" + id, null);
sqliteDB.close();
}
public void deleteData(int id){
SQLiteDatabase sqliteDB = this.getWritableDatabase();
sqliteDB.delete(TABLE_NAME, S_ID + "=" + id, null);
sqliteDB.close();
}
}
以下是显示的错误:
FATAL EXCEPTION: main
Process: json.google_services.newreminderapp, PID: 23419
android.database.sqlite.SQLiteException: no such column: s_id (code 1): , while compiling: UPDATE tbl_reminder SET s_title=? WHERE s_id=1
答案 0 :(得分:0)
删除
public void deletUser(String name) {
String selection = NewUserInfo.USER_NAME + " LIKE ?";
String[] selection_args = {name};
getWritableDatabase().delete(NewUserInfo.TABLE_NAME, selection, selection_args);
}
<强>更新强>
public int updateInformation(String oldName, String newName, String newAge, String newMobile, int newGender) {
String selection = NewUserInfo.USER_NAME + " LIKE ?";
String[] selection_args = {oldName};
ContentValues contentValues = new ContentValues();
contentValues.put(NewUserInfo.USER_NAME, newName);
contentValues.put(NewUserInfo.USER_AGE, newAge);
contentValues.put(NewUserInfo.USER_MOB, newMobile);
contentValues.put(NewUserInfo.USER_GENDER, newGender);
int count = getWritableDatabase().update(NewUserInfo.TABLE_NAME, contentValues, selection, selection_args);
return count;
}
答案 1 :(得分:0)
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("+S_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
S_ID +" TEXT)";
db.execSQL(CREATE_TABLE);
}
答案 2 :(得分:0)
问题在于您的createTable,您将ID定义为主键,将s_id定义为单独的列。然后在你的插入中你从未向你的s_id列提供任何数据,因此你的专栏永远不会真正存在。
要纠正此问题,您应该使用s_id作为自动递增的主键。
你也插入了S_TITLE,我也没有在你的onCreate中看到那一列。
基于课程的其他部分,我相信你的CREATE语句应该是:
"CREATE TABLE " +
TABLE_NAME +&#34; (&#34; + S_ID +&#34; INTEGER AUTOINCREMENT PRIMARY KEY +&#34;,&#34; + S_TITLE +&#34; TEXT&#34;