本文介绍了如果存在sqlite,则重命名列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用一开始没有使用的列,但现在我们正在设置和获取值

I've created which used a column which were not being used initially but now we are setting and getting values

我发现列名不正确以及我想更改列名称但想保留设备中现有数据库的数据

I found the column name is not correctand what i want to change column Name but want to retain the data of existing database in device

是否有任何查询要检查和重命名 sqlite Column像这样

Is there any query to check and rename sqlite ColumnSomething like this

Alter Table MyTable RENAME COLUMN IF EXISTS MyColumn TO MyColumn1;

现有数据库在用户端被用户抛弃印度使用请帮帮我

Existing database is at user end used by user's throw out INDIAplease help me

我知道两种方式都能完成任务,但这不是最佳做法

I know both way will accomplish the task but that's not the best practice

与其重新创造,不如以可怜的名字生活

Rather than recreating i'll live with poor name

我认为可能有解决方案,因为我们可以像这样检查表格,希望我们可以检查列

I think there could be solution like as we can check table like this hope we could check column

SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;

SELECT name FROM sqlite_master WHERE type='table' AND name = 'MyTable';

推荐答案

据我所知,没有办法按照你的要求去做.但是你可以使用

As far as I know there is no way to do what you ask. However you can use

SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'

检查该列是否存在.但是,因为一旦我不知道重新创建表有什么问题,您就必须重命名该列.娱乐意味着数据丢失.

to check whether the column exists or not. However since you just have to rename the column once I do not know what the issue is with the recreation of the table. Recreation does NOT mean data loss.

该过程将遵循:

  1. 开始交易;
  2. ALTER TABLE table RENAME TO tmp_table;
  3. CREATE TABLE table (columnNames);
  4. INSERT INTO table(columnNames) SELECT columnNamesWrong FROM tmp_table;
  5. 删除表 tmp_table_name;
  6. COMMIT;

如果这太麻烦了,请使用工具来完成.

If this is to much fuss use a tool to do it.

关于最佳实践部分,最佳实践是正确命名您的表.由于您通常围绕字段名称构建所有查询,因此重命名列意味着破坏这些查询.我不知道您在寻找什么,但 sqlite 手册 指出:

About the best practice part, it is best practice to get your tables named properly. Since you usually build all your queries around the field names renaming columns means breaking those queries. I do not know what you are looking for but the sqlite manual states:

SQLite 支持有限的 ALTER TABLE 子集.更改表SQLite 中的命令允许用户重命名表或添加新表列到现有表.无法重命名列,删除列,或者在表中添加或删除约束.

注意什么是不可能的.

这篇关于如果存在sqlite,则重命名列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:58