我是移动应用程序构建的初学者。我试图将插入数据函数放在我的Android工作室中,但似乎这些插入函数不起作用,输入数据无法插入。请帮忙。
我在“主要活动.java和数据库助手.java中放置了一些代码。它没有给我任何错误报告,但是当我尝试运行模拟器和输入数据时,我的输入可以插入到sqlite数据库中。
oncreate主要活动
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
name2 = findViewById(R.id.name2);
birthdate2 = findViewById(R.id.birthdate2);
area2 = findViewById(R.id.area2);
receiver2 = findViewById(R.id.receiver2);
submit2 = findViewById(R.id.submit2);
submit2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
name = name2.getText().toString();
birth = birthdate2.getText().toString();
area = area2.getText().toString();
receiver = receiver2.getText().toString();
...
insertData2 (name, birth, area, receiver);
...
public void insertData2 (String name,String birth,String area, String receiver){
boolean add_data = myDb.insertData(name,birth,area,receiver);
if (!add_data){
Toast.makeText(MainActivity.this,"Something went wrong><", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"Success to add data!", Toast.LENGTH_LONG).show();
}
}
数据库助手.java
public boolean insertData(String childname ,String bornday, String areaprogram, String receiverid){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL1, childname);
contentValues.put(COL2, bornday);
contentValues.put(COL3, areaprogram);
contentValues.put(COL4, receiverid);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}
else{
return true;
}
}
缥缈止盈
PIPIONE
一只斗牛犬
相关分类