Updating a query in sqlite












1















Activity Code



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";
db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


Im not sure about the code but this is is the onCreate of an activity... WIll the changes take place using this code?



Is it the correct method?



I hope the query is understandable...



Error



 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.appmaster.akash.messageplus.Settings.UsageSettings.onCreateView(UsageSettings.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)









share|improve this question




















  • 2





    To know if the changes will take place, just try...

    – StephaneM
    Nov 15 '18 at 13:52











  • Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

    – user10643013
    Nov 15 '18 at 13:52













  • just run this query and check your table's content is updated or not

    – Yash Fatnani
    Nov 15 '18 at 13:58











  • I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

    – Fantômas
    Nov 15 '18 at 14:30


















1















Activity Code



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";
db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


Im not sure about the code but this is is the onCreate of an activity... WIll the changes take place using this code?



Is it the correct method?



I hope the query is understandable...



Error



 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.appmaster.akash.messageplus.Settings.UsageSettings.onCreateView(UsageSettings.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)









share|improve this question




















  • 2





    To know if the changes will take place, just try...

    – StephaneM
    Nov 15 '18 at 13:52











  • Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

    – user10643013
    Nov 15 '18 at 13:52













  • just run this query and check your table's content is updated or not

    – Yash Fatnani
    Nov 15 '18 at 13:58











  • I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

    – Fantômas
    Nov 15 '18 at 14:30
















1












1








1








Activity Code



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";
db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


Im not sure about the code but this is is the onCreate of an activity... WIll the changes take place using this code?



Is it the correct method?



I hope the query is understandable...



Error



 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.appmaster.akash.messageplus.Settings.UsageSettings.onCreateView(UsageSettings.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)









share|improve this question
















Activity Code



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";
db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


Im not sure about the code but this is is the onCreate of an activity... WIll the changes take place using this code?



Is it the correct method?



I hope the query is understandable...



Error



 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
at com.appmaster.akash.messageplus.Settings.UsageSettings.onCreateView(UsageSettings.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)






java android sql database sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:56

























asked Nov 15 '18 at 13:46







user10643013















  • 2





    To know if the changes will take place, just try...

    – StephaneM
    Nov 15 '18 at 13:52











  • Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

    – user10643013
    Nov 15 '18 at 13:52













  • just run this query and check your table's content is updated or not

    – Yash Fatnani
    Nov 15 '18 at 13:58











  • I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

    – Fantômas
    Nov 15 '18 at 14:30
















  • 2





    To know if the changes will take place, just try...

    – StephaneM
    Nov 15 '18 at 13:52











  • Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

    – user10643013
    Nov 15 '18 at 13:52













  • just run this query and check your table's content is updated or not

    – Yash Fatnani
    Nov 15 '18 at 13:58











  • I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

    – Fantômas
    Nov 15 '18 at 14:30










2




2





To know if the changes will take place, just try...

– StephaneM
Nov 15 '18 at 13:52





To know if the changes will take place, just try...

– StephaneM
Nov 15 '18 at 13:52













Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

– user10643013
Nov 15 '18 at 13:52







Im not sure how to... Because i cant add a toast or a Log statement for such code @StephaneM

– user10643013
Nov 15 '18 at 13:52















just run this query and check your table's content is updated or not

– Yash Fatnani
Nov 15 '18 at 13:58





just run this query and check your table's content is updated or not

– Yash Fatnani
Nov 15 '18 at 13:58













I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

– Fantômas
Nov 15 '18 at 14:30







I'm not sure wether this command UPDATE MainData SET MostMessagesSent = (SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData)) WHERE Data = MyData would run or not. Test it in SQLiteBrowser or any tool able to run queries and commands against SQLite

– Fantômas
Nov 15 '18 at 14:30














1 Answer
1






active

oldest

votes


















0














If db is properly initialized, with this line:



db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


you want to execute an UPDATE statement that will update the column MostMessagesSent of table MainData with a value fetched from the query strored in the variable MMS:



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";


In order for this to work you must have initialized properly the db object.

I suppose you have a class extending SQLiteOpenHelper which provides the initialization of db.

Also the names of the tables and columns must match the intended behavior.

The problem is that execSQL() does not return a value indicating that the update was successful or not.
You have to check it yourself.






share|improve this answer


























  • I got an error... look to the edited question please

    – user10643013
    Nov 15 '18 at 17:56











  • @NikkiZepher is db initialized properly? I can't say without your code.

    – forpas
    Nov 15 '18 at 18:04











  • i have just declared db; What should be its value?

    – user10643013
    Nov 15 '18 at 18:15











  • @NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

    – forpas
    Nov 15 '18 at 18:18











  • i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

    – user10643013
    Nov 16 '18 at 18:58











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',
autoActivateHeartbeat: false,
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%2f53320872%2fupdating-a-query-in-sqlite%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









0














If db is properly initialized, with this line:



db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


you want to execute an UPDATE statement that will update the column MostMessagesSent of table MainData with a value fetched from the query strored in the variable MMS:



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";


In order for this to work you must have initialized properly the db object.

