gpt4 book ai didi

android - 异常 "table.... has no column named...."

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:23 27 4
gpt4 key购买 nike

我在尝试插入名为“my_card”的表时遇到了一个非常奇怪的错误。

这些是我在单独的接口(interface)中定义的一些常量:

//tables name
public static final String TABLE_MY_CARD= "my_card" ;
public static final String TABLE_MY_CONTACTS = "my_contacts" ;

// Columns
public static final String NAME = "name" ;
public static final String PHONE = "phone" ;
public static final String EMAIL = "email";

public static final String FACEBOOK = "facebook";
public static final String WEBSITE = "web";
public static final String MAJOR = "major";
public static final String ULINK = "ulink";

之后我有一个扩展 SQLiteOpenHelper 的类,它有 2 个函数。最重要的是第一个创建参数给定的表。

public class MySQLite_methods extends SQLiteOpenHelper implements Tables {

private static final String DATABASE_NAME = "my_database.db" ;
private static final int DATABASE_VERSION = 1;
private String TABLE_NAME;

/** Create a helper object for the Events database */
public MySQLite_methods (Context ctx, String table_name)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
this.TABLE_NAME = table_name;
}

@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT NOT NULL," +
PHONE + " TEXT NOT NULL,"+
EMAIL + " TEXT NOT NULL,"+
FACEBOOK + " TEXT,"+
WEBSITE + " TEXT,"+
MAJOR + " TEXT NOT NULL,"+
ULINK + " TEXT NOT NULL" + ");" ;

db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

最后,在我的 Activity 中,按下保存按钮后,将创建该类的一个实例,并且必须将一些 EditText-s 的值插入到数据库中:

            MySQLite_methods mysql = new MySQLite_methods (context, TABLE_MY_CARD);


Log.d("geo", "before getWr....");

// getting the database..
SQLiteDatabase db = mysql.getWritableDatabase();

// creating an object of type ContentValues which represents the record i am gonna add into the DB
ContentValues values = new ContentValues();

values.put(NAME, name.getText().toString());
values.put(PHONE, phone.getText().toString());
values.put(EMAIL, email.getText().toString());

values.put(FACEBOOK, facebook.getText().toString());
values.put(WEBSITE, website.getText().toString());
values.put(MAJOR, major.getText().toString());
values.put(ULINK, ulink.getText().toString());

Log.d("geo", "before insert");

// inserting data
db.insertOrThrow(TABLE_MY_CARD, null, values);

在插入时它给出了这个异常:

“android.database.sqlite.SQLiteException:表 my_card 没有名为 web ... 的列。”

我一遍又一遍地检查拼写错误,但没有...所以我现在不知道该怎么办...有什么想法吗?谢谢

最佳答案

您的创建 SQL 以 CREATE TABLE IF NOT EXISTS 开头。所以我的想法是您在设备已经有该表的情况下添加了 web 列。如果是这种情况,则只需重新安装该应用程序 - 它会删除数据库,因此您可以确定将使用该列创建表。

关于android - 异常 "table.... has no column named....",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197396/

27 4 0