Android SQLITE DB丢失了所有数据

时间:2016-10-20 04:19:48

标签: android database sqlite illegalstateexception

我知道这是一个非常奇怪的问题。我使用sqlite在一台适用于Android操作系统的机器上存储事务数据。该应用程序具有多个表并执行多个db读/写操作。应用经常遇到应用崩溃。它发现应用程序有时会丢失应用程序崩溃的所有数据。

*我正在使用数据库处理程序类来扩展Sqlite Open帮助程序以执行所有数据库操作。 这是我遵循的教程的链接。 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

DatabaseHandler类

    public class DatabaseHandler extends SQLiteOpenHelper {


private static DatabaseHandler mInstance = null;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 77;

// Database Name
public static final String DATABASE_NAME = "chillarMachine";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
private static final String TABLE_OPERATIONAL_DEVICE_DATA= "operational_device_data";
private static final String TABLE_DEVICE_INFO= "device_info";
private static final String TABLE_USER= "user";
private static final String TABLE_BLOCKED_CARDS_INFO= "blocked_cards_info";
private static final String TABLE_USER_PERMISSION_DATAA= "user_permission_dataa";
private static final String TABLE_ATTENDENCE_TYPE= "attendence_type";
private static final String TABLE_ATTENDENCE_DATA= "attendence_data";
private static final String TABLE_TRANSACTION_TYPE= "transaction_type";
private static final String TABLE_SUCCESS_TRANSACTION= "success_transaction";
private static final String TABLE_PAYMENT_TRANSACTION= "payment_transaction";
private static final String TABLE_PAYMENT_TYPE= "payment_type";
private static final String TABLE_RECHARGE_DATA= "recharge_data";
private static final String TABLE_CATEGORY_LIST= "category_list";
private static final String TABLE_ITEM_STOCK= "item_stock";
private static final String TABLE_ITEM_LIST= "item_list";
private static final String TABLE_SALES_ITEM_LIST= "sales_item_list";
private static final String TABLE_ITEM_SALE_TRANSACTION= "item_sale_transaction";
private static final String TABLE_FEES_TRANSACTIONS= "fees_transactions";
private static final String TABLE_FEES_LIST= "fees_list";
private static final String TABLE_FEES_STRUCTURE= "fees_structure";
private static final String TABLE_LIBRARY_BOOK_TRANSACTION= "library_book_transaction";
private static final String TABLE_ITEM_TYPE= "item_type";

private static final String TABLE_REFUND_TRANSACTION= "refund_transaction";




// Common Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_MACHINE_ID= "machine_id";
private static final String KEY_SERVER_TIMESTAMP= "server_timestamp";
private static final String KEY_TRANSACTION_TYPE_ID= "transaction_type_id";
private static final String KEY_TRANSACTION_ID= "transaction_id";
private static final String KEY_CARD_SERIAL= "card_serial";
private static final String KEY_ITEM_ID= "item_id";
private static final String KEY_CATEGORY_ID= "category_id";
private static final String KEY_SALES_TRANS_ID= "sales_trans_id";
private static final String KEY_BILL_NO= "bill_no";
private static final String KEY_TOTAL_AMOUNT= "total_amount";
private static final String KEY_FEES_TRANS_ID= "fees_trans_id";
private static final String KEY_FEES_ID= "fees_id";


//Device info columns names
private static final String KEY_SERIAL_NO= "serial_no";
private static final String KEY_SCHOOL_ID= "school_id";
private static final String KEY_SCHOOL_NAME= "school_name";
private static final String KEY_SCHOOL_PLACE= "school_place";
private static final String KEY_DEVICE_LAST_TRANS_ID= "device_last_trans_id";
private static final String KEY_MAIN_SERVER_URL= "main_server_url";
private static final String KEY_MAIN_UPLOAD_PATH= "main_upload_path";

//User table Columns names
private static final String KEY_USER_ID= "user_id";
private static final String KEY_USER_NAME= "user_name";
private static final String KEY_PASSWORD= "password";

//Blocked cards info table Columns names
private static final String KEY_BLOCKED_CARDS_ID= "blocked_cards_id";


//User permission data table Column names
private static final String KEY_PERMISSION_ID= "permission_id";

private static final String KEY_PERMISSION= "permission";

//Attendence Type table Column names
private static final String KEY_ATTENDENCE_TYPE_NAME= "attendence_type_name";

//Attendence data table column names
private static final String KEY_ATTENDENCE_DATA_ID= "attendence_data_id";

private static final String KEY_IN_OUT= "in_out";
private static final String KEY_ATTENDENCE_TYPE_ID= "attendence_type_id";

//Transaction type table column names
private static final String KEY_TRANSACTION_TYPE_NAME= "transaction_type_name";

//Success transaction table column names
private static final String KEY_PREV_BALANCE= "prev_balance";
private static final String KEY_CURRENT_BALANCE= "current_balance";
private static final String KEY_TIME_STAMP= "time_stamp";


//Payment transaction table column names
private static final String KEY_AMOUNT= "amount";

//PAyment type table column names
private static final String KEY_PAYMENT_TYPE_NAME= "payment_type_name";

//Recharge data table column names
private static final String KEY_RECHARGE_ID= "recharge_id";
private static final String KEY_RECHARGE_AMOUNT= "recharge_amt";
private static final String KEY_RECHARGE_TIME= "recharge_time";
private static final String KEY_PAYMENT_TYPE_ID= "payment_type_id";



//Fees list table column names

//Fees structure table column names
private static final String KEY_FEES_STRUCTURE_NAME= "fees_structure_name";


// Table Create Statements
// Operational device data table create statement
private static final String CREATE_TABLE_OPERATIONAL_DEVICE_DATA = "CREATE TABLE IF NOT EXISTS "
        + TABLE_OPERATIONAL_DEVICE_DATA + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_LAST_TRANS_ID
        + " TEXT," + KEY_MACHINE_ID + " TEXT," + KEY_LAST_MACHINE_TRANS_TIME
        + " DATETIME," + KEY_LAST_SERVER_TRANS_TIME + " DATETIME," + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";


private static final String CREATE_TABLE_DEVICE_INFO = "CREATE TABLE IF NOT EXISTS "
        + TABLE_DEVICE_INFO + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_MACHINE_ID
        + " TEXT," + KEY_SERIAL_NO + " TEXT," + KEY_SCHOOL_ID
        + " TEXT," +KEY_SCHOOL_NAME+ " TEXT," +KEY_SCHOOL_PLACE+ " TEXT," +KEY_DEVICE_LAST_TRANS_ID+ " TEXT," + KEY_MAIN_SERVER_URL + " TEXT," + KEY_MAIN_UPLOAD_PATH + " TEXT" + ")";


private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + "("
        + KEY_USER_ID + " INTEGER PRIMARY KEY," + KEY_USER_NAME + " TEXT,"
        + KEY_PASSWORD + " TEXT," + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_BLOCKED_CARDS_INFO= "CREATE TABLE IF NOT EXISTS " + TABLE_BLOCKED_CARDS_INFO + "("
        + KEY_BLOCKED_CARDS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_CARD_SERIAL + " TEXT,"
        + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_TABLE_USER_PERMISSION_DATAA = "CREATE TABLE IF NOT EXISTS " + TABLE_USER_PERMISSION_DATAA + "("
        + KEY_PERMISSION_ID + " INTEGER PRIMARY KEY," + KEY_TRANSACTION_TYPE_ID + " INTEGER,"
       + KEY_USER_ID + " INTEGER," + KEY_PERMISSION + " INTEGER," + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_TABLE_ATTENDENCE_TYPE = "CREATE TABLE IF NOT EXISTS " + TABLE_ATTENDENCE_TYPE + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ATTENDENCE_TYPE_NAME + " TEXT,"
        + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_TABLE_ATTENDENCE_DATA = "CREATE TABLE IF NOT EXISTS " + TABLE_ATTENDENCE_DATA + "("
        + KEY_ATTENDENCE_DATA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TRANSACTION_ID + " TEXT,"
        + KEY_IN_OUT + " INTEGER," + KEY_ATTENDENCE_TYPE_ID + " INTEGER," + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_TABLE_TRANSACTION_TYPE = "CREATE TABLE IF NOT EXISTS " + TABLE_TRANSACTION_TYPE + "("
        + KEY_TRANSACTION_TYPE_ID + " INTEGER PRIMARY KEY," + KEY_TRANSACTION_TYPE_NAME + " TEXT,"
        + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

private static final String CREATE_TABLE_SUCCESS_TRANSACTION = "CREATE TABLE IF NOT EXISTS "
        + TABLE_SUCCESS_TRANSACTION + "(" + KEY_TRANSACTION_ID + " VARCHAR PRIMARY KEY," + KEY_USER_ID
        + " INTEGER," + KEY_TRANSACTION_TYPE_ID + " INTEGER," + KEY_PREV_BALANCE
        + " FLOAT," + KEY_CURRENT_BALANCE + " FLOAT," + KEY_CARD_SERIAL + " TEXT," + KEY_TIME_STAMP + " DATETIME," + KEY_SERVER_TIMESTAMP + " DATETIME" + ")";

  public static DatabaseHandler getInstance(Context ctx) {

    // Use the application context, which will ensure that you
    // don't accidentally leak an Activity's context.

    if (mInstance == null) {
        mInstance = new DatabaseHandler(ctx);
    }
    return mInstance;
}



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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);

    db.execSQL(CREATE_TABLE_OPERATIONAL_DEVICE_DATA);
    db.execSQL(CREATE_TABLE_DEVICE_INFO);
    db.execSQL(CREATE_TABLE_USER);
    db.execSQL(CREATE_BLOCKED_CARDS_INFO);
    db.execSQL(CREATE_TABLE_USER_PERMISSION_DATAA);
    db.execSQL(CREATE_TABLE_ATTENDENCE_TYPE);



}

