3

In my project ,add a function to insert elements to data base in a class public class DBHelper extends SQLiteOpenHelper .The function is given below.

public void insertIntoDatabase(String field_one,String field_two){
    String sql = "INSERT INTO UserTable.NAME (COL_USERNAME, COL_PASSWORD) " +
            "VALUES (" + field_one + ", " + field_two + ")";
    db.execSQL(sql);

In main activity read name and password and call the function insert into database

DBHelper dbhelper;



dbhelper.insertIntoDatabase(rdname.getText().toString(), rdpwrd.getText().toString());

But there was an error .

3
  • "But there was an error ." ... really descriptive (add logcat and what the error tells) Commented Mar 15, 2013 at 13:07
  • Which error did you get? Commented Mar 15, 2013 at 13:08
  • error at dbhelper.insertIntoDatabase(rdname.getText().toString(), rdname.getText().toString()); Commented Mar 15, 2013 at 13:20

3 Answers 3

6

I pretty recommend to you use API method of SQLiteDatabase db.insert() that is directly designated for inserting new rows to table. This solution is cleaner.

public void insertIntoDatabase(String field_one, String field_two){
   ContentValues data = new ContentValues();
   data.put("COL_USERNAME", field_one);
   data.put("COL_PASSWORD", field_two);
   if (db == null) {
      // make sure that your db is properly initialised
   }
   db.insert("tablename", "nullColumnHack", data);
}

Explanation of nullColumnHack:

If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.

It means that in Android SQLite you have to specify one column that will be assign NULL to in the case if you pass empty ContentValues.

If you want to use your approach i recommend to you an usage of placeholders.

String sql = "INSERT INTO UserTable.NAME (COL_USERNAME, COL_PASSWORD) " +
            "VALUES (?, ?)";
    db.execSQL(sql, new String[] {field_one, field_two});

Note: Probably it didn't work because you didn't use single quotes.

VALUES ("' + field_one + '", "' + field_two + '")"

And also make sure that given tablename is correct. I recommend to create static variables which will hold tablename and column names.

public static final String TABLE_NAME = "Table";
String query = "insert into " + ClassName.TABLE_NAME + " values(?, ?)"
Sign up to request clarification or add additional context in comments.

1 Comment

what if all i have in a table is just the _ID , and I want to add an empty row into it? what should I put into the parameters of the insert() ?
0

Your sql string doesn't separate your static variables from the rest of your SQL statement. This should fix it:

String sql = "INSERT INTO " + UserTable.NAME + "(" + COL_USERNAME + "," + COL_PASSWORD ") VALUES (" + ...

Just make sure that UserTable.NAME is the table name and that is has fields named exactly the same as the values of COL_USERNAME and COL_PASSWORD.

Comments

0
String sql = "INSERT INTO UserTable.NAME (COL_USERNAME, COL_PASSWORD) " +
             "VALUES ("+'" + field_one + "', '" + field_two + "'+")";

Values of column should be in '' single quoted string....

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.