2015-08-09 3 views
1

Я пытаюсь найти ошибку в течение довольно долгого времени и буду признателен за любую помощь.Ошибка синтаксиса SQLiteException

Я использую логин и регистрацию для android, и я использую PHP, MySQL и SQLite. В приведенном ниже коде я пытаюсь добавить информацию в базу данных 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 = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "android_api"; 

    // Login table name 
    private static final String TABLE_LOGIN = "login"; 

    // Login 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_GROUP = "group"; 
    private static final String KEY_UID = "uid"; 
    private static final String KEY_CREATED_AT = "created_at"; 

    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_LOGIN + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," 
       + KEY_EMAIL + " TEXT UNIQUE," + KEY_GROUP + " TEXT," + 
       KEY_UID + " 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_LOGIN); 

     // Create tables again 
     onCreate(db); 
    } 

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

     ContentValues values = new ContentValues(); 
     values.put(KEY_NAME, name); // Name 
     values.put(KEY_EMAIL, email); // Email 
     values.put(KEY_GROUP, group); 
     System.out.println("The group is: " + group); 
     values.put(KEY_UID, uid); 
     values.put(KEY_CREATED_AT, created_at); // Created At 

     // Inserting Row 
     long id = db.insert(TABLE_LOGIN, 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_LOGIN; 

     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("email", cursor.getString(2)); 
      user.put("group", cursor.getString(3)); 
      user.put("uid", cursor.getString(4)); 
      user.put("created_at", cursor.getString(5)); 
     } 
     cursor.close(); 
     db.close(); 
     // return user 
     Log.d(TAG, "Fetching user from Sqlite: " + user.toString()); 

     return user; 
    } 

    /** 
    * Getting user login status return true if rows are there in table 
    * */ 
    public int getRowCount() { 
     String countQuery = "SELECT * FROM " + TABLE_LOGIN; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     int rowCount = cursor.getCount(); 
     db.close(); 
     cursor.close(); 

     // return row count 
     return rowCount; 
    } 

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

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

Ошибка в том, что я не могу вставить в таблицу и консоли говорит:

08-09 14:44:25.863 29539-29539/cindyliu96.test E/SQLiteLog﹕ (1) near "group": syntax error 
08-09 14:44:25.864 29539-29539/cindyliu96.test E/SQLiteDatabase﹕ Error inserting [email protected] name=E created_at=2015-08-09 14:45:35 uid=55c7c9ff16c9c3.21000664 group= 
    android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: INSERT INTO login(email,name,created_at,uid,group) 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:1469) 
      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) 
      at cindyliu96.test.SQLiteHandler.addUser(SQLiteHandler.java:76) 
      at cindyliu96.test.RegisterActivity$3.onResponse(RegisterActivity.java:139) 
      at cindyliu96.test.RegisterActivity$3.onResponse(RegisterActivity.java:115) 
      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:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

ответ

0

group является зарезервированным словом в SQL, так что вы не можете использовать его в качестве имени столбца. Вы можете, например, позвонить в эту колонку user_group вместо:

private static final String KEY_GROUP = "user_group";