// Adding new User................................................................................................................
void addUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_USER_ID,user.getUser_id());
    values.put(KEY_USER_NAME, user.getUser_name()); // User Name
    values.put(KEY_PASSWORD, user.getPassword()); // User Password
    values.put(KEY_SERVER_TIMESTAMP,user.getServer_timestamp()); //server timestamp

    // Inserting Row
    db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection
}

// Getting single contact
User getUser(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_USER, new String[] { KEY_USER_ID,
                    KEY_USER_NAME, KEY_PASSWORD ,KEY_SERVER_TIMESTAMP}, KEY_USER_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    User user = new User(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
    // return contact
    return user;
}
// Getting All Conta
public List<User> getAllUsers() {
    List<User> userList = new ArrayList<User>();
    // Select All Query


    String selectQuery = "SELECT  * FROM " + TABLE_USER;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            User user = new User();
            user.setUser_id(Integer.parseInt(cursor.getString(0)));
            user.setUser_name(cursor.getString(1));
            user.setPassword(cursor.getString(2));
            // Adding contact to list
            userList.add(user);
        } while (cursor.moveToNext());
    }


    cursor.close();          // Dont forget to close your cursor
    db.close();
    // return contact list
    return userList;
}

// Updating single user
public int updateUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_USER_NAME, user.getUser_name());
    values.put(KEY_PASSWORD, user.getPassword());

    // updating row
    return db.update(TABLE_USER, values, KEY_USER_ID + " = ?",
            new String[] { String.valueOf(user.getUser_id()) });
}

