如何只复制表结构? [英] How to copy only the table structure?

查看:33
本文介绍了如何只复制表结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个数据库.它最多有 9 个表,将来可能会超过.但是表的结构(列数和列名)对于所有表都是相同的.我知道有一个选项可以复制表的结构并重命名并使用它,但我不知道如何准确地做到这一点.请给我相关的答案,以减少我的代码.

I want to create a database. It has up to 9 tables in future it may exceed. But the structure (number of columns and column name) of table is same for all tables. I know that there is an option to copy the structure of table and rename it and use it but I don't know how to do it exactly. Please give me the relevant answer so that it reduces my code.

推荐答案

您可以通过 PRAGMA table_info ()

例如

String sqlstr = " PRAGMA table_info (" + table_name + ")";
        Cursor csr = db.rawQuery(sqlstr, null);

生成的光标具有带列的行:-

The resultant cusors has rows with columns :-

cid(rowid)

name(列名)

type(列类型,例如 INTEGER)

type (column type e.g. INTEGER)

notnull(如果可以为空则为 0)

notnull (0 if can be null)

dflt_value(如果有默认值)

pk(0 如果不是主键的一部分,否则 1,2...如果是主键的一部分)

pk (0 if not part of the primary key otherwise 1,2... if part of the primary key)

然后您可以从中构建 SQL.

You could then build the SQL from this.

另一种选择是通过以下方式提取用于创建表的SQL:-

Another alternative could be to extract the SQL used to create the table via :-

SELECT sql FROM sqlite_master WHERE type='table' AND name='<yourtable>'

然后您可以更改结果 SQL,将基表名称更改为新表名称.

You could then alter the resultant SQL, changing the base table name to the new table name.

第三个选项是使用类似的东西复制表格

A third option is to copy the table using something like

CREATE TABLE newtable AS SELECT * FROM basetable WHERE 0

然而,这不是结构的完整副本,例如未创建主键.

However, this isn't a full comprehensive copy of the structure, e.g. primary keys aren't created.

这篇关于如何只复制表结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