1

Hi I am trying to insert two different values name and comment in my table but I am getting an error. I have a button and when it is clicked I want the values to be stored in the database,I would like to mention that I am quite new to sql and I am just experimenting. Is there any possible way to insert 2 values in 2 different rows ? if so how ? Here is my try. many thanks.

   public void onClick(View v)
    {
   mydb.execSQL("insert into test (name) values(?);",new String[]             
   {name.getText().toString()});
   mydb.execSQL("insert into test (comment) values(?);",new String[] 
   {comment.getText().toString()});
   Toast.makeText(getApplicationContext(), "DATA INSERTED", 3000).show();

   }
   });

this is my create table statement.

mydb.execSQL("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT,name     
varchar,comment varchar);");

This seems that is not working though.Any advice guys ?

2
  • Do you mean to insert 2 values in 2 COLUMNS?! If so, you can (and should) do it with only 1 INSERT command Commented Mar 30, 2014 at 13:39
  • yes. sorry for the mistake. Commented Mar 30, 2014 at 13:40

1 Answer 1

1

To insert your values:

mydb.execSQL
(
    "INSERT INTO test (name, comment) VALUES (?, ?);",
    new String[]             
    {
        name.getText().toString(),
        comment.getText().toString()
    }
);

But:

  1. Make sure that mydb is open before executing any query or commands on it.
  2. Make sure that your EditTexts are declared and assigned.
Sign up to request clarification or add additional context in comments.

3 Comments

it seems that this way is working but I am getting another error saying that my table has no column named comment. Am i doing something wrong ? thank you sir.I will update my code with the create table statement.
no, that seems it didnt work. I still get the same error Sir.
Can you post the logcat, to show us the error? add it to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.