【发布时间】:2017-08-10 01:53:59
【问题描述】:
我的 SQLite 数据库有 5 列,我想更新数据。
它是第一次创建的,只有我必须更新它可以工作的一列。但现在我意识到我必须更新 2 列。我的应用程序有一个自定义的列表视图,它是由这 4 列创建的(id 列除外)。
我必须更新 2 列。它们是第三列和第四列。首先,在我创建的数据库中,我必须更新可以通过 id 从数据库中找到的第三列。但是现在,我尝试更新第三列和第四列。我这样做是因为第四列是颜色的整数,我想根据第三列值更新列表视图的颜色。
我正在寻找答案,但没有找到。我该如何解决?
我的数据库添加方法;
public boolean addData(String item , String number ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3 , number);
contentValues.put(COL4, "50" ); //third column
contentValues.put(COL5 , "#27fbf0"); // Fourth column
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
我的数据库更新方法;
public void updateName(String newName, int id, String oldName,String newboya,String oldboya){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " +
COL4 + " = '" + newName + "' AND " +
COL5 + " = '" + newboya+ "' WHERE " +
COL1 + " = '" + id + "'" + " AND " +
COL4 + " = '" + oldName + " AND " + //third column
COL5 + " = '" + oldboya+ "'"; // Fourth column
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
How to I create my customized listview ;
while(data.moveToNext()){
listDataId.add(data.getString(0));
listData.add(data.getString(1));
listDatanumber.add(data.getString(2));
listDataoran.add(data.getString(3));
listDataBoya.add(data.getString(4));
}
liste.setAdapter(oyunTextView);
}
class OyunTextView extends BaseAdapter {
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.oyuntextview, null);
TextView namesbox = (TextView) convertView.findViewById(R.id.isim);
sayılar = (TextView) convertView.findViewById(R.id.sayı);
namesbox.setText(listData.get(position));
sayılar.setText(listDataoran.get(position));
convertView.setBackgroundColor(Color.parseColor(listDataBoya.get(position)));
return convertView;
}
}
如何为数据库设置新值的颜色; sonuc 是新值。它转到 COL4
sonuc = Integer.valueOf(old) + toplamaislemi;
if (sonuc >= 10 && sonuc < 19){ //20-10
boyayazı = "#032E28" ;
}
//
if (sonuc <0 || sonuc ==0 ){
sonuc = 0;
boyayazı = "#121210";
Toast.makeText(AnaMenu.this,
"Bu kişi için en düşük orana ulaştınız.Artık bu kişi ile eşleştirilmeyeceksiniz.",
Toast.LENGTH_LONG).show();
}
int se = Integer.parseInt(listDataId.get(selec));
String oldboya = listDataBoya.get(selec);
mDatabaseHelper.updateName(String.valueOf(sonuc), se, old ,boyayazı , oldboya );
oyunTextView.notifyDataSetChanged();
Intent diger = new Intent(AnaMenu.this,AnaMenubos.class);
startActivity(diger);
AnaMenu.this.finish();
}
} ) .show();
}
我的日志:
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/DatabaseHelper: updateName: query: UPDATE people_table SET oran = '45' AND boya = '#139F98' WHERE ID = '13' AND oran = '50 AND boya = '#27fbf0'
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/DatabaseHelper: updateName: Setting name to 45
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication E/SQLiteLog: (1) near "#27fbf0": syntax error
08-10 04:55:00.681 791-791/com.example.asusnb.myapplication D/AndroidRuntime: Shutting down VM
08-10 04:55:00.691 791-791/com.example.asusnb.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.asusnb.myapplication, PID: 791
android.database.sqlite.SQLiteException: near "#27fbf0": syntax error (code 1): , while compiling: UPDATE people_table SET oran = '45' AND boya = '#139F98' WHERE ID = '13' AND oran = '50 AND boya = '#27fbf0'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1795)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1726)
at com.example.asusnb.myapplication.DatabaseHelper.updateName(DatabaseHelper.java:106)
at com.example.asusnb.myapplication.AnaMenu$2.onClick(AnaMenu.java:299)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
【问题讨论】:
-
请清理您的代码,并考虑使用准备好的语句。
-
偏离路线。我现在正在做
-
我已经编辑了准备好的语句。清理的代码在哪里?这是我的应用程序代码还是我的问题。
-
您缺少报价...
AND oran = '50 AND boya = '#27fbf0'
标签: android sqlite listview colors