sqlite错误SQLiteException:表用户没有名为

时间:2017-09-21 11:14:34

标签: android sqlite android-sqlite

在将数据插入sqlite时出错是堆栈跟踪

E/SQLiteDatabase: Error inserting addhar_number=test profile_pic=null token=null name=Nikhil Patil email=niks34547@gmail.com 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)

这是我的sqlite处理程序类

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);
}

}

在我的活动中,我称之为

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

我确实更改了版本号,数据库名称但没有帮助,因为我想将用户详细信息用于进一步使用bt无法将数据插入数据库。请帮助我,这对我来说很重要。

1 个答案:

答案 0 :(得分:4)

CREATE TABLE SQL中,在addhar_number等列名称和TEXT等类型之间添加空格。

卸载您的应用,以便再次调用onCreate()