add column to another one row by row









up vote
-1
down vote

favorite












I'm writing code on android. I try to write a code which take data from one column and add to another column's string, do this for all database.



Like this:



Before:



 A column | B column 
string 1 string 2


After:



A column | B column 
string 1 string 2+string 1


I'll try get strings from columns and update column but i can't do.



Database.KEY_COLUMN1 and Database.KEY_COLUMN2 are name of columns(string type)



SQLiteDatabase mydatabase = dbHelper.getMydatabase();
String query = new String( "select "+Database.KEY_COLUMN1+",
"+Database.KEY_COLUMN2'+ " from "+ Database.TABLE_NAME );
cursor = mydatabase.rawQuery(query,null);
while(cursor.moveToNext())
String column1 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN1));
String column2 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN2));
column2=column1+colum2;
mydatabase.execSQL("UPDATE "+Database.DATABASE_TABLE+" SET "
+ Database.KEY_COLUMN2+ " = "
+Datababese.KEY_COLUMN1+ " + " + Database.KEY_COLUMN2
+" WHERE "+Database.KEY_COLUMN2 + " = " +cursor.getColumnIndex(Database.KEY_COLUMN2));

cursor.close();


I updated exceSQL but it didn't write anyting to databese.










share|improve this question























  • Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
    – m0skit0
    Oct 20 '14 at 14:41







  • 1




    colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
    – StuartLC
    Oct 20 '14 at 14:41














up vote
-1
down vote

favorite












I'm writing code on android. I try to write a code which take data from one column and add to another column's string, do this for all database.



Like this:



Before:



 A column | B column 
string 1 string 2


After:



A column | B column 
string 1 string 2+string 1


I'll try get strings from columns and update column but i can't do.



Database.KEY_COLUMN1 and Database.KEY_COLUMN2 are name of columns(string type)



SQLiteDatabase mydatabase = dbHelper.getMydatabase();
String query = new String( "select "+Database.KEY_COLUMN1+",
"+Database.KEY_COLUMN2'+ " from "+ Database.TABLE_NAME );
cursor = mydatabase.rawQuery(query,null);
while(cursor.moveToNext())
String column1 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN1));
String column2 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN2));
column2=column1+colum2;
mydatabase.execSQL("UPDATE "+Database.DATABASE_TABLE+" SET "
+ Database.KEY_COLUMN2+ " = "
+Datababese.KEY_COLUMN1+ " + " + Database.KEY_COLUMN2
+" WHERE "+Database.KEY_COLUMN2 + " = " +cursor.getColumnIndex(Database.KEY_COLUMN2));

cursor.close();


I updated exceSQL but it didn't write anyting to databese.










share|improve this question























  • Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
    – m0skit0
    Oct 20 '14 at 14:41







  • 1




    colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
    – StuartLC
    Oct 20 '14 at 14:41












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm writing code on android. I try to write a code which take data from one column and add to another column's string, do this for all database.



Like this:



Before:



 A column | B column 
string 1 string 2


After:



A column | B column 
string 1 string 2+string 1


I'll try get strings from columns and update column but i can't do.



Database.KEY_COLUMN1 and Database.KEY_COLUMN2 are name of columns(string type)



SQLiteDatabase mydatabase = dbHelper.getMydatabase();
String query = new String( "select "+Database.KEY_COLUMN1+",
"+Database.KEY_COLUMN2'+ " from "+ Database.TABLE_NAME );
cursor = mydatabase.rawQuery(query,null);
while(cursor.moveToNext())
String column1 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN1));
String column2 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN2));
column2=column1+colum2;
mydatabase.execSQL("UPDATE "+Database.DATABASE_TABLE+" SET "
+ Database.KEY_COLUMN2+ " = "
+Datababese.KEY_COLUMN1+ " + " + Database.KEY_COLUMN2
+" WHERE "+Database.KEY_COLUMN2 + " = " +cursor.getColumnIndex(Database.KEY_COLUMN2));

cursor.close();


I updated exceSQL but it didn't write anyting to databese.










share|improve this question















I'm writing code on android. I try to write a code which take data from one column and add to another column's string, do this for all database.



Like this:



Before:



 A column | B column 
string 1 string 2


After:



A column | B column 
string 1 string 2+string 1


I'll try get strings from columns and update column but i can't do.



