samedi 27 février 2016

E/SQLiteDatabase: Error inserting

I'm trying to create SQLiteDatabase that functionality to keep user login when the table not null, if it is null the activity will request the user and password, and the table is have two rows first is Name and second is position. but i don't know why it give me this error:

02-27 09:53:22.142 11519-11519/com.amaiub.hussain.smartmenu E/SQLiteLog: (1) no such table: User_Table
02-27 09:53:22.149 11519-11519/com.amaiub.hussain.smartmenu E/SQLiteDatabase: Error inserting user_position=admin user_name=hussain
                                                                              android.database.sqlite.SQLiteException: no such table: User_Table (code 1): , while compiling: INSERT INTO User_Table(user_position,user_name) VALUES (?,?)

I create this table in database

public class DatabaseHelper extends SQLiteOpenHelper {

    private final static String DB_NAME = "e-Order";
    private final static int DB_VERSION = 1;
    private final String USER_TABLE = "User_Table";


    private String USER="user_name",POSITION="user_position";

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub


        db.execSQL("CREATE TABLE '" + USER_TABLE + "'('" + USER + "'TEXT,'" + POSITION + "'TEXT)");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS '" + USER_TABLE + "'");

        onCreate(db);
    }
    public int logIn(String userName, String userPosition) {

        int result = 0;

        SQLiteDatabase db = getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put(USER, userName);
        cv.put(POSITION, userPosition);

        result = (int) db.insert(USER_TABLE, null, cv);

        if (db != null)
            db.close();

        return result;
    }
    public int getUser() {

        int total = 0;

        SQLiteDatabase db = getReadableDatabase();

        Cursor cur = db.query(USER_TABLE, null, null, null, null, null, null);

        if (cur.getCount() > 0)
            total = cur.getCount();

        if (cur != null)
            cur.close();

        if (db != null)
            db.close();

        return total;

    }
    public int logOut() {
        int result = 0;

        SQLiteDatabase db = getWritableDatabase();

        result = db.delete(USER_TABLE, null, null);

        if (db != null)
            db.close();

        return result;
    }

and this in login Activity

@Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.names().get(0).equals("user")||jsonObject.names().get(0).equals("position")) {
                                    Toast.makeText(getApplicationContext(), "Welcome "+jsonObject.getString("position"), Toast.LENGTH_SHORT).show();
                                    user_name=jsonObject.getString("user");
                                    user_position=jsonObject.getString("position");
                                    if (user_name!=null||user_position!=null) {
                                        helper.logIn(user_name, user_position);

                                        finish();
                                        Intent intent = new Intent(getApplicationContext(), MainPage.class);
                                        intent.putExtra("user", user_name);
                                        intent.putExtra("position", user_position);

                                        startActivity(intent);
                                    }

                                } else {
                                    sign_in.setError("");
                                    Toast.makeText(getApplicationContext(), "Error" + jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                                    creatingAnime();
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }

I don't know what is the problem

Aucun commentaire:

Enregistrer un commentaire