Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid HTTP Client "Cannot add or update a child row: a foreign key constraint fails"
    primarykey
    data
    text
    <p>I am creating an android application that will upload some data to a database using the following code found below. I have been able to add other pieces of information to the database (when the api calls for different things), however here I get the following error (code follows). Thank you in advance!</p> <pre><code>05-27 20:35:51.144: E/content(8749): array(1) { [0]=&gt; array(2) { ["value"]=&gt; string(5) "18-24" ["question_id"]=&gt; string(2) "14" }}{"success":0,"error":"Query: INSERT INTO client_question_answers (client_form_id, question_id, value) VALUES ('4', '14', '18-24')\nDatabase error message: Cannot add or update a child row: a foreign key constraint fails (`mypsych`.`client_question_answers`, CONSTRAINT `client_question_answers_ibfk_2` FOREIGN KEY (`client_form_id`) REFERENCES `client_forms` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)\nDatabase error number: 1452"} </code></pre> <p>Make the call here:</p> <pre><code>NetworkUtility.sendForm("4", "14","18-24", key); </code></pre> <p>Here is the sendForm method:</p> <pre><code>public static void sendForm(String client_form_id, String question_id, String value, String key) throws ClientProtocolException, JSONException, IOException { List&lt;BasicNameValuePair&gt; parameters=new ArrayList&lt;BasicNameValuePair&gt;(); JSONObject answers = new JSONObject(); answers.put("question_id", question_id); answers.put("value", value); JSONArray answers1 = new JSONArray(); answers1.put(answers); String answers2 = answers1.toString(); parameters.add(new BasicNameValuePair("answers", answers2)); parameters.add(new BasicNameValuePair("key", key)); StringBuilder url=new StringBuilder(); url.append("https://myurl.com/api/forms/answers/create/"); url.append(client_form_id); JSONObject root = new JSONObject(getJSONContent(parameters, url.toString())); Log.e("goal", root.toString()); } </code></pre> <p>Finally the method that handles the actual HTTP commands</p> <pre><code>private static String getJSONContent(List&lt;BasicNameValuePair&gt; parameters, String url) throws ClientProtocolException, IOException, JSONException { HttpClient client=new DefaultHttpClient(); HttpPost request=new HttpPost(url); if (cookieStore == null) { cookieStore = new BasicCookieStore(); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); } UrlEncodedFormEntity formEntity=new UrlEncodedFormEntity(parameters); request.setEntity(formEntity); HttpResponse response=client.execute(request, httpContext); InputStream is=response.getEntity().getContent(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=null; StringBuilder content=new StringBuilder(); while ((line=br.readLine())!=null) { content.append(line); } Log.e("content", content.toString()); return content.toString(); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload