Android Studio SQLiteDatabase - 没有这样的列[重复]

问题描述 投票:-1回答:3

这个问题在这里已有答案:

我刚刚开始使用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
android database sqlite android-sqlite
3个回答
0
投票

问题在于您的createTable,您将ID定义为主键,将s_id定义为单独的列。然后在您的插入中,您从未向您的s_id列提供任何数据,因此您的列永远不会真正存在。

要更正此问题,您应该使用_id作为自动递增的主键。

你也插入S_TITLE,我也没有在你的onCreate中看到那个列。

根据课程的其他部分,我相信你的CREATE语句应该是:

"CREATE TABLE " +TABLE_NAME +“(”+ S_ID +“INTEGER AUTOINCREMENT PRIMARY KEY +”,“+ S_TITLE +”TEXT“


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);
}

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.