gpt4 book ai didi

java - 无法在数据库android中插入

转载 作者:行者123 更新时间:2023-12-01 12:31:42 25 4
gpt4 key购买 nike

数据库插入存在问题。我不知道该怎么办。

它写入错误插入。

public class MainActivity extends Activity implements OnClickListener {

BaseOpener bo;
private static final String ML = "ML";
Button read, write;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
read = (Button) findViewById(R.id.read);
read.setOnClickListener(this);
write = (Button) findViewById(R.id.write);
write.setOnClickListener(this);
bo = new BaseOpener(this);
Log.i("Ml", "good start");
}

@Override
public void onClick(View v) {
SQLiteDatabase db = bo.getWritableDatabase();
switch (v.getId()) {
case R.id.write:
{
ContentValues cv = new ContentValues();
Log.i("ML", "write");
cv.put("id", 1);
cv.put("name", "Petr");
cv.put("phone", "911");
cv.clear();
db.insert("table1", null, cv);
}
break;
case R.id.read:
{`enter code here`
Log.i("ML", "read");
Cursor c = db.query("table1",null,null,null,null,null,null);
if(c.moveToFirst()){
int idColIndex = c.getColumnIndex("id");
int nameColIndex = c.getColumnIndex("name");
int emailColIndex = c.getColumnIndex("phone");
Log.i("ML",
"ID = " + c.getInt(idColIndex) +
", name = " + c.getString(nameColIndex) +
", phone = " + c.getString(emailColIndex));
}
}
break;
}
db.close();
Log.i("ML", "the base is closed");
}
}
public class BaseOpener extends SQLiteOpenHelper {

public BaseOpener(Context context) {
super(context, "contacts", null, 1);
Log.i("ML", "the base is ready");
}`enter code here`
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table table1 (" + "id integer primary key,"
+ "name String," + "phone String" + ");");
Log.i("ML", "table1 is ready");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}

最佳答案

清除()删除所有值。了解有关 ContentValues 的更多信息 here

关于java - 无法在数据库android中插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25876666/

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