Database.KEY_COLUMN1 and Database.KEY_COLUMN2 are name of columns(string type)



SQLiteDatabase mydatabase = dbHelper.getMydatabase();
String query = new String( "select "+Database.KEY_COLUMN1+",
"+Database.KEY_COLUMN2'+ " from "+ Database.TABLE_NAME );
cursor = mydatabase.rawQuery(query,null);
while(cursor.moveToNext())
String column1 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN1));
String column2 = cursor.getString(cursor.getColumnIndex(Database.KEY_COLUMN2));
column2=column1+colum2;
mydatabase.execSQL("UPDATE "+Database.DATABASE_TABLE+" SET "
+ Database.KEY_COLUMN2+ " = "
+Datababese.KEY_COLUMN1+ " + " + Database.KEY_COLUMN2
+" WHERE "+Database.KEY_COLUMN2 + " = " +cursor.getColumnIndex(Database.KEY_COLUMN2));

cursor.close();


I updated exceSQL but it didn't write anyting to databese.







android sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 23:48









Brad Larson

161k40363541




161k40363541










asked Oct 20 '14 at 14:37









user4129359

12




12











  • Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
    – m0skit0
    Oct 20 '14 at 14:41







  • 1




    colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
    – StuartLC
    Oct 20 '14 at 14:41
















  • Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
    – m0skit0
    Oct 20 '14 at 14:41







  • 1




    colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
    – StuartLC
    Oct 20 '14 at 14:41















Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
– m0skit0
Oct 20 '14 at 14:41





Btw you can SELECT and UPDATE in same SQL, e.g. UPDATE table SET column = (SELECT...)
– m0skit0
Oct 20 '14 at 14:41





1




1




colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
– StuartLC
Oct 20 '14 at 14:41




colum2 aside, can you verify that you need to do this updating generically, i.e. not on a specific table? Otherwise, a single UPDATE statement would update all records in one statement?
– StuartLC
Oct 20 '14 at 14:41












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The problem with your UPDATE statement is that an UPDATE without a WHERE clause updates all rows of the table, so you end up with the same values in all rows.



If you wanted to update rows one by one, you would have to read the ID of the each row, and use that to filter the UPDATE, like this:



UPDATE MyTable SET BColumn = 'newValue' WHERE ID = 123


Anyway, you can simply execute a single command that does the string concatenation individually for each row:



UPDATE MyTable SET ArrayColumn = Column1 + Column2


or in Java:



db.execSQL("UPDATE " + Database.DATABASE_TABLE +
" SET " + Database.KEY_ARRAY + " = " +
Database.KEY_COLUMN1 + " + " + Database.KEY_COLUMN2);





share|improve this answer






















  • Thanks. So i need id values to update row by row. How can i get id of that row?
    – user4129359
    Oct 21 '14 at 8:32










  • Why would you want to update row by row? Did you read the second half of the answer?
    – CL.
    Oct 21 '14 at 10:48










  • Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
    – user4129359
    Oct 21 '14 at 12:56











  • Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
    – CL.
    Oct 21 '14 at 13:15










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26468276%2fadd-column-to-another-one-row-by-row%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The problem with your UPDATE statement is that an UPDATE without a WHERE clause updates all rows of the table, so you end up with the same values in all rows.



If you wanted to update rows one by one, you would have to read the ID of the each row, and use that to filter the UPDATE, like this:



UPDATE MyTable SET BColumn = 'newValue' WHERE ID = 123


Anyway, you can simply execute a single command that does the string concatenation individually for each row:



UPDATE MyTable SET ArrayColumn = Column1 + Column2


or in Java:



db.execSQL("UPDATE " + Database.DATABASE_TABLE +
" SET " + Database.KEY_ARRAY + " = " +
Database.KEY_COLUMN1 + " + " + Database.KEY_COLUMN2);





share|improve this answer






















  • Thanks. So i need id values to update row by row. How can i get id of that row?
    – user4129359
    Oct 21 '14 at 8:32










  • Why would you want to update row by row? Did you read the second half of the answer?
    – CL.
    Oct 21 '14 at 10:48










  • Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
    – user4129359
    Oct 21 '14 at 12:56











  • Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
    – CL.
    Oct 21 '14 at 13:15














up vote
0
down vote













The problem with your UPDATE statement is that an UPDATE without a WHERE clause updates all rows of the table, so you end up with the same values in all rows.



