Reputation: 10555
I use the following code to create database in my android application. Actually, In need to copy data from another table to this table. It only copied 10868 records, but the original table contains 24000 records.
I checked and finally found at the 10869 record, there is a null value on one of the field. But I didn't specify "not null " during table creation. But this table caan't accept null values.
private static final String SCRIPT_CREATE_DATABASE1 =
"create table " + MYDATABASE_TABLE2 + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_RCODE + " text, "
+ KEY_RNAME + " text);";
Is there any other ways to allow columns to accept null values? I really confused
Edit: Android code
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();
}
Upvotes: 0
Views: 4549
Reputation: 8643
You are not checking if the values you are retreiving from the JSON object actually have a value.
If they do not have a value that they can cast to the type you are retreiving, they will throw a JSONException.
You can easily do this check with 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);
}
Upvotes: 1