I suppose you have a class extending SQLiteOpenHelper which provides the initialization of db.

Also the names of the tables and columns must match the intended behavior.

The problem is that execSQL() does not return a value indicating that the update was successful or not.
You have to check it yourself.






share|improve this answer


























  • I got an error... look to the edited question please

    – user10643013
    Nov 15 '18 at 17:56











  • @NikkiZepher is db initialized properly? I can't say without your code.

    – forpas
    Nov 15 '18 at 18:04











  • i have just declared db; What should be its value?

    – user10643013
    Nov 15 '18 at 18:15











  • @NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

    – forpas
    Nov 15 '18 at 18:18











  • i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

    – user10643013
    Nov 16 '18 at 18:58
















0














If db is properly initialized, with this line:



db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


you want to execute an UPDATE statement that will update the column MostMessagesSent of table MainData with a value fetched from the query strored in the variable MMS:



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";


In order for this to work you must have initialized properly the db object.

I suppose you have a class extending SQLiteOpenHelper which provides the initialization of db.

Also the names of the tables and columns must match the intended behavior.

The problem is that execSQL() does not return a value indicating that the update was successful or not.
You have to check it yourself.






share|improve this answer


























  • I got an error... look to the edited question please

    – user10643013
    Nov 15 '18 at 17:56











  • @NikkiZepher is db initialized properly? I can't say without your code.

    – forpas
    Nov 15 '18 at 18:04











  • i have just declared db; What should be its value?

    – user10643013
    Nov 15 '18 at 18:15











  • @NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

    – forpas
    Nov 15 '18 at 18:18











  • i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

    – user10643013
    Nov 16 '18 at 18:58














0












0








0







If db is properly initialized, with this line:



db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


you want to execute an UPDATE statement that will update the column MostMessagesSent of table MainData with a value fetched from the query strored in the variable MMS:



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";


In order for this to work you must have initialized properly the db object.

I suppose you have a class extending SQLiteOpenHelper which provides the initialization of db.

Also the names of the tables and columns must match the intended behavior.

The problem is that execSQL() does not return a value indicating that the update was successful or not.
You have to check it yourself.






share|improve this answer















If db is properly initialized, with this line:



db.execSQL("UPDATE MainData SET MostMessagesSent = "+ MMS + "+ WHERE Data = MyData");


you want to execute an UPDATE statement that will update the column MostMessagesSent of table MainData with a value fetched from the query strored in the variable MMS:



String MMS = "(SELECT Name FROM UserData WHERE MessagesSent=(SELECT max(MessagesSent) FROM UserData))";


In order for this to work you must have initialized properly the db object.

I suppose you have a class extending SQLiteOpenHelper which provides the initialization of db.

Also the names of the tables and columns must match the intended behavior.

The problem is that execSQL() does not return a value indicating that the update was successful or not.
You have to check it yourself.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 18:07

























answered Nov 15 '18 at 13:59









forpasforpas

16.5k3628




16.5k3628













  • I got an error... look to the edited question please

    – user10643013
    Nov 15 '18 at 17:56











  • @NikkiZepher is db initialized properly? I can't say without your code.

    – forpas
    Nov 15 '18 at 18:04











  • i have just declared db; What should be its value?

    – user10643013
    Nov 15 '18 at 18:15











  • @NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

    – forpas
    Nov 15 '18 at 18:18











  • i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

    – user10643013
    Nov 16 '18 at 18:58



















  • I got an error... look to the edited question please

    – user10643013
    Nov 15 '18 at 17:56











  • @NikkiZepher is db initialized properly? I can't say without your code.

    – forpas
    Nov 15 '18 at 18:04











  • i have just declared db; What should be its value?

    – user10643013
    Nov 15 '18 at 18:15











  • @NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

    – forpas
    Nov 15 '18 at 18:18











  • i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

    – user10643013
    Nov 16 '18 at 18:58

















I got an error... look to the edited question please

– user10643013
Nov 15 '18 at 17:56





I got an error... look to the edited question please

– user10643013
Nov 15 '18 at 17:56













@NikkiZepher is db initialized properly? I can't say without your code.

– forpas
Nov 15 '18 at 18:04





@NikkiZepher is db initialized properly? I can't say without your code.

– forpas
Nov 15 '18 at 18:04













i have just declared db; What should be its value?

– user10643013
Nov 15 '18 at 18:15





i have just declared db; What should be its value?

– user10643013
Nov 15 '18 at 18:15













@NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

– forpas
Nov 15 '18 at 18:18





@NikkiZepher what do you mean just declared? Do you have a class extending SQLiteOpenHelper? If you do post its code and the code of the initialization of db. If you don't have such a class then you can't access an sqlite database.

– forpas
Nov 15 '18 at 18:18













i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

– user10643013
Nov 16 '18 at 18:58





i do have a class extending SQLiteOpenHelper but there is no method in there for updating table... i have a method for fetching data and adding data but not for updating so can u please get me the method code for updating?

– user10643013
Nov 16 '18 at 18:58




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320872%2fupdating-a-query-in-sqlite%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

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma