如何在 Android SQlite 中添加布尔列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24604817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:28:43  来源:igfitidea点击:

How to Add a Boolean Column in Android SQlite

androidsqliteandroid-sqlite

提问by BST Kaal

I have created a table for my ContentProviderusing the following line :

ContentProvider使用以下行为我创建了一个表 :

static final String CREATE_DB_TABLE = 
  " CREATE TABLE " + CONTACTS_TABLE_NAME +
  " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
  " pid TEXT NOT NULL, " +
  " name TEXT NOT NULL,"+
  "number TEXT NOT NULL);";

It has 4 columns. Now i want to add a column with a boolean value of true/false. How can i add append/change this statement if i have to add a booleancolumn named "status".

它有 4 列。现在我想添加一个布尔值为真/假的列。如果我必须添加一个名为"status"布尔列,我如何添加附加/更改此语句。

回答by Phant?maxx

You could use something like this:

你可以使用这样的东西:

Create your table:

创建你的表:

static final String CREATE_DB_TABLE = 
    "CREATE TABLE " + CONTACTS_TABLE_NAME " + 
    " (_id INTEGER PRIMARY KEY AUTOINCREMENT," + 
    "..." + " flag INTEGER DEFAULT 0)";

retrieve your value as:

检索您的价值为:

Boolean flag = (cursor.getInt(cursor.getColumnIndex("flag")) == 1);

回答by My God

As mentioned by M D,

正如 MD 所说,

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

SQLite 没有单独的布尔存储类。相反,布尔值存储为整数 0 (false) 和 1 (true)。

Use the following ALTER statement -

使用以下 ALTER 语句 -

ALTER TABLE CREATE_DB_TABLE ADD status boolean NOT NULL default 0;

Then you can use Cursor to get the required value and then convert to actual boolean -

然后您可以使用 Cursor 获取所需的值,然后转换为实际的布尔值 -

flag=cursor.getString(0).equals("1")

You can use this flag to display on user screen.

您可以使用此标志在用户屏幕上显示。

Reference: http://www.sqlite.org/datatype3.html

参考:http: //www.sqlite.org/datatype3.html

回答by Santhosh

public enum Status{
        TRUE,FALSE;
}
static final String CREATE_DB_TABLE = 
  " CREATE TABLE " + CONTACTS_TABLE_NAME +
  " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +......
  " booleanColumn INTEGER DEFAULT 0);";

//while inserting

pass Status.TRUE.ordinal() values to database and

//while retrieving

cross check with the ordinal value for status

//插入时

将 Status.TRUE.ordinal() 值传递给数据库并

//检索时

与状态的序号值进行交叉检查

回答by Pankaj Arora

Sqlite does not provide any class to store boolean values.

Sqlite 不提供任何类来存储布尔值。

You can use 0for falseand 1for true. You will then have to convert these values after retrieving them from your database.

您可以使用0forfalse1for true。然后,您必须在从数据库中检索这些值后对其进行转换。

回答by Abolfazl Miadian

unfortunately android sqlite does not support Boolean type and you should use Integer instead of it

不幸的是,android sqlite 不支持 Boolean 类型,您应该使用 Integer 而不是它