2

having error while inserting data into sqlite here is stack trace

E/SQLiteDatabase: Error inserting addhar_number=test profile_pic=null token=null name=Nikhil Patil [email protected] phone=  profile_pic_bg=null gender=test birthday=test
                                                                                   android.database.sqlite.SQLiteException: table user has no column named addhar_number (code 1): , while compiling: INSERT INTO user(addhar_number,profile_pic,token,name,email,phone,profile_pic_bg,gender,birthday) 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.wowoni.bikesharing.bicyclesharing.Helper.SQliteHandler.addUser(SQliteHandler.java:88)
                                                                                       at com.wowoni.bikesharing.bicyclesharing.activity.HomeActivity.loadNavHeader(HomeActivity.java:227)
                                                                                       at com.wowoni.bikesharing.bicyclesharing.activity.HomeActivity.onCreate(HomeActivity.java:129)
                                                                                       at android.app.Activity.performCreate(Activity.java:6684)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2652)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2766)
                                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6229)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

here is my sqlite handler class

public class SQliteHandler extends SQLiteOpenHelper {
private static final String TAG = SQliteHandler.class.getSimpleName();

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;
// Database Name
private static final String DATABASE_NAME="bicycle_user";
// Contacts table name
private static final String TABLE_USER="user";

// user Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_PHONE="phone";
private static final String KEY_ADDHAR_NUMBER="addhar_number";
private static final String KEY_GENDER="gender";
private static final String KEY_PROFILE_PIC="profile_pic";
private static final String KEY_PROFILE_PIC_BG="profile_pic_bg";
private static final String KEY_BIRTHDAY="birthday";
private static final String KEY_Token="token";
private static final String KEY_CREATED_AT = "created_at";

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

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_User_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE,"+ KEY_PHONE + "TEXT UNIQUE,"
            + KEY_ADDHAR_NUMBER + "TEXT UNIQUE,"+ KEY_GENDER + "TEXT,"
            + KEY_PROFILE_PIC + "BLOB,"+ KEY_PROFILE_PIC_BG + "BLOB,"
            + KEY_BIRTHDAY + "TEXT," + KEY_Token + "TEXT UNIQUE," + KEY_CREATED_AT + "TIMESTAMP" + ")";
    db.execSQL(CREATE_User_TABLE);

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

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

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String name, String email, String phone, String addhar_number, String gender, byte[] profile_pic, byte[] profile_pic_bg, String birthday, String token)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values= new ContentValues();
    values.put(KEY_NAME,name);
    values.put(KEY_EMAIL,email);
    values.put(KEY_PHONE,phone);
    values.put(KEY_ADDHAR_NUMBER,addhar_number);
    values.put(KEY_GENDER,gender);
    values.put(KEY_PROFILE_PIC,profile_pic);
    values.put(KEY_PROFILE_PIC_BG,profile_pic_bg);
    values.put(KEY_BIRTHDAY,birthday);
    values.put(KEY_Token,token);

    // Inserting Row
    long id = db.insert(TABLE_USER, 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 selectQuery="SELECT * FROM " + TABLE_USER;

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

    cursor.moveToFirst();
    if(cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("email", cursor.getString(2));
        user.put("phone", cursor.getString(3));
        user.put("addhar_number", cursor.getString(4));
        user.put("gender", cursor.getString(5));
        user.put("profile_pic", String.valueOf(cursor.getBlob(6)));
        user.put("profile_pic_bg", String.valueOf(cursor.getBlob(7)));
        user.put("birthday", cursor.getString(8));
        user.put("token", cursor.getString(9));
        user.put("created_at", cursor.getString(10));
    }

    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}
/**
 * Re crate database Delete all tables and create them again
 * */
public void deleteUsers() {
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(TABLE_USER, null, null);
    db.close();

    Log.d(TAG, "Deleted all user info from sqlite");
}

public void updateUser(String name, String email, String phone, String addhar_number, String gender,
                       byte[] profile_pic, byte[] profile_pic_bg, String birthday, String token){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME,name);
    values.put(KEY_EMAIL,email);
    values.put(KEY_PHONE,phone);
    values.put(KEY_ADDHAR_NUMBER,addhar_number);
    values.put(KEY_GENDER,gender);
    values.put(KEY_PROFILE_PIC,profile_pic);
    values.put(KEY_PROFILE_PIC_BG,profile_pic_bg);
    values.put(KEY_BIRTHDAY,birthday);
    values.put(KEY_Token,token);

    int id = db.update(TABLE_USER,values,KEY_EMAIL + " = ?",new String[]{email});
    db.close();
    Log.d(TAG, "Table is updated : " + id);
}

}

and in my activity i am calling it as

db.addUser(name, user_email, phone, addhar_number, gender, profile_pic_array, profile_pic_bg_array, birthday, token);

i did change version number,name of database but not helping,as i want to put user details for further use bt not able to insert the data into database.Please help me as its important for me to get done.

4
  • 1
    Did you delete the app on the device? DB changes will be deployed only if the DB has to be created which is when the app is installed Commented Sep 21, 2017 at 11:17
  • Update your table creation query to(Added AUTOINCREMENT for primary key), String CREATE_User_TABLE = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT UNIQUE,"+ KEY_PHONE + "TEXT UNIQUE," + KEY_ADDHAR_NUMBER + "TEXT UNIQUE,"+ KEY_GENDER + "TEXT," + KEY_PROFILE_PIC + "BLOB,"+ KEY_PROFILE_PIC_BG + "BLOB," + KEY_BIRTHDAY + "TEXT," + KEY_Token + "TEXT UNIQUE," + KEY_CREATED_AT + "TIMESTAMP" + ")"; or pass value of KEY_ID(id) while inserting. Commented Sep 21, 2017 at 11:23
  • @SHIDHIN.T.S i update my table creation query as per your suggestion thanks and please look at my updateuser() method in sqlitehandler class is it right or have any error also please Commented Sep 21, 2017 at 11:34
  • @NikhilPatil You can use, db.update(TABLE_USER, values,KEY_EMAIL +" = ?", new String[] {email}); or db.update(TABLE_USER, values,KEY_EMAIL +" = '"+ email + "'", null); for update Commented Sep 21, 2017 at 11:43

1 Answer 1

11

In your CREATE TABLE SQL, add spaces between column names such as addhar_number and types such as TEXT.

Uninstall your app so that onCreate() is invoked again.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.