Android sqlitedatabase没有这样的表

时间:2014-02-21 10:48:07

标签: android sqlite

在我的SQLiteOpenHelper类中使用我的数据库时,我得到一个'没有这样的表'错误,即使onCreate方法被调用eand我在该方法中创建了表结构。

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): , while compiling: SELECT _id, teamName FROM team WHERE _id=? OR _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,当我需要创建或删除我的结构时,我会将其循环。

1 个答案:

答案 0 :(得分:3)

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

  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 + "); " 

在这里,我看到你只是演示。由你自己纠正。