Android 工作室创建 table

Android studio create table

我试图将一个新的 table 添加到我之前创建的数据库中,但是当我尝试添加数据时它说找不到 table。

我尝试调试,我认为问题出在下面的方法上,它没有执行第二个 table,这是我的新 table 第一个 table 已经存在

`public void onCreate(SQLiteDatabase db) {
   db.execSQL(TABLE_CREATE);
   db.execSQL(PASSWORD_TABLE_CREATE);

}`

这是我的 table 创建语句

private static final String PASSWORD_TABLE_CREATE = "create table password (p_id integer primary key autoincrement not null, " + "p_text text not null, p_description text, p_category text not null);";


private static final String TABLE_CREATE = "create table accounts (id integer primary key autoincrement not null , " + "name text not null , email text not null , password text not null);";

您应该在创建后更改您的数据库,方法是使用 -

升级它
onUpgrade(SQLiteDatabase, int, int)

所以添加

db.execSQL(PASSWORD_TABLE_CREATE);

in onUpgrade(SQLiteDatabase, int, int) 如果 TABLE_CREATE 已经创建并且您想添加一个新的 table PASSWORD_TABLE_CREATE

此外,请在每个 CREATE TABLE 中添加 IF NOT EXISTS 以避免在您尝试创建任何已存在的 table 时出现任何错误。

private static final String PASSWORD_TABLE_CREATE = "create table if not exists password (p_id integer primary key autoincrement not null, " + "p_text text not null, p_description text, p_category text not null);";



private static final String TABLE_CREATE = "create table accounts (id integer primary key autoincrement not null , " + "name text not null , email text not null , password text not null);";

这些查询有效。问题是您必须清除数据,然后重新 运行 您的项目。否则,您必须 version 您的数据库并执行新的创建查询。