SQLite无法使用某些表名吗?

时间:2018-10-23 16:26:44

标签: android sqlite

我正在学习使用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语句。

2 个答案:

答案 0 :(得分:0)

您的意思是列名而不是表名。
可能是当您第一次创建数据库时,列名NAMEABC到现在想通过简单地更改{{1 }}。
好吧,这不是那么容易。
从要对其进行测试的仿真器/设备中卸载您的应用程序,以便删除数据库,然后重新运行修改后的代码。这样,数据库将使用新名称重新创建表。
您会看到TITLE的{​​{1}}方法不是在每次运行应用程序时触发,只有在没有数据库或数据库版本已升级的情况下才会触发。

答案 1 :(得分:0)

检查此线程:SQL As Understood By SQLite

SQL标准指定了很多关键字,这些关键字不能用作表,索引,列,数据库,用户定义的函数,归类,虚拟表模块或任何其他命名对象的名称。关键字列表如此之长,以至于很少有人能记住它们。对于大多数SQL代码,最安全的选择是永远不要使用任何英语单词作为用户定义对象的名称。

这是整个列表:

  • 中止
  • 动作
  • 添加
  • 之后
  • 全部
  • ALTER
  • 分析
  • AND
  • AS
  • ASC
  • ATTACH
  • 自动增加
  • 之前
  • 开始
  • BETWEEN
  • BY
  • CASCADE
  • 案例
  • CAST
  • 检查
  • 收集
  • 提交
  • CONFLICT
  • 约束
  • 创建
  • 交叉
  • 当前
  • CURRENT_DATE
  • CURRENT_TIME
  • CURRENT_TIMESTAMP
  • 数据库
  • 默认
  • 可延缓
  • 推迟
  • 删除
  • DESC
  • DETACH
  • DISTINCT
  • DO
  • DROP
  • 每个
  • ELSE
  • END
  • ESCAPE
  • 独家
  • 存在
  • 解释
  • 失败
  • 过滤器
  • 关注
  • FOR
  • 外国
  • GLOB
  • 拥有
  • IF
  • IGNORE
  • 立即
  • IN
  • INDEX
  • INDEXED
  • 初始化
  • 内部
  • 插入
  • INSTEAD
  • 相交
  • INTO
  • IS
  • ISNULL
  • 加入
  • KEY
  • 喜欢
  • LIMIT
  • MATCH
  • 自然
  • 没事
  • NOTNULL
  • NULL
  • OF
  • 偏移量
  • 开启
  • OR
  • 订购
  • 外部
  • PARTITION
  • 计划
  • PRAGMA
  • 正在准备
  • PRIMARY
  • 查询
  • 升起
  • 范围
  • 递归
  • 参考
  • REGEXP
  • REINDEX
  • 发布
  • 重命名
  • 替换
  • 限制
  • RIGHT
  • 回滚
  • ROW
  • ROWS
  • 保存点
  • 选择
  • 设置
  • TEMP
  • 临时
  • 之后
  • TO
  • 交易
  • TRIGGER
  • 未绑定
  • UNION
  • 独特
  • 更新
  • 使用
  • 真空
  • VALUES
  • 查看
  • 虚拟
  • 何时
  • 在哪里
  • WINDOW
  • 没有