我试图将某些内容添加到列表中,同时将其添加到数据库中。这可以通过按下按钮来完成。当我将项目添加到列表时,这工作正常,但是当我添加数据库功能时,它停止工作。错误始于:
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&#34; Table&#34 ;:语法错误(代码1):,同时编译:CREATE TABLE Table(id INTEGER PRIMARY KEY,name TEXT) 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) 在android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677) 在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608) 在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) 在com.segunfamisa.sample.bottomnav.ItemController.addItemToDB(ItemController.java:15) 在com.segunfamisa.sample.bottomnav.FragmentMyList $ 1.onClick(FragmentMyList.java:52) 在android.view.View.performClick(View.java:5637) 在android.view.View $ PerformClick.run(View.java:22429) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
Table是SQLLite中的保留关键字,不能将其用作表名。 http://www.sqlite.org/lang_keywords.html
还要注意列名TEXT。这不是在SQLLite中保留的,而是在MySQL中,例如它。使用此列名可防止您以后切换到其他数据库供应商。
答案 1 :(得分:1)
为表格尝试其他名称,因为它是关键字,并在此处使用表格名称\&#39;或\&#34;表名在这里\&#34;
答案 2 :(得分:0)
我猜你必须将表的表名放入某种逃避字符中(google说它的“ - 双引号”)
ItemDAO中的:
private static final String TABLEITEM = "\"Table\"";
表是SQLLite中的保留关键字,因此通常错误(练习)想法将保留关键字用作表和列名称。但是,如果必须,您必须逃避这些名称:reference