Parmod,

There are several ways to do this:

1. Build the SQL statement string to include values:

String mysql = "INSERT INTO " + SAMPLE_TABLE_NAME = " VALUES ('" + nm + "', "'" + add + "')";

The variable will get this value:

INSERT INTO whatever_your_table_name_is VALUES ('Pramod', 'Deore')

2. Use what's called variable binding to supply values:

SQLiteDatabase database = <whatever>;
SQLiteStatement stmt = database.compileStatement("INSERT INTO " + SAMPLE_TABLE_NAME + " VALUES (?, ?)";

for (.... insert many values .... ) {
    stmt.bindString(1, valueForColumn1);
    stmt.bindLong(2, valueForColumn2);

    long newRowId = stmt.executeInsert();
}

The second way is preferred for several reasons:

- If you insert several rows, each insert runs faster than technique #1.
- It uses less memory inside MySQL
- It is secure - the user can supply malicious values, and you don't take special care to detect or escape them, the first technique can result in the execution of arbitrary SQL statements


http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html
http://developer.android.com/reference/android/database/sqlite/SQLiteProgram.html

-- Kostya

18.10.2010 14:26, Evgeny V ?????:
insert into my_table_name (string_type_column_name1, integer_type_column_name2) values('string_value1', 777)


On Mon, Oct 18, 2010 at 11:46 AM, pramod.deore <[email protected] <mailto:[email protected]>> wrote:

    Hi, I had created a database and I am writing some values ino that
    database as.

    sampleDB.execSQL("INSERT INTO " +
                                   SAMPLE_TABLE_NAME +
                                   " Values ('s','m');");

    Then here the values are added as s and m respectively.It is fine. But
    now I don't want to use hard coded value like's' and 'm'. Now I have 2
    String as.
    String nm = "Pramod";
    String add = "deore";

    Now I want to add these 2 String to database. When I write code as
    sampleDB.execSQL("INSERT INTO " +
                                   SAMPLE_TABLE_NAME +
                                   " Values (nm,add');");

    Then I got error as -
    10-18 15:15:14.464: ERROR/Database(21408): Failure 1 (no such column:
    s) on 0x2fb288 when preparing 'INSERT INTO App Values (s,m);'.
    10-18 15:15:14.473: ERROR/ShowSwitches(21408): Could not create or
    Open the database.

    How to add String to database?
    Thanks

    --
    You received this message because you are subscribed to the Google
    Groups "Android Developers" group.
    To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:android-developers%[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/android-developers?hl=en


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to