2017-03-01 97 views
-2

我試圖添加一些東西到一個列表中,並同時將它添加到數據庫。這是通過按下按鈕完成的。當我只是將項目添加到列表中時,這工作得很好,但是當我添加數據庫功能時,它停止工作。錯誤始於:通過按下按鈕將項目添加到SQL數據庫

db.execSQL(createTable);

public class ItemDAO extends SQLiteOpenHelper { 

     private static final String DATABASENAME = "ItemsDB"; 
     private static final Integer DATABASEVERSION = 1; 

     private static final String TABLEITEM = "Table"; 
     private static final String ITEM = "name"; 
     private static final String ID = "id"; 

     private Context context; 

     public ItemDAO(Context context) { 
      super(context, DATABASENAME, null, DATABASEVERSION); 
      this.context = context; 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 

      String createTable = "CREATE TABLE " + TABLEITEM + "(" 
        + ID + " INTEGER PRIMARY KEY, " 
        + ITEM + " TEXT" + ")"; 

      db.execSQL(createTable); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     } 

     public void addItemToDatabase(Item item){ 

      ContentValues fila = new ContentValues(); 

      fila.put(ID, item.getId()); 
      fila.put(ITEM, item.getName()); 

      SQLiteDatabase database = getWritableDatabase(); 

      database.insert(TABLEITEM, null, fila); 

      database.close(); 
     } 

     public List<Item> getItemsFromDatabases(){ 

      String selectQuery = "SELECT * FROM " + TABLEITEM; 

      SQLiteDatabase database = getReadableDatabase(); 

      Cursor cursor = database.rawQuery(selectQuery, null); 
      List<Item> itemList = new ArrayList<>(); 

      while(cursor.moveToNext()){ 

       Item item = getItem(cursor); 
       itemList.add(item); 
      } 

      cursor.close(); 
      database.close(); 
      return itemList; 
     } 


     private Item getItem(Cursor cursor){ 
      Item item = new Item(); 

      Integer itemID = cursor.getInt(cursor.getColumnIndex(ID)); 
      item.setId(itemID); 

      String itemName = cursor.getString(cursor.getColumnIndex(ITEM)); 
      item.setName(itemName); 
      return item; 
     } 
    } 

//另一類

public class ItemController { 

    public void addItemToDB(Item item, Context context){ 
     ItemDAO itemDAO = new ItemDAO(context); 
     itemDAO.addItemToDatabase(item); 
    } 

    public List<Item> getItemsFromDB(Context context){ 
     ItemDAO itemDAO = new ItemDAO(context); 
     return itemDAO.getItemsFromDatabases(); 
    } 

} 

//另一類

public class FragmentMyList extends Fragment { 
    private EditText editTxt; 
    private Button btn; 
    private ListView list; 
    private ArrayAdapter<String> adapter; 
    private ArrayList<String> arrayList; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View vistaADevolver = inflater.inflate(R.layout.fragment_my_list, container, false); 

     editTxt = (EditText)vistaADevolver.findViewById(R.id.editText); 
     btn = (Button) vistaADevolver.findViewById(R.id.button); 
     list = (ListView) vistaADevolver.findViewById(R.id.recyclerView); 
     arrayList = new ArrayList<>(); 

     // Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown), 
     // and the array that contains the data 
     adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, arrayList); 

     // Here, you set the data in your ListView 
     list.setAdapter(adapter); 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ItemController itemController = new ItemController(); 

       String itemName = editTxt.getText().toString(); 
       Item item = new Item(); 
       item.setName(itemName); 
       item.setId(0); 
       itemController.addItemToDB(item, getActivity()); 

       // this line adds the data of your EditText and puts in your array 
       arrayList.add(itemName); 
       // next thing you have to do is check if your adapter has changed 
       adapter.notifyDataSetChanged(); 
      } 
     }); 
     return vistaADevolver; 
    } 
} 

E/AndroidRuntime:致命異常:主 工藝:com.segunfamisa.sample.bottomnav,PID: 2792 android.database.sqlite.SQLiteException:near「Table」:syntax error(code 1):,while compiling:CREATE TABLE Table(id INTEGER PRIMARY KEY,name TEXT)在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)上的android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) ) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement。(SQLiteStatement.java :31) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608) at com.segunfamisa.sample.bottomnav.ItemDAO .onCreate( ItemDAO.java:45) 在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) 在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 在com.segunfamisa.sample。 bottomnav.ItemDAO.addItemToDatabase(ItemDAO.java:63) at com.segunfamisa.sample.bottomnav.ItemController.addItemToDB(ItemController.java:15) at com.segunfamisa.sample.bottomnav.FragmentMyList $ 1.onClick(FragmentMyList.java :52) at android.view.View.performClick(View.java:5637) at android.view.View $ PerformClick.run(View.java:22429) 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:6119) at java.lang.reflect.Method.invoke(Native Method) at com .android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

+0

我覺得如果你添加的錯誤信息,這將有助於... –

+0

有是錯誤 –

+0

您是否已經擁有一個名爲'Table'的表格,但您尚未清除? – UnstableEagle

回答

1

表是在一個SQLLite保留關鍵字,你不能用它作爲表名。 http://www.sqlite.org/lang_keywords.html

還要注意列名TEXT。這不是在SQLLite中保留的,但在MySQL中是這樣的。使用此列名稱可防止您將來切換到另一個數據庫供應商。

+0

這很有效。謝謝! –

+0

@MatiasNagore不要!樂於幫助! – user2323354

0

我猜你必須把表是變成某種逃避字符的域名(谷歌表示,它「 - 雙引號)

在ItemDAO

private static final String TABLEITEM = "\"Table\""; 

表是在SQLLite中保留關鍵字通常是不好(練習)想法使用保留關鍵字作爲表和列的名稱。但是如果你必須要避開這些名字:reference

+0

我不確定這是否正常工作,我沒有測試過,但它是可能的。但請不要這樣做,使用Table作爲表名是一個壞習慣。 – user2323354

1

嘗試對錶中的一些其他的名字,因爲它是一個關鍵字,並使用\「表名在這裏\」或\「表名稱這裏\」

+0

工作。謝謝! –