// Deleting single contact
public void deleteUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_USER, KEY_USER_ID + " = ?",
            new String[] { String.valueOf(user.getUser_id()) });
    db.close();
}


// Getting contacts Count
public int getUserCount() {
    String countQuery = "SELECT  * FROM " + TABLE_USER;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
public  void deleteAllUser(){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_USER);
    db.close();
}







// USER PERMISSION DATA ...........................................................................................................



// Getting single contact
User_Permission_Data getUserpermission(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_USER_PERMISSION_DATAA, new String[] { KEY_PERMISSION_ID,KEY_TRANSACTION_TYPE_ID,
                    KEY_USER_ID, KEY_PERMISSION,KEY_SERVER_TIMESTAMP }, KEY_PERMISSION_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    User_Permission_Data contact = new User_Permission_Data(
            Integer.parseInt(cursor.getString(0)),Integer.parseInt(cursor.getString(1)), Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor.getString(3)),cursor.getString(4));
    // return contact
    return contact;
}

// Getting All Contacts
public List<User_Permission_Data> getAllUserspermission() {
    List<User_Permission_Data> permissionList = new ArrayList<User_Permission_Data>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_USER_PERMISSION_DATAA;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            User_Permission_Data userpermission = new User_Permission_Data();
            userpermission.setPermission_id(Integer.parseInt(cursor.getString(0)));
            userpermission.setTransaction_type_id(Integer.parseInt(cursor.getString(1)));
            userpermission.setuser_id(Integer.parseInt(cursor.getString(2)));
            userpermission.setPermission(Integer.parseInt(cursor.getString(3)));
            userpermission.setServer_timestamp(cursor.getString(4));

            // Adding contact to list
            permissionList.add(userpermission);
        } while (cursor.moveToNext());
    }

    cursor.close();          // Dont forget to close your cursor
    db.close();

    // return contact list
    return permissionList;
}

