如何在表格中创建具有浮点值的列?

问题描述 投票:0回答:1

我正在数据库中创建一个表,其中一列需要存储浮点数。但是,当我要求 SQLiteDatabase 创建表时,我不断收到浮点值名称下的错误。

我尝试在“创建表”语句末尾之外的某个位置声明浮动。

db.execSQL("CREATE TABLE " + teams_table_name + " (teamName varchar (50) primary key not null, foreign key (username) references " + users_table_name + " (username), averageBST float(3, 2), foreign key (pkmnOne) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnTwo) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnThree) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFour) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFive) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnSix) references " + pkmn_table_name + " (pkmnName));");

我尝试不声明浮点数中的位数以及小数点后有多少位。

db.execSQL("CREATE TABLE " + teams_table_name + " (teamName varchar (50) primary key not null, foreign key (username) references " + users_table_name + " (username), foreign key (pkmnOne) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnTwo) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnThree) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFour) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFive) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnSix) references " + pkmn_table_name + " (pkmnName), averageBST float);");

我尝试将“averageBST”全部设为小写字母。

db.execSQL("CREATE TABLE " + teams_table_name + " (teamName varchar (50) primary key not null, foreign key (username) references " + users_table_name + " (username), foreign key (pkmnOne) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnTwo) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnThree) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFour) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFive) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnSix) references " + pkmn_table_name + " (pkmnName), averagebasestattotal float);");

我尝试说averageBST不能为空,尽管我一开始就不会让这种情况发生。

db.execSQL("CREATE TABLE " + teams_table_name + " (teamName varchar (50) primary key not null, foreign key (username) references " + users_table_name + " (username), foreign key (pkmnOne) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnTwo) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnThree) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFour) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnFive) references " + pkmn_table_name + " (pkmnName), foreign key (pkmnSix) references " + pkmn_table_name + " (pkmnName), averageBST float not null);");

当我尝试这些事情时,我预计“averageBST”下面的红色波形曲线会消失。从来没有。

java android sqlite floating-point create-table
1个回答
0
投票

根据评论。语法错误:- 抱怨单词 float 它说它不是:-

  1. 列名称列表的右括号,或
  2. averageBST
    列与另一个列名称分隔开的逗号。

解决办法很可能是:-

    db.execSQL("CREATE TABLE " + teams_table_name +
            "(" +
            "   teamName varchar (50) primary key not null, " +
            " averageBST REAL ," +
            /* ORIGINAL COMMENTED OUT
            "foreign key (username) references " + users_table_name + " (username, " +
            "averageBST float(3, 2), " +*.
             */
            /*<<<<<<<<<< EXAMPLE OF CORRECT SYNTAX >>>>>>>>>>*/
            "foreign key (username) references " + pkmn_table_name + "(averageBST) ," +
            "foreign key (pkmnOne) references " + pkmn_table_name + " (pkmnName), " +
            "foreign key (pkmnTwo) references " + pkmn_table_name + " (pkmnName), " +
            "foreign key (pkmnThree) references " + pkmn_table_name + " (pkmnName), " +
            "foreign key (pkmnFour) references " + pkmn_table_name + " (pkmnName), " +
            "foreign key (pkmnFive) references " + pkmn_table_name + " (pkmnName), " +
            "foreign key (pkmnSix) references " + pkmn_table_name + " (pkmnName));");

可以看出,FOREIGN KEY 需要以下语法:-

FK

© www.soinside.com 2019 - 2024. All rights reserved.