1

我需要更新数据库中的选定列,但是我无法获取更新的数据。用户单击下一步按钮后,数据应该会更新。表中总共需要更新 12 个字段。

场景有点不同,我已经有一个活动,主要说是否插入了 5 个字段,而在第二个活动中,我需要更新 SAME TABLE 中的 5 个字段

提前致谢

注意:dba 是 DBAdapter,即数据库,myDB 是 SQLiteDatabase

 nxt = (Button)findViewById(R.id.btn_nxt);
          nxt.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                String b1=t1.getText().toString();
                String b2=goal_website_trying_to_achieve_edttxt.getText().toString();
                String b3=communicate_edttxt.getText().toString();
                String b4=success_look_like_edttxt.getText().toString();
                String b5=usp_insights_edttxt.getText().toString();
                String b6=act_edttxt.getText().toString();
                String b7=market_edttxt.getText().toString();
                String b8=your_current_website_www_edttxt.getText().toString();
                String b9=your_current_website_good_edttxt.getText().toString();
                String b10=your_current_website_bad_edttxt.getText().toString();
                String b11=your_current_website_type_edttxt.getText().toString();
                String b12=your_current_website_update_edttxt.getText().toString();
                String b13=your_current_website_supporting_edttxt.getText().toString();

                if(!validatescreen2()){
                    return;
                }

                else{

                    myDB = DBAdapter.openDatabase(Newform_Screen2.this,DBAdapter.DATABASE_TABLE);




                    String updatequery = "UPDATE"+ DBAdapter.DATABASE_TABLE+ " SET ";       

                    updatequery = updatequery + " KEY_MESSAGE_1 = '" + b1 + "',";
                    updatequery = updatequery + " KEY_MESSAGE_2 = '" + b2 + "',";
                    updatequery = updatequery + " KEY_MESSAGE_3 = '" + b3 + "',";
                    updatequery = updatequery + " KEY_MESSAGE_4 = '" + b4 + "',";
                    updatequery = updatequery + " KEY_MESSAGE_5 = '" + b5 + "',";
                    updatequery = updatequery + " KEY_MESSAGE_6 = '" + b6 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_1 = '" + b7 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_2 = '" + b8 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_3 = '" + b9 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_TYPE = '" + b10 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_NEED = '" + b11 + "',";
                    updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_OTHER = '" + b12 + ",";

                    updatequery = updatequery +  "WHERE" + DBAdapter.KEY_ROWID + "=" + mRowId +"';";

                    try{
                        myDB.execSQL(updatequery);
                    }catch(Exception e){
                        //Log("Caught while writing response" + e.getLocalizedMessage());
                    }finally{
                        if(myDB != null)myDB.close();
                        showDialog(DIALOG_ID);
                    }

                }
            }
          });

DBAdapter课堂上

public static SQLiteDatabase openDatabase(Context context, String DBname){
    SQLiteDatabase myDB;
    myDB = context.openOrCreateDatabase(DBname,Context.MODE_PRIVATE, null);
    Cursor rs = null;
    boolean tableExists;
    try{
        rs = myDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE 1=0", null );
        tableExists = true;
    }catch(Exception ex){
        tableExists = false;
    }finally{
        if (rs != null) rs.close();
    }
    if(!tableExists) {
        myDB.execSQL(DATABASE_CREATE);
    }

    return myDB;
}

public static void executeQuery(SQLiteDatabase db,String query){

    if(db != null){
        db.execSQL(query);
    }

}

public static Cursor selectQuery(SQLiteDatabase db,String query){

    if(db != null)
    {
        Cursor c = db.rawQuery(query,null);
        return c;
    }
    else
    return null;

}

public static void closeDatabase(SQLiteDatabase db){
    if (db != null)
    {
        //Log("Closing Database conn");
        db.close();
    }
}

进行更改后,但仍然无法更新..我需要进行任何其他更改吗?

dba.open();
                        ContentValues args = new ContentValues();
                        args.put(DBAdapter.KEY_MESSAGE_1, b2);
                        args.put(DBAdapter.KEY_MESSAGE_2, b3);
                        args.put(DBAdapter.KEY_MESSAGE_3, b4);
                        args.put(DBAdapter.KEY_MESSAGE_4, b5);
                        args.put(DBAdapter.KEY_MESSAGE_5, b6);
                        args.put(DBAdapter.KEY_MESSAGE_6, b7);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_1, b8);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_2, b9);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_3, b10);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_TYPE, b11);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_NEED, b12);
                        args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_OTHER, b13);

                        String where = "_id = " + mRowId.toString();

                myDB.update(DBAdapter.DATABASE_TABLE, args,where, null);    
                            dba.close();
                            showDialog(DIALOG_ID);
                    }


                }

              });

添加 try/catch 后

try{
                        myDB.beginTransaction();
                dba.open();
                ContentValues args = new ContentValues();
                args.put(DBAdapter.KEY_MESSAGE_1, b2);
                args.put(DBAdapter.KEY_MESSAGE_2, b3);
                args.put(DBAdapter.KEY_MESSAGE_3, b4);
                args.put(DBAdapter.KEY_MESSAGE_4, b5);
                args.put(DBAdapter.KEY_MESSAGE_5, b6);
                args.put(DBAdapter.KEY_MESSAGE_6, b7);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_1, b8);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_2, b9);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_3, b10);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_TYPE, b11);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_NEED, b12);
                args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_OTHER, b13);

                String where =DBAdapter.KEY_ROWID +"=" + mRowId.toString();


                myDB.update(DBAdapter.DATABASE_TABLE, args,where, null);
                myDB.setTransactionSuccessful();
                    dba.close();

                    showDialog(DIALOG_ID);
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                     finally {
                        myDB.endTransaction();
                       }
                }


            }

          });

现在我收到这个错误

01-29 17:52:38.874: W/System.err(9257): java.lang.NullPointerException
01-29 17:52:38.874: W/System.err(9257):     at com.example.sales.Newform_Screen2$3.onClick(Newform_Screen2.java:150)
01-29 17:52:38.874: W/System.err(9257):     at android.view.View.performClick(View.java:4084)
01-29 17:52:38.883: W/System.err(9257):     at android.view.View$PerformClick.run(View.java:16966)
01-29 17:52:38.883: W/System.err(9257):     at android.os.Handler.handleCallback(Handler.java:615)
01-29 17:52:38.883: W/System.err(9257):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 17:52:38.893: W/System.err(9257):     at android.os.Looper.loop(Looper.java:137)
01-29 17:52:38.893: W/System.err(9257):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 17:52:38.893: W/System.err(9257):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 17:52:38.893: W/System.err(9257):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 17:52:38.893: W/System.err(9257):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 17:52:38.893: W/System.err(9257):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
4

3 回答 3

0

Use update method instead of query like myDB.update(mTableName, values, sWhereClause, null); values is ContentValues object, put all your fields and values in this object ,sWhereClause is where clause without where word on selected fields like name='xyz'

于 2013-01-29T08:24:41.500 回答
0

使用更新方法。

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

更新数据库中行的便捷方法。您可以从
- http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#update获取更多信息

于 2013-01-29T08:25:04.723 回答
0

显然,您在 SQLite 语句中犯了很多错误。首先,在你写“WHERE”之前,应该删除最后一个“,”。其次,“WHERE”前后应该有空格。最后但并非最不重要的一点是,您需要一个“;” 在字符串末尾的 SQLite-Query-String 中。好吧,这三件事可能无法完全解决问题,但它们至少应该让您更接近解决方案。

于 2013-05-19T22:55:48.147 回答