If you wanted to update rows one by one, you would have to read the ID of the each row, and use that to filter the UPDATE, like this:



UPDATE MyTable SET BColumn = 'newValue' WHERE ID = 123


Anyway, you can simply execute a single command that does the string concatenation individually for each row:



UPDATE MyTable SET ArrayColumn = Column1 + Column2


or in Java:



db.execSQL("UPDATE " + Database.DATABASE_TABLE +
" SET " + Database.KEY_ARRAY + " = " +
Database.KEY_COLUMN1 + " + " + Database.KEY_COLUMN2);





share|improve this answer






















  • Thanks. So i need id values to update row by row. How can i get id of that row?
    – user4129359
    Oct 21 '14 at 8:32










  • Why would you want to update row by row? Did you read the second half of the answer?
    – CL.
    Oct 21 '14 at 10:48










  • Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
    – user4129359
    Oct 21 '14 at 12:56











  • Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
    – CL.
    Oct 21 '14 at 13:15












up vote
0
down vote










up vote
0
down vote









The problem with your UPDATE statement is that an UPDATE without a WHERE clause updates all rows of the table, so you end up with the same values in all rows.



If you wanted to update rows one by one, you would have to read the ID of the each row, and use that to filter the UPDATE, like this:



UPDATE MyTable SET BColumn = 'newValue' WHERE ID = 123


Anyway, you can simply execute a single command that does the string concatenation individually for each row:



UPDATE MyTable SET ArrayColumn = Column1 + Column2


or in Java:



db.execSQL("UPDATE " + Database.DATABASE_TABLE +
" SET " + Database.KEY_ARRAY + " = " +
Database.KEY_COLUMN1 + " + " + Database.KEY_COLUMN2);





share|improve this answer














The problem with your UPDATE statement is that an UPDATE without a WHERE clause updates all rows of the table, so you end up with the same values in all rows.



If you wanted to update rows one by one, you would have to read the ID of the each row, and use that to filter the UPDATE, like this:



UPDATE MyTable SET BColumn = 'newValue' WHERE ID = 123


Anyway, you can simply execute a single command that does the string concatenation individually for each row:



UPDATE MyTable SET ArrayColumn = Column1 + Column2


or in Java:



db.execSQL("UPDATE " + Database.DATABASE_TABLE +
" SET " + Database.KEY_ARRAY + " = " +
Database.KEY_COLUMN1 + " + " + Database.KEY_COLUMN2);






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 21 '14 at 13:09

























answered Oct 20 '14 at 18:08









CL.

134k12120153




134k12120153











  • Thanks. So i need id values to update row by row. How can i get id of that row?
    – user4129359
    Oct 21 '14 at 8:32










  • Why would you want to update row by row? Did you read the second half of the answer?
    – CL.
    Oct 21 '14 at 10:48










  • Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
    – user4129359
    Oct 21 '14 at 12:56











  • Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
    – CL.
    Oct 21 '14 at 13:15
















  • Thanks. So i need id values to update row by row. How can i get id of that row?
    – user4129359
    Oct 21 '14 at 8:32










  • Why would you want to update row by row? Did you read the second half of the answer?
    – CL.
    Oct 21 '14 at 10:48










  • Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
    – user4129359
    Oct 21 '14 at 12:56











  • Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
    – CL.
    Oct 21 '14 at 13:15















Thanks. So i need id values to update row by row. How can i get id of that row?
– user4129359
Oct 21 '14 at 8:32




Thanks. So i need id values to update row by row. How can i get id of that row?
– user4129359
Oct 21 '14 at 8:32












Why would you want to update row by row? Did you read the second half of the answer?
– CL.
Oct 21 '14 at 10:48




Why would you want to update row by row? Did you read the second half of the answer?
– CL.
Oct 21 '14 at 10:48












Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
– user4129359
Oct 21 '14 at 12:56





Of course i read. I edited first message. Did you read that message after edit? I want update row by row because i though if update all database it write same value in all rows. If it can add a column to another one, it doesn't matter.
– user4129359
Oct 21 '14 at 12:56













Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
– CL.
Oct 21 '14 at 13:15




Stack Overflow is not a forum but a question&answer site; therefore, do not change your question after it got answered. (And the ID column would be a column like any other.)
– CL.
Oct 21 '14 at 13:15

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26468276%2fadd-column-to-another-one-row-by-row%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo