如果存在sqlite,则重命名列?

编程入门 行业动态 更新时间:2024-10-25 09:37:50
本文介绍了如果存在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 correct and 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 Column Something like this

Alter Table MyTable RENAME COLUMN IF EXISTS MyColumn TO MyColumn1;

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

Existing database is at user end used by user's throw out INDIA please 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.

该过程将遵循:

  • 开始交易;
  • ALTER TABLE table RENAME TO tmp_table;
  • CREATE TABLE table (columnNames);
  • INSERT INTO table(columnNames) SELECT columnNamesWrong FROM tmp_table;
  • 删除表 tmp_table_name;
  • 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 supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.

    注意什么是不可能的.

    更多推荐

    如果存在sqlite,则重命名列?

    本文发布于:2023-10-16 16:03:17,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1498055.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重命名   sqlite

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!