java无法在安卓中将数据插入到数据库的第二个表中
我已经在DatabaseHelper
类中创建了一个名为student.db
的数据库和两个表Student_profile
和Teacher_profile
。当我向第一个表中插入数据时,它会被插入,但当我试图处理第二个表时,它不会被插入。
我确信我在识别用于插入数据的活动的EditText字段中的值时没有犯任何错误。
需要注意的一件特别的事情是,如果我正在插入第二个表Teacher_profile
的条目,调用第一个表Student_profile
的insertData1
函数,它就会被插入(两个表具有相同的列类型和相同的列数),但当我调用其合法的insertData2
函数时,我的问题恰恰是这样
我使用了三个java文件:
主要活动->;插入学生档案表
教师->;插入教师档案表
数据库助手->;java代码,其中编写了用于插入数据库的代码
package com.example.point.sqlite;
import 安卓.content.ContentValues;
import 安卓.content.Context;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelperClass extends SQLiteOpenHelper {
public static final String Database_name="student.db";
public static final String Table_name="Student_profile";
public static final String col1="id";
public static final String col2="name";
public static final String col3="surname";
public static final String col4="marks";
public static final String Table_name2="Teacher_profile";
public static final String cl1="id";
public static final String cl2="name";
public static final String cl3="surname";
public static final String cl4="age";
public DatabaseHelperClass(Context context) {
super(context, Database_name, null, 1);//whenever the constructor will be called the database will be created
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+ Table_name+"(id integer primary key autoincrement,name text,surname text,marks integer)");
db.execSQL("create table "+ Table_name2+"(id integer primary key autoincrement,name text,surname text,age integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table"+Table_name);
db.execSQL("drop table"+Table_name2);
onCreate(db);
}
//for inserting the student records
public boolean insertData(String name,String surname,String marks){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cont=new ContentValues();
cont.put(col2,name);
cont.put(col3, surname);
cont.put(col4,marks);
long result=db.insert(Table_name,null,cont);
if(result==-1) {
return false;
}
else
return true;
}
// for showing student records
public Cursor showDatabase()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from "+ Table_name,null);
return res;
}
//for inserting teacher record
public boolean insertData2(String name,String surname,String age){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cont=new ContentValues();
cont.put(col2,name);
cont.put(col3, surname);
cont.put(col4,age);
long result=db.insert(Table_name2,null,cont);
if(result==-1) {
return false;
}
else
return true;
}
//for showing teacher record
public Cursor showDatabase2()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("select * from "+ Table_name2,null);
return res;
}
public boolean updateDatabaseTable(String id,String name,String surname,String marks){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cont=new ContentValues();
cont.put(col1,id);
cont.put(col2,name);
cont.put(col3, surname);
cont.put(col4,marks);
db.update(Table_name,cont,"id = ?",new String[] {id});//id is the parameter
return true;
}
public int deleteData(String id)
{
SQLiteDatabase db=this.getWritableDatabase();
return db.delete(Table_name,"id = ?",new String[] {id}); }}
package com.example.point.sqlite;
import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.content.Intent;
import 安卓.database.Cursor;
import 安卓.os.Bundle;
import.安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.Toast;
public class MainActivity extends Activity {
DatabaseHelperClass myDb;
EditText ed1,ed2,ed3,id;
Button b;//button to insert into student table
Button teacher,showData,updateData,delData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb=new DatabaseHelperClass(this);
ed1=(EditText)findViewById(R.id.editText);//name
ed2=(EditText)findViewById(R.id.editText2);//surname
ed3=(EditText)findViewById(R.id.editText4);//marks
b=(Button)findViewById(R.id.button);//insert button for student
teacher=(Button)findViewById(R.id.button5);//button to go to teacher.java
showData=(Button)findViewById(R.id.button2);
updateData=(Button)findViewById(R.id.button3);
delData=(Button)findViewById(R.id.button4);
id=(EditText)findViewById(R.id.editText3);//id
//this works well and data get inserted
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted=myDb.insertData(ed1.getText().toString(), ed2.getText().toString(), ed3.getText().toString());
if(isInserted==true)
{
Toast.makeText(getApplicationContext(),"data inserted",Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(),"data not inserted",Toast.LENGTH_LONG).show();
setEmpty();
}
});
teacher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(getApplicationContext(),Teacher.class);
startActivity(i);
}
});
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean res= myDb.updateDatabaseTable(id.getText().toString(),ed1.getText().toString(),ed2.getText().toString(),ed3.getText().toString());
if(res==true)
{
Toast.makeText(getApplicationContext(),"data updated",Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(),"data not updated",Toast.LENGTH_LONG).show();
setEmpty();
}
});
showData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.showDatabase();
if (res.getCount() == 0)
{
showMessage("Error!", "no data found");
return;
}
StringBuffer bf = new StringBuffer();
while (res.moveToNext())
{
bf.append("ID: " + res.getString(0)+"\n");//0 for first column that is col1 id
bf.append("NAME: " + res.getString(1)+"\n");
bf.append("SURNAME: " + res.getString(2)+"\n");
bf.append("MARKS: " + res.getString(3)+"\n\n");
}
showMessage("Data",bf.toString());
setEmpty();
}
});
delData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int deletedRows=myDb.deleteData(id.getText().toString());
if(deletedRows>0)
Toast.makeText(getApplicationContext(),"data deleted",Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),"data not deleted",Toast.LENGTH_LONG).show();
setEmpty();
}
});
}
public void setEmpty()
{
ed1.setText("");
ed2.setText("");
ed3.setText("");
id.setText("");
}
public void showMessage(String title,String message)
{
AlertDialog.Builder build=new AlertDialog.Builder(this);
build.setCancelable(true);
build.setTitle(title);
build.setMessage(message);
build.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}`
package com.example.point.sqlite;
import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.database.Cursor;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import.安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.Toast;
public class Teacher extends Activity {
DatabaseHelperClass myDb;
EditText ed1,ed2,ed3;
Button b;//for inserting teacher records
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher);
myDb=new DatabaseHelperClass(this);
ed1=(EditText)findViewById(R.id.editText7);//name
ed2=(EditText)findViewById(R.id.editText8);//surname
ed3=(EditText)findViewById(R.id.editText9);//age
b=(Button)findViewById(R.id.button6);
/*here is the issue its not getting inserted when i m trying to insert into teacher database table using insertData2 and trying to see it using showDatabase2 */
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData2(ed1.getText().toString(), ed2.getText().toString(), ed3.getText().toString());
if (isInserted == true) {
Toast.makeText(getApplicationContext(), "data inserted", Toast.LENGTH_LONG).show();
Cursor res = myDb.showDatabase2();
if (res.getCount() == 0) {
showMessage("Error!", "no data found");
return;
}
StringBuffer bf = new StringBuffer();
while (res.moveToNext()) {
bf.append("ID: " + res.getString(0) + "\n");//0 for first column that is col1 id
bf.append("NAME: " + res.getString(1) + "\n");
bf.append("SURNAME: " + res.getString(2) + "\n");
bf.append("AGE: " + res.getString(3) + "\n\n");
}
showMessage("Data", bf.toString());
} else
Toast.makeText(getApplicationContext(), "data not inserted", Toast.LENGTH_LONG).show();
}
});
}
public void showMessage(String title,String message)
{
AlertDialog.Builder build=new AlertDialog.Builder(this);
build.setCancelable(true);
build.setTitle(title);
build.setMessage(message);
build.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_teacher, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
# 1 楼答案
检查第二个表插入方法
insertData2
,并使用列名cl2
而不是col2
,其他方法也是如此# 2 楼答案
你必须使用
而不是