0

I want to initialize one column values on Android Sqlite data-base with another column values in the same data-base. However, I used this function to do that:

 public void InitializeColumnValues(){
    String sql = "UPDATE \""+ DATABASE_TABLE + "\" SET \"" + Column2 +"\" = \"" + Column1 + "\"";
    db.rawQuery(sql, null); 

}

But, unfortunately it does not work with me! Is there any problem on its structure? or maybe in Android Sqlite data-base I need to initialize each value in column one by one based on id value!

Is there any way to do this with less time as possible?

Any help will be appreciated,
Thanks ...

4
  • What do you mean with "does not work"? Commented Jan 15, 2014 at 8:52
  • yes,, I mean the Column2 values do not initialize as Column1 values .. the update for column2 values function above not execute! ... Commented Jan 15, 2014 at 8:55
  • you have to try to find any feedback from the sqlite engine. probably an error, a warning, or info about affected rows. check the log cat, the catched exceptions, the return values, etc...there is always a mesagge Commented Jan 15, 2014 at 15:57
  • yes ,, actually I check all of them! but there is no error in LogCat!The problem in the function above that it does not work! actually I update the column values one by one based on row _id value; but this take much time! now I am looking for best mechanism to reduce the update time!. thanks @Carlos Robles Commented Jan 15, 2014 at 21:20

1 Answer 1

4

Your problem can be solved by using "db.execSQL(sql)" instead of using "db.rawQuery(sql,null)" and also no need to use the quotation symbol '"' in writing the required query. Thus, the complete and tested function be as follow:

 public void InitializeColumnValues() {
    String sql = "UPDATE "+ DATABASE_TABLE + " SET " + Column2  +" = " + Column1;
    db.execSQL(sql);
    db.close();
 }

Note that please the main difference between execSQL and rawQuery APIs is:

public void execSQL (String sql)

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

public Cursor rawQuery (String sql, String[] selectionArgs)

Runs the provided SQL and returns a Cursor over the result set.

I hope that the answer is useful and works well with you.

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

1 Comment

How much can I thank you...! really no much! sure with your great remarkable note everything goes well with me! ... you just who know, who's the "mistakes maker" ... Thanks @Mahdi

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.