没有这样的列 SQLite Android

huangapple go评论80阅读模式
英文:

No Such Column SQLite Android

问题

以下是翻译好的内容:

有人知道为什么我会收到错误消息,指示我没有名为COL2的列吗?

错误信息如下所示:

插入错误,COL2=sdfsdf
android.database.sqlite.SQLiteException: 表TWhereToGoList没有名为COL2的列(错误代码1 SQLITE_ERROR):在编译时:INSERT INTO TWhereToGoList(COL2)VALUES(?)

在我的合同类中,我创建了数据库变量:

public static final class YourEntry implements BaseColumns {
    public static final String TABLE_NAME = "TWhereToGoList";
    public static final String intWhereToGoListOID = BaseColumns._ID;
    public static final String COL2 = "COL2";

我想问你是否可以帮助我解决这个问题:

我的数据库助手类(片段):

@Override
public void onCreate(SQLiteDatabase db) {
    db.deleteDatabase(new File(DATABASE_NAME));
    String createTable = ("CREATE TABLE " + Contract.YourEntry.TABLE_NAME + " (" +
            Contract.YourEntry.intWhereToGoListOID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            Contract.YourEntry.COL2 + " TEXT);"
    );
    db.execSQL(createTable);
}

public boolean addData(String string) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    // 将值放在COL2处
    values.put(Contract.YourEntry.COL2, string);

    // 在此处出现错误
    long result = db.insert(Contract.YourEntry.TABLE_NAME, null, values);

    // 如果插入日期不正确,它将返回-1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

感谢任何答案,如果您有任何问题,请在下面的评论中提问 没有这样的列 SQLite Android

英文:

Does anyone know why I get the error that I "do not have" a column named COL2?

Error inserting COL2=sdfsdf
    android.database.sqlite.SQLiteException: table TWhereToGoList has no column named COL2 (code 1 SQLITE_ERROR): , while compiling: INSERT INTO TWhereToGoList(COL2) VALUES (?)

My Contract class here I create the DB variables:

public static final class YourEntry implements BaseColumns {
    public static final String TABLE_NAME = "TWhereToGoList";
    public static final String intWhereToGoListOID = BaseColumns._ID;
    public static final String COL2 = "COL2";

I would like to ask you if you could help me out there:

My DB Helper class (snippets):

@Override
public void onCreate(SQLiteDatabase db) {
    db.deleteDatabase(new File(DATABASE_NAME));
    String createTable = ("CREATE TABLE " + Contract.YourEntry.TABLE_NAME + " (" +
            Contract.YourEntry.intWhereToGoListOID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            Contract.YourEntry.COL2 + "TEXT);"
    );
    db.execSQL(createTable);

public boolean addData(String string) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    //Wo dass die values hingelegt werden @COL2
    values.put(Contract.YourEntry.COL2, string);

I get the error right here:

> long result = db.insert(Contract.YourEntry.TABLE_NAME, null, values);

    //if date as inserted incorrectly it will return -1
    if (result == -1) {
        return false;
    } else {
        return true;
    }
}

Thanks for any answers, if you have some questions ask in the comments below 没有这样的列 SQLite Android

答案1

得分: 2

Obviously you are missing a white space at your database creation:

String createTable = ("创建表格" + Contract.YourEntry.TABLE_NAME + " (" +
        Contract.YourEntry.intWhereToGoListOID + " 整数 主键 自动增量, " +
        Contract.YourEntry.COL2 + " 文本);"
);

在最后一部分,你应该有一个额外的空格 文本);,而现在是 文本);

英文:

Obviously you are missing a white space at your database creation:

String createTable = ("CREATE TABLE " + Contract.YourEntry.TABLE_NAME + " (" +
        Contract.YourEntry.intWhereToGoListOID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        Contract.YourEntry.COL2 + " TEXT);"
);

in the last part, you should have an extra space " TEXT);" which is now "TEXT);"

答案2

得分: 1

SQLiteDatabase在首次调用SQLiteOpenHelper时会被自动创建,而您正在onCreate中使用db.deleteDatabase(new File(DATABASE_NAME));代码行来删除它。只需移除此行代码,(重新)重新安装应用程序,您的数据库应该能够正确创建,并且插入操作应该能够正常运行。

需要注意的是,如果您在创建表时只有一个列,然后再添加另一列,您必须在重写的onUpdate方法中处理此情况,onCreate不会再次被调用。

英文:

SQLiteDatabase is auto-created when first SQLiteOpenHelper gets called and you are deleting it onCreate with db.deleteDatabase(new File(DATABASE_NAME)); line. just remove it, (fresh) reinstall app and you db should be created properly and inserting should start work

note that when you will create your table with e.g. only one and further you will add another column then you have to handle this in overidden onUpdate method, onCreate won't be called again

huangapple
  • 本文由 发表于 2020年10月19日 13:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64421794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定