更新Android中SQLite表中的选定列

时间:2013-01-29 08:13:20

标签: android sqlite android-sqlite

我需要更新数据库中的选定列,但是我无法获取更新的数据。只要用户点击下一步按钮,数据就会更新。有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)

3 个答案:

答案 0 :(得分:0)

使用更新方法而不是像myDB.update(mTableName, values, sWhereClause, null);这样的查询值是ContentValues对象,将所有字段和值放在此对象中,sWhereClause是where子句,而不包含所选字段上的单词,如name ='xyz'

答案 1 :(得分:0)

使用更新方法。

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

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

答案 2 :(得分:0)

显然你在SQLite-Statement中犯了很多错误。首先,在写“WHERE”之前,应删除最后一个“,”。其次,“WHERE”之前和之后应该有空格。最后但并非最不重要的是,你需要一个“;”在你的SQLite-Query-String中结束。这3件事可能无法完全解决问题,但至少应该让你更接近解决方案。