2016-02-27 49 views
0

我試圖創建SQLiteDatabase,該功能保持用戶登錄時表不爲空,如果它爲空活動將請求用戶和密碼,並且該表有兩行先是名字和第二個是位置。但我不知道爲什麼給我這個錯誤:E/SQLiteDatabase:插入錯誤

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 (?,?) 

我在數據庫中創建

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

此表,並在此登錄活動

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


         } 

我不知道有什麼問題

回答

0

請嘗試使用以下查詢:

"CREATE TABLE " + USER_TABLE + "(" + USER + " TEXT, " + POSITION + " TEXT)"; 

好像有修正需要在桌子上創建。這裏看看create table Syntax。 link

不要忘記在進行上述更改和測試之前卸載以前的應用程序。

編碼快樂......

+0

感謝親愛的,但問題不是在代碼中,我不知道爲什麼,但是當我做卸載它再工作了,謝謝。 –

0

看來你有一些錯誤,在創建腳本db.execSQL("CREATE TABLE '" + USER_TABLE + "'('" + USER + "'TEXT,'" + POSITION + "'TEXT)");

嘗試類型像之前加空格:

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

這樣做:

1º在值SQLiteDatabase db下添加;

public void onCreate(SQLiteDatabase db) { 
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + USER_TABLE + "(" 

       + USER + " TEXT," 
       + POSITION + " TEXT" + ")"; 
     db.execSQL(CREATE_CONTACTS_TABLE); 
    }