C.Junior
C.Junior

Reputation: 97

SQLite cant insert in to table

My Error

https://i.sstatic.net/sWcfc.png

08-01 10:30:37.922 10238-10238/com.example.androiddev.army31 E/SQLiteLog: (1) table user has no column named email 08-01 10:30:37.923 10238-10238/com.example.androiddev.army31 E/SQLiteDatabase: Error inserting mobile=4321 name=4321 lastname=4321 email=597ff5de5808e6.61338449 created_at=4321 uid=4321 id_card=2017-08-01 10:30:38 users_name=4321

android.database.sqlite.SQLiteException: table user has no column named email (code 1): , while compiling: INSERT INTO user(mobile,name,lastname,email,created_at,uid,id_card,users_name) VALUES (?,?,?,?,?,?,?,?)

                                                                               at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                               at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                               at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                               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:1472)
                                                                               at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
                                                                               at com.example.androiddev.army31.helper.SQLiteHandler.addUser(SQLiteHandler.java:89)
                                                                               at com.example.androiddev.army31.LoginScreen.Sign_up$2.onResponse(Sign_up.java:152)
                                                                               at com.example.androiddev.army31.LoginScreen.Sign_up$2.onResponse(Sign_up.java:126)
                                                                               at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                               at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                               at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

My SQLite Code i cant find the problem on this

I'M try to many time change any value and got the same error

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USERS + "("
            + KEY_USERS_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_LASTNAME + " TEXT ,"
            + KEY_ID_CARD + " TEXT ," + KEY_MOBILE + " TEXT ,"
            + KEY_USERS_NAME + " TEXT UNIQUE," + KEY_UID + " TEXT," + KEY_EMAIL + "TEXT "
            + KEY_CREATED_AT + " TEXT" + ")";

    db.execSQL(CREATE_LOGIN_TABLE);

    Log.d(TAG, "Database tables created");
}

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

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String name, String lastname , String users_name
                    ,String email , String id_card , String mobile , String uid, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    values.put(KEY_LASTNAME , lastname); //LastName
    values.put(KEY_USERS_NAME, users_name);
    values.put(KEY_EMAIL , email);
    values.put(KEY_ID_CARD , id_card);
    values.put(KEY_MOBILE , mobile);
    values.put(KEY_UID, uid); // Email
    values.put(KEY_CREATED_AT, created_at); // Created At

    // Inserting Row
    long id = db.insert(TABLE_USERS, null, values);
    db.close(); // Closing database connection

    Log.d(TAG, "New user inserted into sqlite: " + id);
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_USERS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("lastname" , cursor.getString(2));
        user.put("users_name", cursor.getString(3));
        user.put("email" , cursor.getString(4));
        user.put("id_card" , cursor.getString(5));
        user.put("mobile" , cursor.getString(6));
        user.put("uid", cursor.getString(7));
        user.put("created_at", cursor.getString(8));
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}

Here from my main code to implement SQLite to store from register form

and check to the login scree

    // Session manager
    session = new SessionManager(getApplicationContext());

    // SQLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // Check if user is already logged in or not
    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(Sign_up.this,
                ReportActivity.class);
        startActivity(intent);
        finish();
    }
    // Progress dialog
    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);

    mContext = this;

    // Register Button Click event
    btnSignUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String name = etFname.getText().toString().trim();
            String lastname = etLname.getText().toString().trim();
            String users_name = etUser.getText().toString().trim();
            String password = etPassword.getText().toString().trim();
            String id_card = etIdCard.getText().toString().trim();
            String mobile = etMobile.getText().toString().trim();
            String email = etEmail.getText().toString().trim();

            if (!name.isEmpty() && !lastname.isEmpty() && !users_name.isEmpty() && !password.isEmpty() &&!email.isEmpty()
                    &&!id_card.isEmpty() &&!mobile.isEmpty() ) {
                registerUser(name, lastname , users_name, email, id_card , password , mobile);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

}

private void registerUser(final String name, final String lastname ,final String users_name,
                          final String email , final  String id_card ,final String mobile ,final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

    progressDialog.setMessage("Registering ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_REGISTER, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    // User successfully stored in MySQL
                    // Now store the user in sqlite
                    String uid = jObj.getString("uid");

                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String lastname = user.getString("lastname");
                    String users_name = user.getString("users_name");
                    String created_at = user
                            .getString("created_at");
                    String email = user.getString("email");
                    String id_card = user.getString("id_card");
                    String mobile = user.getString("mobile");

                    db.addUser(name, lastname ,  users_name, uid, created_at , email , id_card ,mobile);

Upvotes: 1

Views: 244

Answers (2)

rockstar
rockstar

Reputation: 687

You have got Error "table user has no column named email" that means problem in column created. So, Please make sure following steps:

  1. change ur create query like @Avi Answer

    like

    @Override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USERS + "("
            + KEY_USERS_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_LASTNAME + " TEXT ,"
            + KEY_ID_CARD + " TEXT ," + KEY_MOBILE + " TEXT ,"
            + KEY_USERS_NAME + " TEXT UNIQUE," + KEY_UID + " TEXT," + KEY_EMAIL + "TEXT "
            + KEY_CREATED_AT + " TEXT" + ")";
    
    db.execSQL(CREATE_LOGIN_TABLE);
    
    Log.d(TAG, "Database tables created");
    

    }

here u r missing one comma and one space before text datatype (near by KEY_EMAIL)

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USERS + "("
        + KEY_USERS_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_LASTNAME + " TEXT ,"
        + KEY_ID_CARD + " TEXT ," + KEY_MOBILE + " TEXT ,"
        + KEY_USERS_NAME + " TEXT UNIQUE," + KEY_UID + " TEXT," + KEY_EMAIL + " TEXT ,"
        + KEY_CREATED_AT + " TEXT" + ")";
  1. uninstall app and install again after changing this query.

I hope this will helpful for u.

thanks

Upvotes: 3

VikasGoyal
VikasGoyal

Reputation: 3376

There is problem with your CREATE_LOGIN_TABLE query please find the correct one:-

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE "
            + TABLE_USERS + "("
            + KEY_USERS_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_LASTNAME + " TEXT ,"
            + KEY_ID_CARD + " TEXT ,"
            + KEY_MOBILE + " TEXT ,"
            + KEY_USERS_NAME + " TEXT UNIQUE,"
            + KEY_UID + " TEXT,"
            + KEY_EMAIL + " TEXT, "
            + KEY_CREATED_AT + " TEXT" + ")";

    db.execSQL(CREATE_LOGIN_TABLE);

    Log.d(TAG, "Database tables created");
}

Also now you need to make sure your onUpgrade method will get called this time, so try the new code with updated DB version or uninstall and install the app again.

Upvotes: 1

Related Questions