1
String ans1= "Apple";

myDb.execSQL("INSERT INTO " +
               TABLE_NAME + " Values (ans1);");

It just doesn't work this way.. why?? I need a simple way to do this, I don't want use class that's confusing for a beginner like me, possible?

6 Answers 6

6

Your insert statement should read something like

"INSERT INTO " + TABLE_NAME + " VALUES ('" + ans1 + "');"

Notice the single quotes that wrap ans1, if you are inserting a string value into an SQL table it should be wrapped in single quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

@Lucifer, Just interested to know why you edited my answer to only remove "Hope that Helps". I am knew to Stack Overflow, and was just been friendly.
sure, there should not be tag lines added, that's why i removed it, nothing else.
4

Try this example :

public long insertValue(String value1, String value2) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_VALUE1, value1);
        initialValues.put(KEY_VALUE2, value2);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

2 Comments

what is the KEY_VALUE there means?
I declare my column on the top ( public static final String KEY_VALUE1 = "value1"; )
0

values should be something like

values("+ans1+");

observe ans1 is inside quote.

Note: raw sql statements are prone to sql injection attacks.

1 Comment

Data security is my kryptonite. When developing web applications it has caused me many sleepless nights. I don't give it much thought when developing a local database such as this. Should I be more concerned about it?
0

To inject the VALUE of variable into a sql statement you need to end your quotes before the variable + the variable + open your quotes again. For a String value you will also need to add single quotes around the variable.

myDb.execSQL("INSERT INTO " +
           TABLE_NAME + " Values ('" + ans1 + "');");

Comments

0

You are inserting variable's value in to table, hence you have to use variable ANS out of the " ( double cots ), like below,

String ans1= "Apple";

myDb.execSQL("INSERT INTO " + TABLE_NAME + " Values ( '" + ans1 + "' );");

It is same as your have not use TABLE_NAME variable inside the ".

Comments

0

For me i wanted to have last inserted id using select from database and then insert new data into database, the select query is used that function

pagesClass.getLastID('users', function(err, id){var id = id}

or you can use direct id which was returned by function

User.createUser = function (result){
var pagesClass = new PagesClass();
pagesClass.getLastID('users', function(err, id){
    conn.query("INSERT INTO users(id, name, email, password) VALUE('"+id+"', 'djsk"+id+"', '[email protected]"+id+"', 'bercove') ", function (err, res){
        if(err)
            result(null, err);
        else
            result(null, res);
    });
});
};

those id into name column and email column was showing how you can also add the variable to the other columns

Comments

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.