// Updating single user
public int updateUserpermission(User_Permission_Data userpermissiondata) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues valuespermission = new ContentValues();
    valuespermission.put(KEY_TRANSACTION_TYPE_ID, userpermissiondata.getTransaction_type_id());
    valuespermission.put(KEY_USER_ID, userpermissiondata.getuser_id());
    valuespermission.put(KEY_PERMISSION, userpermissiondata.getPermission());
    valuespermission.put(KEY_PERMISSION_ID, userpermissiondata.getPermission_id());
    valuespermission.put(KEY_SERVER_TIMESTAMP, userpermissiondata.getServer_timestamp());

    // updating row
    return db.update(TABLE_USER_PERMISSION_DATAA, valuespermission, KEY_PERMISSION_ID + " = ?",
            new String[] { String.valueOf(userpermissiondata.getuser_id()) });
}

// Deleting single contact
public void deleteUserpermission(User_Permission_Data user) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_USER_PERMISSION_DATAA, KEY_TRANSACTION_TYPE_ID + " = ?",
            new String[] { String.valueOf(user.getuser_id()) });
    db.close();
}


// Getting contacts Count
public int getUserCountpermission() {
    String countQuery = "SELECT  * FROM " + TABLE_USER_PERMISSION_DATAA;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
public  void deleteAllUserpermission(){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from "+ TABLE_USER_PERMISSION_DATAA);
    db.close();
}


public void userPermission(int userId){
    System.out.println("CODMOB:permiss");
    String MY_QUERY = "SELECT transaction_type_name FROM transaction_type INNER JOIN user_permission_dataa" +
            " WHERE user_permission_dataa.user_id=?";
    System.out.println(MY_QUERY);
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(MY_QUERY, new String[]{String.valueOf(userId)});


    TransactionType transactionType=new TransactionType();

    System.out.println("CODMOB:cursor "+ transactionType.getTransaction_type_name());

    cursor.close();

}


public List<Payment_Transaction> getAllpaytransactiontoUp() {
    List<Payment_Transaction> paytrans = new ArrayList<Payment_Transaction>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_PAYMENT_TRANSACTION+" where "+KEY_SERVER_TIMESTAMP+" = ''";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Payment_Transaction paytranss = new Payment_Transaction();

            paytranss.setId(Integer.parseInt(cursor.getString(0)));
            paytranss.setbillno(cursor.getString(1));
            paytranss.settrans_id(cursor.getString(2));

            paytranss.setamount(Float.parseFloat(cursor.getString(3)));


            paytranss.setserver_timestamp(cursor.getString(4));

            // Adding contact to list
            paytrans.add(paytranss);
        } while (cursor.moveToNext());
    }

    cursor.close();          // Dont forget to close your cursor
//        db.close();
    // return contact list
    return paytrans;
}




public List<Refund> getAllRefundtoUp() {
    List<Refund> liblist = new ArrayList<Refund>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_REFUND_TRANSACTION+" where "+KEY_SERVER_TIMESTAMP+" = ''";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Refund sl = new Refund();
            sl.setId(Integer.parseInt(cursor.getString(0)));
            sl.setTrans_id(cursor.getString(1));
            sl.setOrig_trans_id(cursor.getString(2));
            sl.setAmount(cursor.getFloat(3));
            sl.setserver_timestamp(cursor.getString(4));

            // Adding contact to list
            liblist.add(sl);
        } while (cursor.moveToNext());
    }

    cursor.close();          // Dont forget to close your cursor
    // return contact list
    return liblist;
}



public List<Payment_Transaction> getallPaymentbyBill(String transid) {
    List<Payment_Transaction> feetransact = new  ArrayList<Payment_Transaction>();
    // Select All Query
    String selectQuery = "SELECT * FROM " + TABLE_PAYMENT_TRANSACTION+" where "+KEY_TRANSACTION_ID+" = "+"'"+transid+"'";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Payment_Transaction itemsss = new Payment_Transaction();
            itemsss.setId(Integer.parseInt(cursor.getString(0)));
            itemsss.setbillno(cursor.getString(1));
            itemsss.settrans_id(cursor.getString(2));
            itemsss.setamount(Float.valueOf(cursor.getString(3)));
            itemsss.setserver_timestamp(cursor.getString(4));

            // Adding contact to list
            feetransact.add(itemsss);
        } while (cursor.moveToNext());
    }

    cursor.close();          // Dont forget to close your cursor

    // return contact list
    return feetransact;
}




