插入新表时“没有这样的表”错误

时间:2013-11-15 08:13:17

标签: android sqlite android-sqlite sql-insert

我正在尝试创建一个数据库但是当我尝试在表中插入值时,我收到错误消息,表示没有找到表。

这是我的databasemanager类:

public class DatabaseManager {
    private Context context = null;
    private SQLiteDatabase database=null;
    private DatabaseHelper dbHelper=null;

    public DatabaseManager(Context context) {
        this.context = context;
        dbHelper = new DatabaseHelper(context);
    }

    public void open() throws SQLException {
        if (dbHelper==null) {
            dbHelper = new DatabaseHelper(context);
        }

        if (database==null) {
            database = dbHelper.getWritableDatabase();
        }
    }

    public void close() {
        dbHelper.close();
        database = null;
        dbHelper = null;       
    }

    public long insertCategoriesDetail(Category categorydetail){
        long result = 0;

        try{
            if (database==null) {
                open();
            }
            ContentValues values = new ContentValues();
            values.put(DatabaseContract.RetailDatabaseEntry.COLUMN_CATEGORYDESCRIPTION, categorydetail.getCategorydescription());
            //values.put(DatabaseContract.RetailDatabaseEntry.COLUMN_CATEGORYIMAGE, categorydetail.getCategoryimage());

            result = database.insert(DatabaseContract.RetailDatabaseEntry.TABLE_NAME_CATEGORY, null, values);
            Toast.makeText(context, "Res : " + result, Toast.LENGTH_SHORT).show();
            close();
        }
        catch(Exception ex){
            Toast.makeText(context, "crashed" + ex.toString(), Toast.LENGTH_SHORT).show();
        }

        return result;
    }

    public List<Category> getCategoriesDetail() {
        if (database==null) {
            open();
        }

        List<Category> categoriesdetailsList = new ArrayList<Category>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + DatabaseContract.RetailDatabaseEntry.TABLE_NAME_CATEGORY;

        Cursor cursor = database.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Category detailList = new Category();
                detailList.setId(Integer.parseInt(cursor.getString(0)));
                detailList.setCategorydescription(cursor.getString(1));
                detailList.setCategoryimage(cursor.getBlob(2));
                categoriesdetailsList.add(detailList);
            } while (cursor.moveToNext());
        }

        close();
        return categoriesdetailsList;
    }
}

这是db创建类:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TEXT_TYPE = " TEXT";
    private static final String INTEGER_TYPE = " INTEGER";
    private static final String COMMA_SEP = ", ";
    private static final String BLOB_TYPE = " BLOB";

    private static final  String SQL_CREATE_RETAIL_DB_CATEGORY =
    "CREATE TABLE " + DatabaseContract.RetailDatabaseEntry.TABLE_NAME_CATEGORY + " (" +
    DatabaseContract.RetailDatabaseEntry.COLUMN_ID_CATEGORY+ " INTEGER PRIMARY KEY AUTOINCREMENT," +
    DatabaseContract.RetailDatabaseEntry.COLUMN_CATEGORYDESCRIPTION + TEXT_TYPE + COMMA_SEP +
    DatabaseContract.RetailDatabaseEntry.COLUMN_CATEGORYIMAGE + BLOB_TYPE + " )";

    private static final String SQL_DELETE_RETAIL_DB_CATEGORY =
    "DROP TABLE IF EXISTS " + DatabaseContract.RetailDatabaseEntry.TABLE_NAME_CATEGORY ;

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "RETAILNYURALS.db";

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

    @SuppressLint("NewApi")
    public DatabaseHelper(Context context, String name, CursorFactory factory,

    int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_RETAIL_DB_CATEGORY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_RETAIL_DB_CATEGORY);
        onCreate(db);
    }
}

0 个答案:

没有答案