我正在学习使用Java制作Android应用程序,所以很抱歉,如果这个问题以noob出现。
因此,我制作了一个ListView并将一个SQLite数据库链接到它。
打开应用程序时,将加载列表视图。
现在,如果我的表名是TITLE,LOCATION和DESCRIPTION,则所有启动时崩溃都表示TITLE和LOCATION表不存在。
但是,如果我的表名是NAME,ABC和DESCRIPTION,即使这些表不存在,应用也会加载。
SQLiteDatabaseHandler.java:
public class SQLiteDatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "remindersdb";
private static final String TABLE_NAME = "reminders";
private static final String KEY_ID = "ID";
private static final String KEY_NAME = "TITLE";
private static final String KEY_LOC = "LOCATION";
private static final String KEY_DESCRIPTION = "DESCRIPTION";
private static final String[] COLUMNS = {KEY_ID, KEY_NAME, KEY_LOC, KEY_DESCRIPTION};
public SQLiteDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,ABC TEXT,DESCRIPTION TEXT)");
db.execSQL("create table reminders (ID INTEGER PRIMARY KEY AUTOINCREMENT, TITLE TEXT, LOCATION TEXT, DESCRIPTION TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(String name, String location1, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cValues = new ContentValues();
cValues.put(KEY_NAME, name);
cValues.put(KEY_LOC, location1);
cValues.put(KEY_DESCRIPTION, description);
long newRowId = db.insert(TABLE_NAME, null, cValues);
db.close();
/*SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME,name);
contentValues.put(KEY_POSITION,position);
contentValues.put(KEY_DESCRIPTION,description);
long result = db.insert(TABLE_NAME,null ,contentValues);
if(result == -1)
return false;
else
return true;*/
}
public boolean updateData(String id, String name, String location1, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, id);
contentValues.put(KEY_NAME, name);
contentValues.put(KEY_LOC, location1);
contentValues.put(KEY_DESCRIPTION, description);
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{id});
return true;
}
public ArrayList<HashMap<String, String>> getData() {
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<HashMap<String, String>> userList = new ArrayList<>();
String query = "SELECT TITLE, LOCATION, DESCRIPTION FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()) {
HashMap<String, String> user = new HashMap<>();
user.put("name", cursor.getString(cursor.getColumnIndex(KEY_NAME)));
user.put("location1", cursor.getString(cursor.getColumnIndex(KEY_LOC)));
userList.add(user);
}
return userList;
}
/*public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}*/
public void deleteData(String id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + " = ?", new String[]{String.valueOf(id)});
db.close();
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabaseHandler db = new SQLiteDatabaseHandler(this);
ArrayList<HashMap<String, String>> userList = db.getData();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.content_main, R.id.textview1 , mobileArray);
ListView lv = (ListView) findViewById(R.id.listView1);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, userList, R.layout.list_row,new String[]{"TITLE","LOCATION","DESCRIPTION"}, new int[]{R.id.name, R.id.designation, R.id.location});lv.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
Intent intent = new Intent(MainActivity.this, CreateActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
由于格式问题,我删除了一些基本代码,例如import语句。
答案 0 :(得分:0)
您的意思是列名而不是表名。
可能是当您第一次创建数据库时,列名NAME
,ABC
到现在想通过简单地更改{{1 }}。
好吧,这不是那么容易。
从要对其进行测试的仿真器/设备中卸载您的应用程序,以便删除数据库,然后重新运行修改后的代码。这样,数据库将使用新名称重新创建表。
您会看到TITLE
的{{1}}方法不是在每次运行应用程序时触发,只有在没有数据库或数据库版本已升级的情况下才会触发。>
答案 1 :(得分:0)
检查此线程:SQL As Understood By SQLite
SQL标准指定了很多关键字,这些关键字不能用作表,索引,列,数据库,用户定义的函数,归类,虚拟表模块或任何其他命名对象的名称。关键字列表如此之长,以至于很少有人能记住它们。对于大多数SQL代码,最安全的选择是永远不要使用任何英语单词作为用户定义对象的名称。
这是整个列表: