gpt4 book ai didi

Android sqlitedatabase 没有这样的表

转载 作者:行者123 更新时间:2023-11-29 17:52:38 25 4
gpt4 key购买 nike

在我的 SQLiteOpenHelper 类中寻址我的数据库时,我收到“没有这样的表”错误,即使调用了 onCreate 方法并且我在该方法中创建了表结构。

public class BVCDbHelper extends SQLiteOpenHelper {

// Increment when db schema changes
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "BVC.db";

private static final String INTEGER_TYPE = " INTEGER";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + Games.TABLE_NAME + " (" +
Games._ID + TEXT_TYPE + " PRIMARY KEY," +
Games.COLUMN_NAME_HOME + TEXT_TYPE + COMMA_SEP +
Games.COLUMN_NAME_GUESTS + TEXT_TYPE + COMMA_SEP +
Games.COLUMN_NAME_TIMESTAMP + INTEGER_TYPE + "); " +
"CREATE TABLE " + Sets.TABLE_NAME + " (" +
Sets._ID + INTEGER_TYPE + " PRIMARY KEY," +
Sets.COLUMN_NAME_GAME_CODE + TEXT_TYPE + COMMA_SEP +
Sets.COLUMN_NAME_HOME + INTEGER_TYPE + COMMA_SEP +
Sets.COLUMN_NAME_GUESTS + INTEGER_TYPE + "); " +
"CREATE TABLE " + Teams.TABLE_NAME + " (" +
Teams._ID + TEXT_TYPE + " PRIMARY KEY," +
Teams.COLUMN_NAME_NAME + TEXT_TYPE + "); " +
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + Games.TABLE_NAME
+ "; DROP TABLE IF EXISTS " + Sets.TABLE_NAME
+ "; DROP TABLE IF EXISTS " + Teams.TABLE_NAME

public BVCDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
Log.i("BVCDbHelper", "Table structures created in " + db.getPath());
}

错误:

android.database.sqlite.SQLiteException: no such table: team (code 1): ,编译时:
SELECT _id, teamName FROM team WHERE _id=?或者 _id=?

onCreate 的执行没有问题,并且不会产生错误。生成此错误的方法尝试插入游戏,但当我插入游戏时它不会返回此类错误。这可能是什么原因造成的?

/** Create team */
public void addTeam(Team t){
ContentValues values = new ContentValues();
values.put(Teams._ID, t.getCode());
values.put(Teams.COLUMN_NAME_NAME, t.getName());

insert(Teams.TABLE_NAME, null, values);
}
/** General insert method */
private void insert(String tableName, String nullColumnHack, ContentValues values){
SQLiteDatabase db = this.getWritableDatabase();
db.insertOrThrow(tableName, nullColumnHack, values);
db.close();
}

或者我应该尝试为每个表而不是一个表使用单独的语句(和 execSQL 方法)来创建我的表吗?

更新:我需要一张一张地创建表格。这解决了我的问题。我现在有了带有创建和删除条目的 ArrayLists,当我需要创建或删除我的结构时,我会循环它。

最佳答案

Create Query 中添加 Column NameColumn Type 之间的空格,例如:

  private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + Games.TABLE_NAME + " (" +
Games._ID + " "+TEXT_TYPE + " PRIMARY KEY, " +
Games.COLUMN_NAME_HOME + " "+TEXT_TYPE + COMMA_SEP +
Games.COLUMN_NAME_GUESTS + " "+TEXT_TYPE + COMMA_SEP +
Games.COLUMN_NAME_TIMESTAMP + " "+INTEGER_TYPE + "); " +
"CREATE TABLE " + Sets.TABLE_NAME + " (" +
Sets._ID +" "+INTEGER_TYPE + " PRIMARY KEY, " +
Sets.COLUMN_NAME_GAME_CODE +" "+TEXT_TYPE + COMMA_SEP +
Sets.COLUMN_NAME_HOME +" "+INTEGER_TYPE + COMMA_SEP +
Sets.COLUMN_NAME_GUESTS " "+INTEGER_TYPE + "); "

在这里我看到你只是演示。自行更正。

关于Android sqlitedatabase 没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com