SQLite数据库允许空值

时间:2013-02-01 13:12:01

标签: android sqlite

我使用以下代码在我的Android应用程序中创建数据库。实际上,需要将数据从另一个表复制到此表。它只复制了10868条记录,但原始表包含24000条记录。

我检查并最终在10869记录中找到了其中一个字段的空值。但是在创建表时我没有指定“not null”。但是这个表不能接受空值。

private static final String SCRIPT_CREATE_DATABASE1 =
          "create table " + MYDATABASE_TABLE2 + " ("
          + KEY_ID + " integer primary key autoincrement, "
          + KEY_RCODE + " text, "
          + KEY_RNAME + " text);";

是否有其他方法允许列接受空值?我真的很困惑

修改:Android代码

try
    {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://moberp.svsugar.com:8080/androidservlt/ryotmas");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
    Toast.makeText(getBaseContext(), "Buffer Error"+"Error converting result"+e.toString(), Toast.LENGTH_LONG).show();
    }

    try {
        jObj = new JSONObject(json);
        contacts = jObj.getJSONArray("get");
        for(int i = 0; i < contacts.length(); i++){
        JSONObject c = contacts.getJSONObject(i);
            rcode = c.getString("rcode");
            rname = c.getString("rname");
            radapter.insert(rcode, rname);
        }
        Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show();

    } catch (JSONException e) {
        Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show();
    }

1 个答案:

答案 0 :(得分:1)

您没有检查从JSON对象重新获取的值是否实际具有值。

如果他们没有可以转换为您正在重新搜索的类型的值,则会抛出JSONException。

您可以使用IsNUll

轻松完成此项检查
JSONObject c = contacts.getJSONObject(i);
            rcode = c.getString("rcode");

            if(!c.isNull("rname"))
            {
              rname = c.getString("rname");
            }
            else
            {
              rname = ""; //or rname = null, whatever you want
            }
            radapter.insert(rcode, rname);
        }