无法插入sqlite数据库

时间:2016-06-16 05:47:10

标签: android database sqlite

我试图在sqlite数据库中插入值,但它显示一个错误,指出无法将值插入数据库没有找到这样的表。即使我尝试更改数据库版本并卸载应用程序并再次安装它但是它没有用。

数据库:

public class BuisnessDatabaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "receiptExample";

// Labels table name
private static final String TABLE_LABELSS = "buisnesslabels";
private static final String KEY_ID = "id";
private static final String KEY_IMAGE = "image";
private static final String KEY_STATUS="status";
private static final String KEY_UPLOADSTATUS = "uplaodstatus";
private static final String KEY_USERID="userid";
SQLiteDatabase db;
public BuisnessDatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // Category table create query
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELSS + "("
            + KEY_ID + " INTEGER PRIMARY KEY, "
            + KEY_IMAGE + " BLOB, "
            + KEY_STATUS + " TEXT, "
            + KEY_UPLOADSTATUS + " TEXT, "
            + KEY_USERID + " TEXT" + ");";

    db.execSQL(CREATE_CATEGORIES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELSS);

    // Create tables again
    onCreate(db);
}
public void insertLabel(byte[] imag,String status,String uploadstatus,String userid){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    //values.put(KEY_ID,id);
    values.put(KEY_IMAGE,imag);
    values.put(KEY_STATUS,status);
    values.put(KEY_UPLOADSTATUS,uploadstatus);
    values.put(KEY_USERID,userid);
    // Inserting Row
    db.insert(TABLE_LABELSS, null, values);
    db.close(); // Closing database connection
}
public ArrayList<BuisnesslistItems> getAllLabels(){
    //  List<String> labels = new ArrayList<String>();
    ArrayList<BuisnesslistItems>labels=new ArrayList<BuisnesslistItems>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELSS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {

            BuisnesslistItems buisnesslistItems=new BuisnesslistItems();
            buisnesslistItems.setImage(cursor.getBlob(1));
            buisnesslistItems.setStatus("Status:"+cursor.getString(2));


            labels.add(buisnesslistItems);
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}

public String composeJSONfromSQLite(){
    ArrayList<HashMap<String, String>> label;
    label = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM buisnesslabels where " + KEY_STATUS + " = '"+"no"+"'";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        String encodedImage = Base64.encodeToString(cursor.getBlob(1), Base64.DEFAULT);
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(KEY_ID, cursor.getString(0));
            map.put(KEY_IMAGE, encodedImage);
            map.put(KEY_USERID,cursor.getString(4));
            map.put(KEY_STATUS,"yes");
            label.add(map);
        } while (cursor.moveToNext());
    }
    database.close();
    Gson gson = new GsonBuilder().create();
    //Use GSON to serialize Array List to JSON
    return gson.toJson(label);
}

/**
 * Get Sync status of SQLite
 * @return
 */
public String getSyncStatus(){
    String msg = null;
    if(this.dbSyncCount() == 0){
        msg = "Wallet is Sync with cloud!";
    }else{
        msg = "Wallet needs to be Sync\n";
    }
    return msg;
}

/**
 * Get SQLite records that are yet to be Synced
 * @return
 */
public int dbSyncCount(){
    int count = 0;
    String selectQuery = "SELECT  * FROM buisnesslabels where " +KEY_STATUS+" = '"+"no"+"'";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    count = cursor.getCount();
    database.close();
    return count;
}

/**
 * Update Sync status against each User ID
 * @param id
 * @param status
 */
public void updateSyncStatus(String id, String status){
    SQLiteDatabase database = this.getWritableDatabase();
    String updateQuery = "Update buisnesslabels set " + KEY_STATUS + " = '"+ status +"'," + KEY_UPLOADSTATUS + " = 'Uploaded' where " + KEY_ID + "="+"'"+ id +"'";
    Log.d("query", updateQuery);
    database.execSQL(updateQuery);
    database.close();
}
public void deleteUploaded(String status){
    SQLiteDatabase database=this.getWritableDatabase();
    String deleteQuery="Delete From buisnesslabels where " + KEY_STATUS+ " = 'yes' ";
    Log.d("dquery",deleteQuery);
    database.execSQL(deleteQuery);
    database.close();
}

即使我在同一个数据库中有另一个表,但它工作正常我不知道为什么会出现这样的错误。

错误:

06-16 11:09:14.681  16067-16067/nidhinkumar.reccs E/SQLiteDatabase﹕ Error inserting uplaodstatus=waiting to upload status=no userid=7 image=[B@3f20a44d
     android.database.sqlite.SQLiteException: no such table: buisnesslabels (code 1): , while compiling: INSERT INTO buisnesslabels(uplaodstatus,status,userid,image) VALUES (?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1499)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1371)
        at nidhinkumar.reccs.buisnesscarddetails.BuisnessDatabaseHandler.insertLabel(BuisnessDatabaseHandler.java:70)
        at nidhinkumar.reccs.buisnesscarddetails.Buisnesscardmain.uploa(Buisnesscardmain.java:358)
        at nidhinkumar.reccs.buisnesscarddetails.Buisnesscardmain.onOptionsItemSelected(Buisnesscardmain.java:343)
        at android.app.Activity.onMenuItemSelected(Activity.java:2940)

0 个答案:

没有答案