public List<Success_Transaction> getAllsxstoupNew() {
    List<Success_Transaction> successtrans = new ArrayList<Success_Transaction>();
    // Select All Query
    String selectQuery = "SELECT * FROM " + TABLE_SUCCESS_TRANSACTION+" where "+KEY_SERVER_TIMESTAMP+" = '-1'";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Success_Transaction successss = new Success_Transaction();
            successss.settrans_id(cursor.getString(0));
            successss.setuser_id(Integer.parseInt(cursor.getString(1)));
            successss.settarans_type_id(Integer.parseInt(cursor.getString(2)));
            successss.setprevious_balnce(Float.parseFloat(cursor.getString(3)));
            successss.setcurrent_balance(Float.parseFloat(cursor.getString(4)));
            successss.setcard_serial(cursor.getString(5));
            successss.settime_stamp(cursor.getString(6));
            successss.setserver_timestamp(cursor.getString(7));
            // Adding contact to list
            successtrans.add(successss);
        } while (cursor.moveToNext());
    }
    cursor.close();          // Dont forget to close your cursor
    // return contact list
    return successtrans;
}



public List<Item_Sale> getAllitemsaletoUpNew() {
    List<Item_Sale> feetransact = new ArrayList<Item_Sale>();
    // Select All Query
    String selectQuery = "SELECT * FROM " + TABLE_ITEM_SALE_TRANSACTION+" where "+KEY_SERVER_TIMESTAMP+" = '-1'";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Item_Sale itemsss = new Item_Sale();
            itemsss.setsale_trans_id(cursor.getString(0));
            itemsss.settransaction_id(cursor.getString(1));
            itemsss.setbill_no(cursor.getString(2));
            itemsss.settot_amount(cursor.getString(3));
            itemsss.setserver_timestamp(cursor.getString(4));

            // Adding contact to list
            feetransact.add(itemsss);
        } while (cursor.moveToNext());
    }
    cursor.close();          // Dont forget to close your cursor
//        db.close();
    // return contact list
    return feetransact;
}

public List<Sales_Item> getAllsaletoUpNew() {
    List<Sales_Item> liblist = new ArrayList<Sales_Item>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_SALES_ITEM_LIST+" where "+KEY_SERVER_TIMESTAMP+" = '-1'";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Sales_Item sl = new Sales_Item();
            sl.setSales_item_id(Integer.parseInt(cursor.getString(0)));
            sl.setsales_trans_id(cursor.getString(1));
            sl.setitem_id(cursor.getString(2));
            sl.setitem_quantity(cursor.getString(3));
            sl.setamount(cursor.getString(4));
            sl.setserver_timestamp(cursor.getString(5));


            // Adding contact to list
            liblist.add(sl);
        } while (cursor.moveToNext());
    }
    cursor.close();          // Dont forget to close your cursor
//        db.close();
    // return contact list
    return liblist;
}



public String userLogin(String uName,String uPass){
    String selectQuery ="SELECT * FROM " + TABLE_USER + " WHERE " + KEY_USER_NAME + "='" + uName +"'AND "+KEY_PASSWORD+"='"+uPass+"'";

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

    if (cursor!=null&&cursor.moveToFirst()) {

        String uID=cursor.getString(cursor.getColumnIndex(KEY_USER_ID));

        System.out.println("LOGIN USER ID : "+uID);

        cursor.close();
        return uID;
    }
    cursor.close();
    return "false";


}

public String getSinlgeEntry(String userName)
{
    String password="";
    try {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_USER, null, KEY_USER_NAME + "=?", new String[]{userName}, null, null, null);
        if (cursor.getCount() < 1) // UserName Not Exist
        {
            cursor.close();
            return "NOT EXIST";
        }
        cursor.moveToFirst();
        password = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
        cursor.close();
    }catch(Exception e){
        return "exception";
    }
    return password;
}

}

1 个答案:

答案 0 :(得分:0)

尝试在模拟器中运行您的应用程序并在崩溃后检查数据库以查看它是否实际销毁/重新创建或数据存在且您的方法未提取数据(例如错误的密钥)

如果没有日志,

无法提供更多帮助