在sqlite中更新查询

时间:2018-11-15 13:46:39

标签: java android sql database sqlite

活动代码

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

我不确定代码,但这是活动的onCreate ...使用此代码可以进行更改吗?

这是正确的方法吗?

我希望查询是可以理解的...

错误

 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)

1 个答案:

答案 0 :(得分:0)

如果db已正确初始化,请使用以下行:

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

您想执行一条UPDATE语句,该语句将使用从变量MostMessagesSent中存储的查询中获取的值更新表MainData的列MMS

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

为此,您必须正确初始化db对象。
我想您有一个扩展SQLiteOpenHelper的类,该类提供db的初始化。
此外,表和列的名称必须匹配预期的行为。
问题是execSQL()不返回表示更新成功与否的值。
您必须自己检查。