在不SQLite和错误的NULL列捕 [英] NOT NULL columns in SQLite and error catching

查看:244
本文介绍了在不SQLite和错误的NULL列捕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在由用户定义EditTexts填充数据库。编辑文本无应允许空字段。我知道我可以检查此一对夫妇简单的if语句:如果myEditText.getText()的toString()等于()//显示误差。不过,我会perfer利用这个机会刷上我的SQLite和错误捕获(在我的add方法证明)。 我怎么会去当一个用户试图用空字段添加/更新改变列在下面的表格, NOT NULL 和生成/捕获错误?

I have a database that is being filled by user defined EditTexts. None of the edit texts should allow empty fields. I know that I can check for this with a couple simple if-statements: if myEditText.getText().toString().equals("") // display error. However I would perfer to use this opportunity to brush up on my SQLite and error catching (as demonstrated in my add method). How would I go about altering the columns in the table below to NOT NULL and generating/catching an error when a user attempts to add/update with empty fields?

我的数据库表:

db.execSQL(CREATE TABLE库存(类别TEXT,itemNum TEXT,INTEGER数量,价格REAL,INTEGER形象,UNIQUE(类别,itemNum)关于冲突FAIL););

我的加入方法:

...填写 ContentValues​​值

try{
db.getWritableDatabase().insertWithOnConflict(DatabaseHelper.TABLE_NAME, DatabaseHelper.CATEGORY, values, SQLiteDatabase.CONFLICT_FAIL);
fillItemNumbers();          
}
catch(SQLiteConstraintException e)
{
Toast
.makeText(MyActivity.this, etItemNum.getText().toString() + " already exists in " +     catSpinner.getSelectedItem().toString() +". Consider using Update.",Toast.LENGTH_LONG)
.show();            
}

我的更新方法:

...填写 ContentValues​​值

String[] args = {catSpinner.getSelectedItem().toString(), etItemNum.getText().toString()};
int rowsAffected = db.getWritableDatabase().update(DatabaseHelper.TABLE_NAME, values, DatabaseHelper.CATEGORY + "=? AND " + DatabaseHelper.ITEM_NUMBER + "=?" , args);

更新:
我做了一个小挖以及与此想出了:

UPDATE: I did a little digging and came up with this:

db.execSQL(CREATE TABLE库存(类文本NOT NULL,itemNum文本NOT NULL,数量INTEGER NOT NULL,价格REAL NOT NULL,图像INTEGER NOT NULL,UNIQUE(类别,itemNum)ON冲突FAIL););

这是我所期待的?如果是这样,我怎么可以用它来我的优势(见上文)?

Is this what I am looking for? If so, how can I use this to my advantage (see above)?

推荐答案

我不知道你是否能真正改变列定义的表。我知道你可以改变表本身,比如增加新列表。您可能需要小动作来修改你的数据库,如果有很多要preserve在它的数据。

I am not sure if you can actually Alter Column Definition for table. I know you can Alter Table itself, like adding new Column to Table. You might need little trick to modify your database if there is lot of data in it that you want to preserve.

一种方法是创建新表,并尝试将数据复制到新表,后来删除旧表和重命名新表。这不是最有效的方式做到这一点,但它会得到这份工作虽然完成。

One way to it to create new table and try copying data to new table and afterwards remove old table and rename new Table. It's not most efficient way to do it but it'll get the job done though.

http://www.sqlite.org/lang_altertable.html

修改

在这里你去

CREATE TABLE inventory (category TEXT not null, itemNum TEXT not null, quantity INTEGER not null, price REAL not null, image INTEGER not null, UNIQUE(category, itemNum) ON CONFLICT FAIL);

编辑2
试试这个

CREATE TABLE inventory (category TEXT not null ON CONFLICT FAIL, itemNum TEXT not null ON CONFLICT FAIL, quantity INTEGER not null ON CONFLICT FAIL, price REAL not null ON CONFLICT FAIL, image INTEGER not null ON CONFLICT FAIL, UNIQUE(category, itemNum) ON CONFLICT FAIL);

这篇关于在不SQLite和错误的NULL列捕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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