gpt4 book ai didi

java - 如何获取表单中的UserID并将其添加到表中?

转载 作者:行者123 更新时间:2023-12-01 16:15:01 30 4
gpt4 key购买 nike

我正在尝试从表“用户”中获取用户ID,并将其添加到 Activity 内的表单中,在 Activity 中我将新优惠添加到表“产品”中。我已经知道如何从表中获取 UserID,但我不知道如何将其插入表单中。

如何获取用户ID

public int getuserid(String username) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from user where username=?", new String[]{username});
if (cursor.getCount()>0)
return cursor.getInt(0);
else
return 0;

}
}

编辑

我的 AddOffers Activity ,

   @Override


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addofferactivity);
myTraderDbHelper = new MyTraderDbHelper(this);

add_art = (EditText) findViewById(R.id.add_art);
add_namep = (EditText) findViewById(R.id.add_namep);
add_menge = (EditText) findViewById(R.id.add_menge);
add_standort = (EditText) findViewById(R.id.add_standort);
add_gueltigbis = (EditText) findViewById(R.id.add_gueltigbis);
add_userzuordnung = (EditText) findViewById(R.id.add_userzuordnung);
add_button = (Button) findViewById(R.id.add_button);
AddOffer();

}



public void AddOffer(){
RegDBHelper regDBhelper = new RegDBHelper(this);
regDBhelper.getuserid();
add_button.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
if (add_namep.getText().toString()!=null){
String username = add_namep.getText().toString();
boolean isInsirted = myTraderDbHelper.addOffer(add_art.getText().toString(),
add_namep.getText().toString(),
add_menge.getText().toString(),
add_standort.getText().toString(),
add_gueltigbis.getText().toString(),
getuserid(username)
add_userzuordnung.setText(""+getuserid(username));
}
if (isInserted =true)
Toast.makeText(AddOffersActivity.this,"Hinzugefügt",Toast.LENGTH_LONG).show();
else
Toast.makeText(AddOffersActivity.this,"Nicht hinzugefügt",Toast.LENGTH_LONG).show();
}
}
);
}

第三行括号里有错误,第15、16行有错误

最佳答案

类似的事情:

public void AddOffer(String art, String namep, String menge, String standort, String gueltigbis, String userzuordnung) {

SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME_TITLE, art);
values.put(COLUMN_NAME_NAMEP, namep);
values.put(COLUMN_NAME_MENGE, menge);
values.put(COLUMN_NAME_STANDORT, standort);
values.put(COLUMN_NAME_GUELTIGBIS, gueltigbis);
values.put(COLUMN_NAME_USERZOUDNUNG, userzuordnung);

long newRowId = db.insert(TABLE_NAME, null, values)
}

或者如果您想使用 userId 更新表的行

 public Int updateOffer(Int userId, String art, String namep, String menge, String standort, String gueltigbis, String userzuordnungl)
{
ContentValues values = new ContentValues();
SQLiteDatabase db = dbHelper.getWritableDatabase();
values.put(COLUMN_NAME_TITLE, art);
values.put(COLUMN_NAME_NAMEP, namep);
values.put(COLUMN_NAME_MENGE, menge);
values.put(COLUMN_NAME_STANDORT, standort);
values.put(COLUMN_NAME_GUELTIGBIS, gueltigbis);
values.put(COLUMN_NAME_USERZOUDNUNG, userzuordnung);
return db.update(TABLE_NAME, contentValues, "UserId=?" + userId,null)
}

关于java - 如何获取表单中的UserID并将其添加到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62423415/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com